简体   繁体   中英

Null pointer exception when create a new thread

i have a thread class which is extended with Thread, i am trying to create an object of that class in other class and start the thread from there,thread will be created when the button will be pressed but the app runs fine only once, my main class from where i am creating the thread is :

broadcastobject=new broadcast(messages);
broadcastobject.start();

My thread class is:

public class broadcast  extends Thread{

private DatagramSocket socket;
String str;
private static final int TIMEOUT_MS = 10;
WifiManager mWifi;
EditText et;
DatagramPacket packet;
Button bt;
private static final int SERVERPORT = 11111;
private static final String SERVER_IP = "192.168.1.255";


public  broadcast(String msg){
    str=msg;
}

@Override
    public void run() {


        try {
            socket = new DatagramSocket(SERVERPORT);

            socket.setBroadcast(true);
       //               socket.setSoTimeout(TIMEOUT_MS);
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            InetAddress serverAddr = null;
            try {


                serverAddr = InetAddress.getByName(SERVER_IP);


            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

         packet = new DatagramPacket(str.getBytes(), str.length(),serverAddr,SERVERPORT);

            try {

                    socket.send(packet);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    } 

    }

My log cat is:

     09-02 15:46:47.603: W/System.err(1348): java.net.BindException: bind failed: EADDRINUSE (Address already in use)
     09-02 15:46:47.613: W/System.err(1348):    at libcore.io.IoBridge.bind(IoBridge.java:89)
   09-02 15:46:47.613: W/System.err(1348):  at java.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:68)
  09-02 15:46:47.643: W/System.err(1348):   at java.net.DatagramSocket.createSocket(DatagramSocket.java:133)
  09-02 15:46:47.703: W/System.err(1348):   at java.net.DatagramSocket.<init>(DatagramSocket.java:78)
   09-02 15:46:47.703: W/System.err(1348):  at soft.b.peopleassist.broadcast.run(broadcast.java:65)
  09-02 15:46:47.703: W/System.err(1348): Caused by: libcore.io.ErrnoException: bind failed: EADDRINUSE (Address already in use)
  09-02 15:46:47.703: W/System.err(1348):   at libcore.io.Posix.bind(Native Method)
   09-02 15:46:47.772: W/System.err(1348):  at libcore.io.ForwardingOs.bind(ForwardingOs.java:39)
   09-02 15:46:47.772: W/System.err(1348):  at libcore.io.IoBridge.bind(IoBridge.java:87)
  09-02 15:46:47.772: W/System.err(1348):   ... 4 more
  09-02 15:46:47.793: W/dalvikvm(1348): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
09-02 15:46:47.823: E/AndroidRuntime(1348): FATAL EXCEPTION: Thread-79
 09-02 15:46:47.823: E/AndroidRuntime(1348): java.lang.NullPointerException
 09-02 15:46:47.823: E/AndroidRuntime(1348):    at soft.b.peopleassist.broadcast.run(broadcast.java:90)
  09-02 15:47:21.832: I/Process(1348): Sending signal. PID: 1348 SIG: 9

You are trying to bind a socket on a port, which is already used. You need to close the bound socket before binding a new one.

The Linux system under your app may need some time to notice, that the bound socket is gone, if you just crash your app or forget to call Socket.close() in onDestroy() or wherever it's appropriate in your case.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM