ArrayList: Exception in thread "main" java.lang.NullPointerException -
i still have problem when try add person. code suppose detect invalid name , birthday, , not add person personlist. however, because i'm not able birthday until code read it, person object can added when loop finished. problem occurs when try test add invlid person name and/or birthday. shows:
failed add person: invalid name!!
new record:
name: null"
birthday: unkown dob
has been added!
exception in thread "main" java.lang.nullpointerexception @ instruction.readinstruction(instruction.java:272) @ epb.main(epb.java:12) else if(words[0].equalsignorecase("add")) { person p = new person(); string s1 = line.substring(words[0].length()).trim(); string[] s2 = s1.split(";"); if(s2.length>=2)// s2[0] = " name testing three" s2[1] = " birthday 13-05-1981" , s2[2]=" address.." { for(int i=0; i<s2.length;i++) { string s3 = s2[i].trim(); // "delete leading space" s2[0] = "name testing three" s2[1] = "birthday 13-05-1981" string[] s4 = s3.split("\\s+"); //s4[0] = name; s4[1] = testing; s4[2]=three if(s4.length>=2) // s2[1]=birthday 13-05-1986 has length of 2 { if(s4[0].equalsignorecase("name")) { //system.out.println(s4[1]); string name=""; if(functions.namevalidation(s4[1])) { for(int j=2;j<s4.length;j++) { name = s4[1] + " " + s4[j]; } if(functions.namevalidation(name)) { p.setname(name); } else { system.out.println("failed add person: invalid name!"); break; } } else { system.out.println("failed add person: invalid name!!"); break; } }//end of word equals name //----------------------------------------------------------------- else if(s4[0].equalsignorecase("birthday") && s4.length ==2) { if(functions.datevalidation(s4[1])) { try { p.setbirthdaystring(s4[1]); } catch (parseexception e) { //e.printstacktrace(); } } else { system.out.println("failed add person: invalid birthday format"); break; } } //end of word equals birthday //----------------------------------------------------------------- boolean notfound = false; for(person p1: personlist) { if(p1.getname().equals(p.getname())) { if(p1.getbirthdaystring().equals(p.getbirthdaystring())) { system.out.println("information in record" +"\n" + "name: "+ p.getname() + "\n" + "birthday: " + p.getbirthdaystring() + "\n" +"has been updated"); p1.setemail(p.getemail()); p1.setphone(p.getphone()); p1.setaddress(p.getaddress()); notfound = true; } } } if (!notfound) { if(functions.namevalidation(p.getname()) && functions.datevalidation(p.getbirthdaystring())) { system.out.println("new record: " +"\n"+"name: " + p.getname() + "\""+ "\n"+ "birthday: " + p.getbirthdaystring() + "\nhas been added!"); personlist.add(p); } fileio.outdata(personlist, outputfilename); } system.out.println(); }
i think meant in final loop instead of iterating on personlist
, checking @ each step if contains name , birthday of p
, check condition each element in iteration kind of this:
p1.getname().equals(p.getname())
instead of personlist.contains(p.getname())
and
p1.getbirthdaystring().equals(p.getbirthdaystring())
instead of personlist.contains(p.getbirthdaystring())
this fix logic bug, , exception, since won't calling contains
while iterating through collection.
edit: still have bug you're trying add p
personlist
in same loop (in "else" block: personlist.add(p)
). way you're doing not getting exception, it's trying add person p
each p1
in list that's not equal it. fix need keep boolean inside loop checks if matches found (true if of conditions above p1 ever checked), , add p
personlist
outsite loop, if said boolean not true.
in conclusion, modified code iterating loop:
boolean found = false; for(person p1: personlist) { if(p1.getname.equals(p.getname())) { if(p1.getbirthdaystring.equals(p.getbirthdaystring())) { system.out.println("information in record" +"\n" + "name: "+ p.getname() + "\n" + "birthday: " + p.getbirthdaystring() + "\n" +"has been updated"); p1.setemail(p.getemail()); p1.setphone(p.getphone()); p1.setaddress(p.getaddress()); fileio.outdata(personlist, outputfilename); found = true; } } } if (!found) { system.out.println("new record has been added!"); personlist.add(p); fileio.outdata(personlist, outputfilename); }
Comments
Post a Comment