反序列化时的java.lang.NullPointerException
这是我的预约课反序列化时的java.lang.NullPointerException,java,nullpointerexception,deserialization,Java,Nullpointerexception,Deserialization,这是我的预约课 public class ViewBooking extends javax.swing.JFrame { /** * Creates new form ViewBooking */ public ViewBooking() { initComponents(); } public void dispBookingInfo(){ Reservation[] reservations = new
public class ViewBooking extends javax.swing.JFrame {
/**
* Creates new form ViewBooking
*/
public ViewBooking() {
initComponents();
}
public void dispBookingInfo(){
Reservation[] reservations = new Reservation[1];
try {
String searchCust = SearchName.getText();
FileInputStream inStream = new FileInputStream(searchCust +
"booking.dat");
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);
reservations[0] = (Reservation) objectInputFile.readObject();
objectInputFile.close();
} catch (Exception e) {
}
JOptionPane.showMessageDialog(null, reservations[0].getDetails());
}
我可以序列化保留对象,但当我尝试反序列化它并读取数据时,我在此行遇到NullPointerException
错误:
public class Reservation implements Serializable {
private String name;
private String sDate;
private String eDate;
private String noOfDays;
private String roomNo;
private String totalAmt;
Reservation(String name, String sDate, String eDate, String noOfDays,
String totalAmt, String roomNo) {
this.name = name;
this.totalAmt = totalAmt;
this.roomNo = roomNo;
//throw new UnsupportedOperationException("Not supported yet.");
//To change body of generated methods, choose Tools | Templates.
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getsDate() {
return sDate;
}
public void setsDate(String sDate) {
this.sDate = sDate;
}
public String geteDate() {
return eDate;
}
public void seteDate(String eDate) {
this.eDate = eDate;
}
public String getNoOfDays() {
return noOfDays;
}
public void setNoOfDays(String noOfDays) {
this.noOfDays = noOfDays;
}
public String getRoomNo() {
return roomNo;
}
public void setRoomNo(String roomNo) {
this.roomNo = roomNo;
}
public String getTotalAmt() {
return totalAmt;
}
public void setTotalAmt(String totalAmt) {
this.totalAmt = totalAmt;
}
public String getDetails(){
return "Name: " + name + "\n" + "From: " + sDate + " to " + eDate
+ "\n" + "Duration: " + noOfDays + "Room No: " + roomNo
+ "Total amount: RM" + totalAmt;
}
}
这里有什么问题
我已将代码更改为以下内容:
JOptionPane.showMessageDialog(null, reservations[0].getDetails());
NullPointerException错误已消失,但我仍然无法检索任何数据。为什么我的预订[0]
为空?在您的
public void dispBookingInfo() throws Exception{
String searchCust = SearchName.getText();
FileInputStream inStream = new FileInputStream(searchCust + " booking.dat");
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);
Reservation[] reservations = new Reservation[1];
try {
if (reservations[0] != null) {
reservations[0] = (Reservation) objectInputFile.readObject();
}
objectInputFile.close();
} catch (Exception e) {
System.out.println("error!");
}
JOptionPane.showMessageDialog(null, reservations[0].getDetails());
}
语句忽略任何抛出的异常。因此,有可能
try {
...
} catch () {
...
}
根本不初始化保留[0]
,这将在访问以下内容时导致NullPointerException
:
reservations[0] = (Reservation) objectInputFile.readObject();
在你的
public void dispBookingInfo() throws Exception{
String searchCust = SearchName.getText();
FileInputStream inStream = new FileInputStream(searchCust + " booking.dat");
ObjectInputStream objectInputFile = new ObjectInputStream(inStream);
Reservation[] reservations = new Reservation[1];
try {
if (reservations[0] != null) {
reservations[0] = (Reservation) objectInputFile.readObject();
}
objectInputFile.close();
} catch (Exception e) {
System.out.println("error!");
}
JOptionPane.showMessageDialog(null, reservations[0].getDetails());
}
语句忽略任何抛出的异常。因此,有可能
try {
...
} catch () {
...
}
根本不初始化保留[0]
,这将在访问以下内容时导致NullPointerException
:
reservations[0] = (Reservation) objectInputFile.readObject();
这是你的问题:
reservation[0].getDetails();
reservations[0]
此时将始终为null
,因为您刚刚初始化了数组。这将停止将数据填充到此处的调用,因此当您稍后尝试使用保留[0]访问它时,getDetails()
该元素不可避免地仍然为null
。null
检查完全不需要,因此将其删除
您还可以考虑为类定义<代码> SerialValueUID<代码>。< /P> 为此,请将其添加为类变量:
if (reservations[0] != null) {
reservations[0] = (Reservation) objectInputFile.readObject();
}
private static final long serialVersionUID=;
将
替换为您喜欢的任何长
。完成后,您必须使用类的“新”版本重新创建文件,否则版本号将不匹配
如果不这样做,JVM会根据类本身自动为您生成一个serialVersionUID
,因此如果您更改了类的某些内容,您可能会突然发现反序列化类的旧版本时遇到问题。这是您的问题:
reservation[0].getDetails();
reservations[0]
此时将始终为null
,因为您刚刚初始化了数组。这将停止将数据填充到此处的调用,因此当您稍后尝试使用保留[0]访问它时,getDetails()
该元素不可避免地仍然为null
。null
检查完全不需要,因此将其删除
您还可以考虑为类定义<代码> SerialValueUID<代码>。< /P> 为此,请将其添加为类变量:
if (reservations[0] != null) {
reservations[0] = (Reservation) objectInputFile.readObject();
}
private static final long serialVersionUID=;
将
替换为您喜欢的任何长
。完成后,您必须使用类的“新”版本重新创建文件,否则版本号将不匹配
如果不这样做,JVM会根据类本身自动为您生成一个
serialVersionUID
,因此如果您更改了类的某些内容,您可能会突然发现反序列化旧版本的类时遇到问题。您捕获了异常
,然后对其不做任何处理。这可能是对你隐瞒了你真正的问题。想想看:reservations[0]
必须为null
才能弹出异常,那么您的捕获是什么呢?您还验证了您正在读取的文件是否存在并包含数据吗?发布堆栈跟踪,不要吞下类似“catch(Exception e){}”这样的异常@JonK我正在读取的文件确实存在并包含数据。为什么:reservations[0]
null?我们能做的最好的回答就是猜测。您需要在catch
块中输出一些有意义的消息,以便找出try
中的真正错误。甚至像e.printStackTrace()这样简单的东西
会给你一个正确的方向。“error!”
不是一个有意义的消息输出-它不会告诉你关于错误的任何信息,只是发生了错误。改为打印堆栈跟踪。您正在捕获异常
,然后什么也不做。这可能是对你隐瞒了你真正的问题。想想看:reservations[0]
必须为null
才能弹出异常,那么您的捕获是什么呢?您还验证了您正在读取的文件是否存在并包含数据吗?发布堆栈跟踪,不要吞下类似“catch(Exception e){}”这样的异常@JonK我正在读取的文件确实存在并包含数据。为什么:reservations[0]
null?我们能做的最好的回答就是猜测。您需要在catch
块中输出一些有意义的消息,以便找出try
中的真正错误。甚至像e.printStackTrace()这样简单的东西
会给你一个正确的方向。“error!”
不是一个有意义的消息输出-它不会告诉你关于错误的任何信息,只是发生了错误。而是打印堆栈跟踪。