因此,每当我尝试将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());
我不太确定如何解决这个问题,但我认为日期是罪魁祸首。 但是我已经调试了很长时间并且需要你的专业知识。
答案 0 :(得分:0)
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时会出现类似的问题。