nullpointerexception,并且get方法返回null值,即使我已经设置了日期

kkbh8khc  于 2021-07-12  发布在  Java
关注(0)|答案(3)|浏览(305)

尝试比较日期时发生nullpointerexception。我注意到在调试时,expecteddate和arriveddate变量值正在获取当前日期时间,因为我正在使用set方法设置日期。请更正用于比较日期的代码。我的方法是用来确定货物是否会准时到达,是在预期的时间之前还是之后。

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,香港,cochin,20-05-2017,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;
    }
}
zengzsys

zengzsys1#

你在打电话给 getexpectedDeliveryDate()new Shipment() ,但我怀疑您希望在收到的装运时将其作为参数调用,否则将保持不变。基本上,试着改变 expectedDate = s.getexpectedDeliveryDate();expectedDate = shipment.getexpectedDeliveryDate();

lh80um4z

lh80um4z2#

参见代码中的注解:

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");
    }
}

在shipping中添加一个方法,该方法包含使用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'
b1uwtaje

b1uwtaje3#

get方法返回空值,即使我已经设置了日期
因为你没有使用 Shipment 对象传递给 displayStatusOfShipment(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.time 应用程序编程接口。这个场景是一个很好的例子,可以让您立即进行切换。让我们看看原因:
现代日期时间api有一个类叫做, LocalDate 它只表示日期,即只有三个字段:年、月和日,因此当您比较 LocalDate ,只比较这些字段。 LocalDate 有一套丰富的用于比较的api。 LocalDate#isAfter , LocalDate#isBefore , LocalDate#isEqual 比较这三个字段的值得到结果。
你会这么说的 java.util.Date 也有 Date#before , Date#after 但这就是这些方法与 LocalDate 末端。这些方法 java.util.Date 比较之后的毫秒数 January 1, 1970, 00:00:00 GMT i、 e.一个 Date 可以在之前/之后,甚至相差一毫秒(不同于 LocalDate 只有在一个或多个字段(日、月和年)中存在差异时,才可以是before/after。

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 你自己和他们比较,以确定之前/之后的情况。选择权在你。

相关问题