I am creating a server chat program in java using DatagramSocket and datagramPacket
im getting a java.lang.NullPointerException in client code to send data packet
try
{
InetAddress ia = InetAddress.getLocalHost();
String s1 = new String(Uname + "-C-" + ia.getHostAddress());
cdp = new DatagramPacket(s1.getBytes(), s1.length(), ia, 7080);
csock.send(cdp);
csock.setSoTimeout(5000);
csock.receive(cdp);
s1 = new String(cdp.getData(), 0, cdp.getLength());
chat.append(s1);
}
catch(Exception e)
{
onlineuser.append("\n" + e);
}
Can someone help me to clear this bug.
Thanks in advance
From what you told us in your comments, you aren’t initializing csock. Be sure to call its constructor before attempting to use it.
You have declared it by saying: public DatagramSocket csock; but you haven’t assigned it any value, and thus it defaults to null. That is the source of your exception.