简体   繁体   English

NullPointerException on resultSet尝试将数据加载到TableView中的日期

[英]NullPointerException on resultSet Date trying to load data into tableview

So im getting this NPE whenever im trying to return an ArrayList object into an FXCOLLECTIONS list. 因此,无论何时尝试将ArrayList对象返回到FXCOLLECTIONS列表中,我都会获取此NPE。

Here is the Code. 这是代码。

Method return the al to FXCOLLECTIONS. 方法将al返回到FXCOLLECTIONS。

public ArrayList<MeetBooking> selectMeetBookings()
{
    ArrayList<MeetBooking> meetBookingArrayList = new ArrayList<>();
    System.out.println("Trying to select from table meetBooking");

    try
    {
        //Query the database and storing the result in an object of type ResultSet
        sqlString = "SELECT * FROM CPC.meetBooking ORDER BY bookingID ASC";
        rs = stmt.executeQuery(sqlString);

        //Use the methods of class ResultSet in a loop
        // to display all of the data in the result
        while (rs.next())
        {
            int id = rs.getInt("bookingID");
            Date bookingDateTime = rs.getDate("bookingDateTime");
            System.out.println(bookingDateTime.toString());
            int empID = rs.getInt("employeeID");
            String bookingcmnt = rs.getString("bookingComment");



            MeetBooking meetBooking = new MeetBooking();

            meetBooking.setBookingID(id);
            meetBooking.setBookingDate(bookingDateTime.toString());
            meetBooking.setEmployeeID(empID);
            meetBooking.setBookingComment(bookingcmnt);
            meetBookingArrayList.add(meetBooking);
        }

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return meetBookingArrayList;
}

    Public Class MeetBooking
    //Method to set a date as a string utilises Stringproperty and                      //simplestringproperty
    public void setBookingDate(String value)
    {
    bookingDate.set(value);
    }

    //Controller access to first method
    public ArrayList<MeetBooking> getMeetBookings()
    {
    return dbcon.selectMeetBookings();
    }


    // UI access to controller.getMeetBooks
    meetBookingObservableList =      FXCollections.observableArrayList(controller.getMeetBookings());

Im not quite sure how to solve this, but i think the Date is the culprit. 我不太确定如何解决此问题,但我认为日期是罪魁祸首。 However I've been debugging this for a long time and need your expertise. 但是,我已经调试了很长时间,并且需要您的专业知识。

One potential reason for the NPE is that your database can contain Null values in some of your columns. NPE的一个潜在原因是您的数据库在某些列中可以包含Null值。

If you have Nulls in the bookingDateTime or the employeeID columns, then you will get an exception in one of the following lines: 如果在bookingDateTime或employeeID列中为Null,则以下行之一将出现异常:

Date bookingDateTime = rs.getDate("bookingDateTime");
System.out.println(bookingDateTime.toString());
int empID = rs.getInt("employeeID");

If I remember my Java, the Date class can contain a null. 如果我还记得我的Java,Date类可以包含null。 However, if the value is null, then bookingDateTime.toString() will attempt to convert the Null to string. 但是,如果该值为null,则bookingDateTime.toString()将尝试将Null转换为字符串。 A Null does not have a method, so you get that NullPointerException. Null没有方法,因此您会收到NullPointerException。

Also, an int cannot store Nulls, so you have a similar issue when attempting to force a Null into empID. 另外,一个int不能存储Null,因此在尝试将Null强制为empID时,您也会遇到类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM