nullpointerexception - Null Point Exception in Java -
i having weird problem in code, when execute code first if condition, works fine , excpected. however, when comment if statement , use other if condition (the 1 commented ), gives npe. both getuserip() , getphonenumber() normal getters private strings. , both values being set before normal setters. idea why happening ? thanks.
public void sendbroadcast(final string broadcast) { system.out.println("entered sendbroadcast"); string fullpeep=broadcast; system.out.println("fullpeep: "+fullpeep); string array[] = fullpeep.split("<!!>"); for(user tempuser: friends) { if(tempuser.getuserip().equals(this.getuserip())) { system.out.println("tempuser:" +tempuser.getphonenumber() + " user: "+array[1] ); //if(tempuser.getphonenumber().equals(array[1])) //{ system.out.println("tempuser:" +tempuser.getphonenumber() + " user: "+array[1] ); system.out.println("if statemnt of broadcast method"); try { dataoutputstream out2= new dataoutputstream(socket.getoutputstream()); out2=tempuser.getuserdataoutputstream(); out2.writeutf(fullpeep+"\n"); out2.flush(); } catch (ioexception e) { e.printstacktrace(); } } //} } } exception in thread "thread-6" java.lang.nullpointerexception @ user.sendbroadcast(user.java:180) @ server$serverthread.run(server.java:394)
edit: figured out causing exception , why tempuser.getphonenumber() returning null @ point. all.
change:
if(tempuser.getphonenumber().equals(array[1]))
to:
if(java.util.objects.equals(tempuser.getphonenumber(), array[1]))
objects.equals
null-safe , not mind when phone number null.
Comments
Post a Comment