InetAddress ipAddr;
What i'm trying to do here is i need to get ip address and then put it in socket
public class L implements ActionListener{
public void actionPerformed(final ActionEvent e){
try {
s = new Socket(ipAddr.getHostAddress(), 6111);
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
dout.writeUTF("L");
dout.writeUTF(" ");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I'm having this error msg
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
You're getting this error because you're trying to call method on a null reference. You need to initialise ipAddr
before using it to call methods which is described later in this answer.
s = new Socket(ipAddr.getHostAddress(), 6111); // your code doesn't initialise ipAddr.
Currently, it is unclear from your question which IPAddress you want. So, I'm assuming you're looking for your system's loopback address (default). There are several options to initialise InetAddress as shown below:
String url = "localhost";
byte addr[] = {127, 0, 0, 1}; // loopback address
InetAddress ip1 = InetAddress.getByName(url);
InetAddress ip2 = InetAddress.getByAddress(addr);
InetAddress ip3 = InetAddress.getLocalHost();
// proceed with your sample code by using any of these InetAddress references
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.