Java从txt文件读取向量时出错

Java从txt文件读取向量时出错,java,vector,compiler-errors,Java,Vector,Compiler Errors,我正在尝试将向量写入和读取到文本文件。我创建了一个类writevectorfile(如下所示),它生成一个向量,其中包含一个名为Car的类的对象。类Car实现了Serializable,并且只包含setter和getter方法的信息。在WriteVectorToFile类中,我创建了一个createVector()方法和writetoFile()方法,它们都可以工作。readtoFile()方法中的问题,它给出了一个错误(如下所列)。我想知道我做错了什么,是什么导致了问题 The error :

我正在尝试将向量写入和读取到文本文件。我创建了一个类writevectorfile(如下所示),它生成一个向量,其中包含一个名为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.*;
导入java.util.Vector;
公共类writevectorfile{
私家车1辆;
私人矢量车库;
私有文件myFile;
私有文件输出流输出;
私有文件输入流;
私有对象输出流objin;
私有对象输出流对象;
公共writevectofile(){
this.myFile=新文件(“E:/JAVA/myjava程序/MyVectorFile.txt”);
试一试{
myFile.createNewFile();
System.out.println(“新文件-->成功”);
}捕获(IOE异常){
e、 printStackTrace();
System.out.println(“新文件-->失败”);
}   
}
私有void writetoFile(){
试一试{
out=新文件输出流(myFile);
objout=新的ObjectOutputStream(输出);
}catch(filenotfound异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
对象写入对象(车库);
objout.close();
System.out.println(“写入文件-->成功”);
}捕获(IOE异常){
System.out.println(“写入文件-->失败”);
e、 printStackTrace();
}
}
私有void readtoFile(){
试一试{
FileInputStream in=新的FileInputStream(myFile);
ObjectInputStream objin=新ObjectInputStream(in);
}catch(filenotfound异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//objectobj=null;
Vector tempVec=新向量();
试试{
错误行73:tempVec=(Vector)objin.readObject();
objin.close();
System.out.println(“读取文件-->成功”);
}捕获(例外e){
System.out.println(“读取文件-->失败”);
e、 printStackTrace();
}
//汽车温度=新车();
//tempg=(Car)vecNew.firstElement();
//System.out.println(tempg.toString());
//System.out.println(((Car)(vecNew.firstElement()).toString());
}
私有void createVector(){
this.garage=新向量();
//要创建具有特定数据类型的向量,请添加
//矢量车库=新矢量();
car1=新车(“3245”、“丰田”、“Ferry23”、“双倍”)34500;
本.车库.添加(car1);
新增(新车(“3232”、“菲亚特”、“MozoZ3”、“双倍”)25000);
添加(新车(“2345”,“马自达”,“ZenFix”,“双倍”)13700);
}
公共静态void main(字符串[]args){
WriteVectorToFile测试=新建WriteVectorToFile();
test.createVector();
test.writetoFile();
test.readtoFile();
}
}

因为您已经定义了
私有ObjectInputStream对象
作为实例变量,它是
null
。因此,当您调用
objin.readObject()时null
上的code>将抛出
NullPointerException
。在
readtoFile()
方法中,您可以执行以下操作:

try{
     FileInputStream in = new FileInputStream(myFile);
     ObjectInputStream objin = new ObjectInputStream(in);
   }catch(FileNotFoundException e){ 
      e.printStackTrace();
   }catch(IOException e){
      e.printStackTrace();
   }
定义一个新的本地
ObjectInputStream objin=newobjectinputstream(in)内部try块,它对代码
tempVec=(Vector)objin.readObject()不可见外部尝试捕捉块。这里的
objin
指的是在实例级别声明的实例变量,它是
null
。要纠正它,请更改

ObjectInputStream objin = new ObjectInputStream(in);


在试用区内。

首先,道歉,因为我无法将其作为评论发布,因此将其放入回答区:

您的问题是涉及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范围内创建的,在以下情况下使用时,该范围不同:

tempVec = (Vector) objin.readObject(); 
      objin.close();
      System.out.println("Read File --> Success.");

    }catch(Exception e){

      System.out.println("Read File --> Fail.");
      e.printStackTrace();

    }
因此,解决方案是将所有内容放在一个“try”块中,分别捕获所有异常(或者甚至一起使用异常作为超类[这不是一个好的做法])。这应该可以完成任务。告诉我们你是否解决了这个问题

   try{
     FileInputStream in = new FileInputStream(myFile);
     ObjectInputStream objin = new ObjectInputStream(in);
   }catch(FileNotFoundException e){ 
      e.printStackTrace();
   }catch(IOException e){
      e.printStackTrace();
   }
tempVec = (Vector) objin.readObject(); 
      objin.close();
      System.out.println("Read File --> Success.");

    }catch(Exception e){

      System.out.println("Read File --> Fail.");
      e.printStackTrace();

    }