在使用Serializable读取对象之后,我试图编辑对象数组。 该代码适用于我的对象的新实例,但是当我尝试编辑从文件读取的对象时,该代码给出了NullPointer异常。
public class ShoppingCenter implements Serializable{
// I cant write to file without transient
transient Scanner scan=new Scanner (System.in);
private static final long serialVersionUID = 6529685098267757692L;
//This is the edit details method which will be used to modify the details
public void editDetails()
{
System.out.println("The details are as follows:");
System.out.println("*Name is "+ name);
System.out.println("\t(1-Edit 2-Continue)");
// *******This is where it gives the exception********
if (scan.nextInt()==1)
{System.out.println("\t Write the new value..");
name=scan.next();
System.out.println("Done..");}
System.out.println("*Location is "+ location);
System.out.println("\t(1-Edit 2-Continue)");
if (scan.nextInt()==1)
{System.out.println("\t Write the new value..");
location=scan.next();
System.out.println("Done..");}
}
这是堆栈跟踪。
java.lang.NullPointerException
at ShoppingCenter.editDetails(ShoppingCenter.java:40)
at Part3Main.searchObject(Part3Main.java:99)
at Part3Main.init(Part3Main.java:130)
at Part3Main.main(Part3Main.java:189)
这是我的司机。
public void init(){
readFile("shoppingCenter");
}
public void readFile(String name){
try{
FileInputStream fis = new FileInputStream(name);
ObjectInputStream ois = new ObjectInputStream(fis);
sc = (ShoppingCenter[]) ois.readObject();
ois.close();
System.out.println("The Object was succesfully read from the file");
//System.out.println(sc[1].toString());
//System.out.println(sc.toString());
}
catch (Exception ex){
ex.printStackTrace();
}
}
一点帮助将不胜感激..谢谢..