javaandroidinputstreamoutputstreamwifi-direct

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference


I am trying to create a Chat Application using WiFip2p.Everything is done in this application. Before send and receive message app was working good. After that, I got this error. I am getting this error when I connect to another device. It shows a null object reference. I don't know why I need a solution can anyone help me thank you.

 public class WiFi_Activity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wi_fi_);

        FINDVIEWBYID();
        CLICKLISTINER();
    }


    Handler handler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            switch (msg.what)
            {
                case MESSAGE_READ:

                    byte[]  readbuffer=(byte[])msg.obj;
                    String tempmez=new String(readbuffer,0,msg.arg1);
                    txt_message.setText(tempmez);
                    break;
            }

            return true;
        }
    });

    private void FINDVIEWBYID() {

            btn_onoff=findViewById(R.id.onOff);
            btn_discover=findViewById(R.id.discover);
            btn_send=findViewById(R.id.sendButton);
            listView=findViewById(R.id.peerListView);
            txt_connectionsts=findViewById(R.id.connectionStatus);
            txt_message=findViewById(R.id.readMsg);
            edit_message=findViewById(R.id.writeMsg);

        wifiManager= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        wifiP2pManager= (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);

        channel=wifiP2pManager.initialize(this,getMainLooper(),null);

        broadcastReceiver=new Wifi_broadcast_reciever(wifiP2pManager,channel,this);

        intentFilter=new IntentFilter();

        intentFilter.addAction(wifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
        intentFilter.addAction(wifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
        intentFilter.addAction(wifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
        intentFilter.addAction(wifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);

    }
    private void CLICKLISTINER() {

        btn_onoff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (wifiManager.isWifiEnabled())
                {
                    wifiManager.setWifiEnabled(false);
                    btn_onoff.setText("ON");
                }
                else
                {
                    wifiManager.setWifiEnabled(true);
                    btn_onoff.setText("OFF");
                }

            }
        });

        btn_discover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                wifiP2pManager.discoverPeers(channel, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {

                        txt_connectionsts.setText("Discovery Started");
                    }

                    @Override
                    public void onFailure(int reason) {

                        txt_connectionsts.setText("Discovery starting failuree"+String.valueOf(reason));
                    }
                });
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                 final  WifiP2pDevice wifiP2pDevice=devicearray[position];
                WifiP2pConfig config=new WifiP2pConfig();
                config.deviceAddress=wifiP2pDevice.deviceAddress;
                wifiP2pManager.connect(channel, config, new WifiP2pManager.ActionListener() {
                    @Override
                    public void onSuccess() {

                        Toast.makeText(WiFi_Activity.this, "Connect with"+ wifiP2pDevice.deviceName, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(int reason) {

                        Toast.makeText(WiFi_Activity.this, "Not connected", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String msz=edit_message.getText().toString();
                sendRecieve.write(msz.getBytes());
            }
        });
    }

    WifiP2pManager.PeerListListener peerListListener=new WifiP2pManager.PeerListListener() {
        @Override
        public void onPeersAvailable(WifiP2pDeviceList peerslist) {

            if (!peerslist.getDeviceList().equals(peers));
            {
                peers.clear();
                peers.addAll(peerslist.getDeviceList());

                devicename_array=new String[peerslist.getDeviceList().size()];
                devicearray=new WifiP2pDevice[peerslist.getDeviceList().size()];
                int index= 0;

                 for (WifiP2pDevice device:peerslist.getDeviceList())
                 {
                     devicename_array[index]=device.deviceName;
                     devicearray[index]=device;
                     index++;
                 }

                 ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,devicename_array);
                 listView.setAdapter(arrayAdapter);
            }

            if (peers.size()==0)
            {
                Toast.makeText(WiFi_Activity.this, "No discover show", Toast.LENGTH_SHORT).show();
            }
        }
    };

    WifiP2pManager.ConnectionInfoListener connectionInfoListener=new WifiP2pManager.ConnectionInfoListener() {
        @Override
        public void onConnectionInfoAvailable(WifiP2pInfo info) {

            final InetAddress owneraddress= info.groupOwnerAddress;

            if (info.groupFormed && info.isGroupOwner)
            {
                txt_connectionsts.setText("Host");
                serverclass=new Serverclass();
                serverclass.start();
            }
            else if (info.groupFormed)
            {
                txt_connectionsts.setText("Client");
                clientclass = new Clientclass(owneraddress);
                clientclass.start();

            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        registerReceiver(broadcastReceiver,intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(broadcastReceiver);
    }

    public class Serverclass extends Thread
    {
        Socket socket;
        ServerSocket serverSocket;

        @Override
        public void run() {

            try {
                serverSocket=new ServerSocket(1111);
                socket=serverSocket.accept();
                sendRecieve=new SendRecieve(socket);
                sendRecieve.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public class Clientclass extends Thread
    {

        Socket socket;
        String host_address;

        public Clientclass(InetAddress hostaddress){

            host_address=hostaddress.getHostAddress();
            socket=new Socket();
            sendRecieve=new SendRecieve(socket);
            sendRecieve.start();
        }

        @Override
        public void run() {

            try {
                socket.connect(new InetSocketAddress(host_address,1111),500);


            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private class SendRecieve extends Thread
    {
        private Socket socket;
        private InputStream inputStream;
        private OutputStream outputStream;

        public SendRecieve(Socket skt)
        {

            socket=skt;

            try {
                inputStream=socket.getInputStream();
                outputStream=socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {

            byte[] buffer=new byte[2048];
            int bytes;

            while (socket!=null)
            {
                try {
                    bytes=inputStream.read(buffer);
                    if (bytes>0)
                    {
                        handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
                    }
                } catch (IOException e) {

                    Log.d("error",e.getMessage());
                }
            }
        }


        public void write(byte[] bytes)
        {
            try {
                outputStream.write(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

I am getting this error on this part of code:-

  @Override
    public void run() {

        byte[] buffer=new byte[2048];
        int bytes;

        while (socket!=null)
        {
            try {
                bytes=inputStream.read(buffer);
                if (bytes>0)
                {
                    handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
                }
            } catch (IOException e) {

                Log.d("error",e.getMessage());
            }
        }
    }

Solution

  • You calling Sendreceiver start before Clientclas run executes, so the following is necessary:

    public class Clientclass extends Thread {
    
    Socket socket;
    String host_address;
    
    public Clientclass(InetAddress hostaddress){
    
        host_address=hostaddress.getHostAddress();
        socket=new Socket();
    }
    
    @Override
    public void run() {
    
        try {
            socket.connect(new InetSocketAddress(host_address,1111),500);
    
        sendRecieve=new SendRecieve(socket);
        sendRecieve.start();
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }