我正在尝试将矢量写入并读取到文本文件中。我创建了一个类WriteVectorToFile(如下所示),它创建一个包含一个名为Car的类的对象的向量。 Car类实现了Serializable,只包含setter和getter方法的信息。在WriteVectorToFile类中,我创建了一个createVector()方法和writetoFile()方法,它们可以工作。 readtoFile()方法中的问题,它给出了一个错误(如下所列)。我想知道我做错了什么以及导致问题的原因。
The error :
java.lang.NullPointerException
at WriteVectorToFile.readtoFile(WriteVectorToFile.java:73)
at WriteVectorToFile.main(WriteVectorToFile.java:110)
at __SHELL1.run(__SHELL1.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at bluej.runtime.ExecServer$3.run(ExecServer.java:725)
代码:
import java.io.*;
import java.util.Vector;
public class WriteVectorToFile{
private Car car1;
private Vector garage;
private File myFile;
private FileOutputStream out;
private FileInputStream in;
private ObjectInputStream objin;
private ObjectOutputStream objout;
public WriteVectorToFile(){
this.myFile = new File("E:/JAVA/My Java Programs/MyVectorFile.txt");
try{
myFile.createNewFile();
System.out.println("New File --> Success.");
}catch(IOException e){
e.printStackTrace();
System.out.println("New File --> Fail.");
}
}
private void writetoFile(){
try{
out = new FileOutputStream(myFile);
objout = new ObjectOutputStream(out);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
try{
objout.writeObject(garage);
objout.close();
System.out.println("Write File --> Success.");
}catch(IOException e){
System.out.println("Write File --> Fail.");
e.printStackTrace();
}
}
private void readtoFile(){
try{
FileInputStream in = new FileInputStream(myFile);
ObjectInputStream objin = new ObjectInputStream(in);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
// Object obj = null;
Vector tempVec = new Vector();
try{
ERROR Line 73 : tempVec = (Vector) objin.readObject();
objin.close();
System.out.println("Read File --> Success.");
}catch(Exception e){
System.out.println("Read File --> Fail.");
e.printStackTrace();
}
//Car tempg = new Car();
//tempg = (Car) vecNew.firstElement();
//System.out.println(tempg.toString());
//System.out.println(((Car)(vecNew.firstElement())).toString());
}
private void createVector(){
this.garage = new Vector();
// To create a vector with a specific datatype add <type>
// Vector garage = new Vector<Car>();
car1 = new Car("3245","Toyota","Ferry23",(double)34500);
this.garage.add(car1);
this.garage.add(new Car("3232","Fiat","MozoZ3",(double)25000));
this.garage.add(new Car("2345","Mazda","ZenFix",(double)13700));
}
public static void main (String[] args){
WriteVectorToFile test = new WriteVectorToFile();
test.createVector();
test.writetoFile();
test.readtoFile();
}
}
答案 0 :(得分:0)
因为您已将private ObjectInputStream objin;
定义为实例变量且它是null
。因此,当您在objin.readObject();
上调用null
时,会抛出NullPointerException
。在readtoFile()
方法中,您可以执行以下操作:
try{
FileInputStream in = new FileInputStream(myFile);
ObjectInputStream objin = new ObjectInputStream(in);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
在try块中定义了一个新的本地ObjectInputStream objin = new ObjectInputStream(in);
,它对代码tempVec = (Vector) objin.readObject();
ouside try-catch块是不可见的。这里objin
指的是在实例级别声明的实例变量,它是null
。要纠正它改变
ObjectInputStream objin = new ObjectInputStream(in);
到
objin = new ObjectInputStream(in);
在try块内。
答案 1 :(得分:0)
首先,道歉,因为我不能将其作为评论发布,因此将其放入答案块:
您的问题是涉及try-catch块的范围不匹配。如果您已经完成了任何C ++,那么对象将通过值或引用传递,因此可以非常方便地避免这种情况。但是Java会按值传递所有内容,所以当你这样做时:
try{
FileInputStream in = new FileInputStream(myFile);
ObjectInputStream objin = new ObjectInputStream(in);
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
对象objin
是在try-catch范围内创建的,当你在它中使用它时 NOT 是相同的:
tempVec = (Vector) objin.readObject();
objin.close();
System.out.println("Read File --> Success.");
}catch(Exception e){
System.out.println("Read File --> Fail.");
e.printStackTrace();
}
因此解决方案将所有内容放在一个“try”块中并单独捕获所有异常(甚至一起使用Exception作为超类[不是一个好的实践])。这应该做的工作。如果你设法解决它,请告诉我们。