null pointer exception on DatagramPacket

i have built a chat application that uses both UDP and TCP sockets and it works fine, but after i started testing it, i figure out some strange null pointer exception arises after i go to the main activity and then get back to employee interface and press chat button, so the application is basically has an employee activity that displays information about the employee it also has a button chat when ever you press on it, it will hide some the employee’s info and display a chat dialog in the same activity to make you able to chat with the current employee, the problem is that when ever i go to the previous activity and get back to the employee info and press chat, it gives a null pointer exception.
PS. it was giving address already in use on serverSocket but i closed it onPause() method and onClick() of an
here is the thread that get called after i press chat

private class ChatClientThread extends Thread{
    String sentence;
    String modifiedSentence;
    BufferedReader inFromUser;

    DataOutputStream outToServer;
    BufferedReader inFromServer;

    @Override
    public void run(){
        /*Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream=null;*/
        // clientSocket=null;

        try{
            String data=EmpPhoneNumber;

            DatagramChannel channel = DatagramChannel.open();
            DatagramSocket socket = channel.socket();
            socket.setReuseAddress(true);
            socket.bind(new InetSocketAddress(52276));
            socket.setBroadcast(true);
            InetAddress group = InetAddress.getByName("224.0.1.2");

            DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(),
            group, 52276);
            serverSocket=new ServerSocket();
            serverSocket.setReuseAddress(true);
            serverSocket.bind(new InetSocketAddress(4125));
            int i = 0;
            while(i <= 30){
                Thread.sleep(200);
                socket.send(packet);
                i++;
            }

            while(true){
                clientSocket = serverSocket.accept();

                BufferedReader br= new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                strAcceptConnection= br.readLine();
                System.out.println(strAcceptConnection);

                if(!strAcceptConnection.contains("$@!")){
                    updateTextView("<font color="#0000A0"> <b>"+empNameChat+": </b></font>"+"Error in recieving msg");
                }else{
                    String parts[]=  strAcceptConnection.split("[$@!]+");

                    if(!parts[1].equals("ImReadyNoW")){
                        updateTextView("<font color="#0000A0"> <b>"+empNameChat+": </b></font>"+parts[2]);
                    }
                }
            }

            /* socket = new Socket("192.168.0.101", 23);
            dataOutputStream = new DataOutputStream(
            socket.getOutputStream());



            dataOutputStream.writeUTF("  "+"SolveProblemOrder_2");
            dataOutputStream.flush();
            dataInputStream = new DataInputStream(
            socket.getInputStream());


            String inMsg = "";
            boolean b=false;
            while (!b)
            {
            inMsg = dataInputStream.readUTF();
            if(inMsg!="")
            b=true;
            }
            //close socket.close();
            System.out.println(inMsg);
            Log.d("ClientActivity", "C: Closed.");*/
        }catch (UnknownHostException e){
            e.printStackTrace();
            final String eString = e.toString();

            EmpDetails.this.runOnUiThread(new Runnable(){
                @Override
                public void run(){
                    Toast.makeText(EmpDetails.this, eString, Toast.LENGTH_LONG).show();
                }
            });
        }catch(IOException e){
            e.printStackTrace();
            final String eString = e.toString();

            EmpDetails.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                Toast.makeText(EmpDetails.this, eString, Toast.LENGTH_LONG).show();
                }
            });
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (NullPointerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            EmpDetails.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {

                }
            });
        }
    }
}

null pointer exception happens on:

DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), group, 52276);

Any help!


Source: java

Leave a Reply