Wednesday 15 April 2015

java.lang.NullPointerException Serialization -



java.lang.NullPointerException Serialization -

i can't work out causing exception. happens in reread() method.

i've tested without reread() method , else works fine.

public class serfiles { private objectinputstream in; private objectoutputstream out; private filereader fr; private filewriter fw; private bufferedreader br; private bufferedwriter bw; private stringtokenizer token; private arraylist<product> prod = new arraylist<product>(); private string line = "c"; private product proc; private int a,b,d,e,f; private string c; public serfiles(){ } public void openfiles() { seek { out = new objectoutputstream( new fileoutputstream( "prod.ser" ) ); fr = new filereader("salesdelim.txt"); br = new bufferedreader(fr); system.out.println("open success"); } catch(exception ex) { system.out.println(ex.tostring()); } } public void readandwrite() { seek { line = br.readline(); while(line != null) { if(line != null) { token = new stringtokenizer(line, "**"); = integer.parseint(token.nexttoken()); b = integer.parseint(token.nexttoken()); c = token.nexttoken(); d = integer.parseint(token.nexttoken()); e = integer.parseint(token.nexttoken()); f = integer.parseint(token.nexttoken()); prod.add(new product(a,c,e,f)); line = br.readline(); } } for(int = 0; i<prod.size(); i++) { out.writeobject(prod.get(i)); } system.out.println("write success"); } catch(exception ex) { system.out.println(ex.tostring()); } } public void reread() { seek { in = new objectinputstream(new fileinputstream("prod.ser")); //////error happens here while(true) { proc = (product)in.readobject(); system.out.println(proc.tostring()); } } catch(eofexception ioe){ return; } catch(exception ex) { system.out.println(ex.tostring()); } } public void closefiles() { seek { fr.close(); br.close(); out.close(); in.close(); system.out.println("close success"); } catch(exception ex) { system.out.println(ex.tostring()); } } }

i've tested without reread() method , worked fine.

thank you

try:

in = new objectinputstream(new fileinputstream("prod.ser")); while (true) { proc = (product)in.readobject(); system.out.println(proc); // change!! if (proc == null) break; // quit loop }

so, problem calling tostring() on null-pointer.

java

No comments:

Post a Comment