尝试比较日期时发生异常-Nullpointerexception和get方法返回null值,即使我设置了日期


尝试比较日期时发生NUllpointerexception。我注意到,在调试时,expectedDate和arrivedDate变量值正在获取当前日期时间,因为我正在使用setMethod设置日期。请更正我比较日期的代码。我的方法是用来确定货物是准时到达还是在预期之前到达或在预期之后到达。
public class ShipmentBO {
public void displayStatusOfShipment(Shipment shipment) {
Date expectedDate = new Date();
Date arrivedDate = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
Shipment s = new Shipment();
ShipmentStatus SStatus = new ShipmentStatus();
expectedDate = s.getexpectedDeliveryDate();
arrivedDate = SStatus.getarrivedDate();
String s1 = df.format(expectedDate);
String s2 = df.format(arrivedDate);
if (expectedDate.after(arrivedDate)) {
System.out.println("The shipment arrived after the expected date");
} else
if (expectedDate.before(arrivedDate)) {
System.out.println("The shipment arrived before the expected date");
}
}

我正在下面的主类中设置日期

public static void main(String[] args) throws ParseException {

Scanner sc = new Scanner(System.in);
System.out.println("Enter the shipment details :");
String userDetail = sc.nextLine();
String userDetailParts[] = userDetail.split(",");
//System.out.println(Arrays.toString(userDetailParts));

Shipment shipment = new Shipment();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy"); 
shipment.setid(userDetailParts[0]); 
shipment.setsourcePort(userDetailParts[1]); 
shipment.setdestinationPort(userDetailParts[2]);
shipment.setexpectedDeliveryDate(sdf.parse(userDetailParts[3])); 
shipment.setcustomerName(userDetailParts[4]);
}

我给出的输入是逗号分隔的-STAJU01,香港,科钦,2017年5月20日,karthick

装运类别:

import java.util.Date;
public class Shipment {
private String id;
private String sourcePort;
private String destinationPort;
private Date expectedDeliveryDate;
private String customerName;
private ShipmentStatus[] shipmentStatus;
public String getid() {
return id;
}
public void setid(String id) {
this.id = id;
}
public String getsourcePort() {
return sourcePort;
}
public void setsourcePort(String sourcePort) {
this.sourcePort = sourcePort;
}
public String getdestinationPort() {
return destinationPort;
}
public void setdestinationPort(String destinationPort) {
this.destinationPort = destinationPort;
}
public Date getexpectedDeliveryDate() {
return expectedDeliveryDate;
}
public void setexpectedDeliveryDate(Date expectedDeliveryDate) {
this.expectedDeliveryDate = expectedDeliveryDate;
}
public String getcustomerName() {
return customerName;
}
public void setcustomerName(String customerName) {
this.customerName = customerName;
}
public Shipment() {
}
public Shipment(String id, String sourcePort, String destinationPort, Date expectedDeliveryDate,
String customerName) {
this.id = id;
this.sourcePort = sourcePort;
this.destinationPort = destinationPort;
this.expectedDeliveryDate = expectedDeliveryDate;
this.customerName = customerName;
}
public ShipmentStatus[] getShipmentStatus() {
return shipmentStatus;
}
public void setShipmentStatus(ShipmentStatus[] shipmentStatus) {
this.shipmentStatus = shipmentStatus;
}
}

发货状态:

import java.util.Date;
public class ShipmentStatus {
private String arrivalPort;
private String departurePort;
private Date arrivedDate;
private String status;
private Shipment shipment;
public Shipment getshipment() {
return shipment;
}
public void setshipment(Shipment shipment) {
this.shipment = shipment;
}
public String getarrivalPort() {
return arrivalPort;
}
public void setarrivalPort(String arrivalPort) {
this.arrivalPort = arrivalPort;
}
public String getdeparturePort() {
return departurePort;
}
public void setdeparturePort(String departurePort) {
this.departurePort = departurePort;
}
public Date getarrivedDate() {
return arrivedDate;
}
public void setarrivedDate(Date arrivedDate) {
this.arrivedDate = arrivedDate;
}
public String getstatus() {
return status;
}
public void setstatus(String status) {
this.status = status;
}
public ShipmentStatus() {
}
public ShipmentStatus(String arrivalPort, String departurePort, Date arrivedDate, String status,
Shipment shipment) {
this.arrivalPort = arrivalPort;
this.departurePort = departurePort;
this.arrivedDate = arrivedDate;
this.status = status;
this.shipment = shipment;
}
}

您在new Shipment()上调用getexpectedDeliveryDate(),但我怀疑您想在收到的货物上调用它作为一个参数,否则将保持不变。基本上,尝试将expectedDate = s.getexpectedDeliveryDate();更改为expectedDate = shipment.getexpectedDeliveryDate();

请参阅代码中的注释:

public void displayStatusOfShipment(Shipment shipment) {
// not used:
// SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
// extract date from passed shipment instance
Date expectedDate = shipment.getexpectedDeliveryDate();
// I assume ShipmentStatus is part of Shipment so you don't need a new instance here
// ShipmentStatus SStatus = new ShipmentStatus();
// extract arrivalDate from passed shipment's ShipmentStatus:
Date arrivedDate = shipment.getShipmentStatus().getarrivedDate();
// not used :
// String s1 = df.format(expectedDate);
// String s2 = df.format(arrivedDate);
if (expectedDate.after(arrivedDate)) {
System.out.println("The shipment arrived after the expected date");
} else
if (expectedDate.before(arrivedDate)) {
System.out.println("The shipment arrived before the expected date");
}
}

在Shipment中添加一个方法,该方法包含使用ShipmentStatus数组获取发货的arrivedDate的逻辑。你说这是倒数第三次发货状态,所以代码可以是:

class Shipment {
public Date getArrivedDate() {
if(shipmentStatus!=null && shipmentStatus.length>=3)
return shipmentStatus[shipmentStatus.length-3].getArrivedDate();
return null;
}
}

我不确定the 3rd last status是否是确定正确装运状态的好方法。它认为最好根据一些商业逻辑做出选择。例如:

* first shipment status with arrivedDate!=null
* last shipment status with arrivedDate!=null
* shipmentStatus with status='arrival'

get方法返回空值,即使我设置了日期

这是因为您没有使用传递给displayStatusOfShipment(Shipment shipment)Shipment对象;相反,您正在创建一个新实例,如下所示:

Date expectedDate = new Date();
Shipment s=new Shipment();
expectedDate = s.getexpectedDeliveryDate();

您应该将以上三行全部替换为以下行:

Date expectedDate = shipment.getexpectedDeliveryDate();

类似地,对于这种方法,您应该传递一个类型为ShipmentStatus的参数作为到达日期,并使用该参数来获得到达日期,即您需要以类似的方式替换以下三行,如上图所示:

Date arrivedDate = new Date();
ShipmentStatus SStatus = new ShipmentStatus();
arrivedDate = SStatus.getarrivedDate();

在比较日期时:

我曾建议您停止使用设计糟糕的java.util.Date,改用现代的java.timeAPI。这个场景是一个完美的例子,可以让您立即进行切换。让我们看看原因:

  1. 现代日期时间API有一个名为LocalDate的类,它只表示日期,即它只有三个字段:年、月和日,因此当比较LocalDate的两个实例时,只比较这些字段。LocalDate有一套丰富的API用于比较,例如LocalDate#isAfterLocalDate#isBeforeLocalDate#isEqual等,它们将这三个字段的值进行比较,得出结果。

  2. 你会说java.util.Date也有Date#beforeDate#after等,但这就是这些方法与LocalDate方法的比较结束的地方。java.util.Date的这些方法比较自January 1, 1970, 00:00:00 GMT以来的毫秒,即Date的一个实例即使相差一毫秒也可以在之前/之后(不同于LocalDate的两个实例,其仅在一个或多个字段、日、月和年中相差一毫秒(。

    public class Main {
    public static void main(String[] args) {
    Date date1 = new Date();
    Date date2 = new Date();
    date2.setTime(date1.getTime() - 1);
    System.out.println(date1);
    System.out.println(date2);
    System.out.println(date1.after(date2));
    }
    }
    

    输出:

    Wed Aug 19 16:34:56 BST 2020
    Wed Aug 19 16:34:56 BST 2020
    true
    

如果您仍然坚持使用java.util.Date

如果你仍然坚持使用java.util.Date,你必须自己从Date的两个实例中获得日期、月份和年份,并将它们进行比较,以确定之前/之后的情况。选择权在你。

最新更新