im creating an android app, to link and transfer socket packets to the server through wifi. In order for this, the IP address of the connected server has to be defined. I used the below function to get the IP address. When i hardcode the SERVER_IP = "0"; the app runs normally. Please help!
    private final String SERVER_IP = getIpAddr();
    public String getIpAddr() {
    WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    String ipString = String.format(
            "%d.%d.%d.%d",
            (ip & 0xff),
            (ip >> 8 & 0xff),
            (ip >> 16 & 0xff),
            (ip >> 24 & 0xff));
    return ipString;
}
After Setting up the above, I ran the below codes on the OnCreate function.
class ClientThread implements Runnable {
    @Override
    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}
As soon as that happens, my android application will stop working.
This is the error that appears:
Java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
 
     
    