Я пытаюсь отредактировать массив объектов после их прочтения с помощью 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..");}
}
Это стек Trace.
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();
}
}
Небольшая помощь была бы очень признательна .. Спасибо ..
Это сработало .... Большое спасибо .. !!!!
Временные объекты не десериализованы, поэтому при десериализации поле сканирования в ShoppingCenter будет пустым. Разве вы не можете создать сканирование в editDetails ()?