[英]NullPointerException on resultSet Date trying to load data into tableview
因此,無論何時嘗試將ArrayList對象返回到FXCOLLECTIONS列表中,我都會獲取此NPE。
這是代碼。
方法將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());
我不太確定如何解決此問題,但我認為日期是罪魁禍首。 但是,我已經調試了很長時間,並且需要您的專業知識。
NPE的一個潛在原因是您的數據庫在某些列中可以包含Null值。
如果在bookingDateTime或employeeID列中為Null,則以下行之一將出現異常:
Date bookingDateTime = rs.getDate("bookingDateTime");
System.out.println(bookingDateTime.toString());
int empID = rs.getInt("employeeID");
如果我還記得我的Java,Date類可以包含null。 但是,如果該值為null,則bookingDateTime.toString()將嘗試將Null轉換為字符串。 Null沒有方法,因此您會收到NullPointerException。
另外,一個int不能存儲Null,因此在嘗試將Null強制為empID時,您也會遇到類似的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.