nullpointerexception - java null pointer exception- Occurs at varying points in while loop -
i have following java while
loop:
while(true){ byte buffer[] = new byte[max_pdu_size]; packet = new datagrampacket(buffer, buffer.length); socket.receive(packet); pdu pdu = pdufactory.createpdu(packet.getdata()); system.out.print("got pdu of type: " + pdu.getclass().getname()); if(pdu instanceof entitystatepdu){ entityid eid = ((entitystatepdu)pdu).getentityid(); vector3double position = ((entitystatepdu)pdu).getentitylocation(); system.out.print(" eid:[" + eid.getsite() + ", " + eid.getapplication() + ", " + eid.getentity() + "] "); system.out.print(" location in dis coordinates: [" + position.getx() + ", " + position.gety() + ", " + position.getz() + "]"); } system.out.println(); } }
the intended function of while loop capture pdus being sent across network, , display information them.
when run code, go output had intended in console- @ least initially... after has returned information number of pdus, got error displayed in console (can't remember said now- thought may because trying capture pdu when there wasn't 1 being sent).
i have tried amending code account occasion pdu may not being received on network @ time trying capture information pdu surrounding code following try- catch loop:
try{ socket = new multicastsocket(espdusender.port); address = inetaddress.getbyname(espdusender.default_multicast_group); socket.joingroup(address); while(true){ byte buffer[] = new byte[max_pdu_size]; packet = new datagrampacket(buffer, buffer.length); socket.receive(packet); pdu pdu = pdufactory.createpdu(packet.getdata()); system.out.print("got pdu of type: " + pdu.getclass().getname()); if(pdu instanceof entitystatepdu){ entityid eid = ((entitystatepdu)pdu).getentityid(); vector3double position = ((entitystatepdu)pdu).getentitylocation(); system.out.print(" eid:[" + eid.getsite() + ", " + eid.getapplication() + ", " + eid.getentity() + "] "); system.out.print(" location in dis coordinates: [" + position.getx() + ", " + position.gety() + ", " + position.getz() + "]"); } system.out.println(); } } catch(exception e){ system.out.println(e); system.out.println("this error being generated"); }
however, when run code- still displays first x number of dis packets captures (x varies every time run code), gives me java.lang.nullpointerexception
. understand, occur either because pdu code has captured not contain information, (i.e. 'empty' pdu), or because attempting receive pdu when there isn't 1 being sent on network.
how can make code 'skip' occasion doesn't receive pdu, , keep running? or there else should rid of error?
this may not fix problem (won't know until put stack trace up), check see if pdu null before using it.
while(true){ byte buffer[] = new byte[max_pdu_size]; packet = new datagrampacket(buffer, buffer.length); socket.receive(packet); pdu pdu = pdufactory.createpdu(packet.getdata()); if (pdu != null) { system.out.print("got pdu of type: " + pdu.getclass().getname()); if(pdu instanceof entitystatepdu){ entityid eid = ((entitystatepdu)pdu).getentityid(); vector3double position = ((entitystatepdu)pdu).getentitylocation(); system.out.print(" eid:[" + eid.getsite() + ", " + eid.getapplication() + ", " + eid.getentity() + "] "); system.out.print(" location in dis coordinates: [" + position.getx() + ", " + position.gety() + ", " + position.getz() + "]"); } system.out.println(); } }
if stack trace out, can change actual problem.
Comments
Post a Comment