0

Android handler question

I created a separate thread file

Refer to the thread in another activity.

There is a problem when trying to change UI with handler.

This part is nullpoint.

Message hdmsg= handler.obtainMessage();

I do not know which part is the problem.

ClientThread.java

public class ClientThread extends Thread{
    public ClientThread() {
    }
    public void run(){
        try{
            Thread currThread = Thread.currentThread();
            while (currThread == thisThread) {

                String recvData = ct_in.readUTF();

                StringTokenizer st = new StringTokenizer(recvData, SEPARATOR);
                int command = Integer.parseInt(st.nextToken());
                switch (command) {
                    case MDY_WAITINFO: {
                        StringTokenizer st1 = new StringTokenizer(st.nextToken(), DELIMETER);
                        StringTokenizer st2 = new StringTokenizer(st.nextToken(), DELIMETER); 
                                    /* 
                                     code~ 
                                    */
                        Message hdmsg= handler.obtainMessage();
                        hdmsg.obj=st;
                        handler.sendMessage(hdmsg);

                        break;
                    }
                }
            }
        } catch (IOException e) {
            System.out.println(e);
            release();
        }
    }
}

RoomList.java

public class HostRoomListActivity extends AppCompatActivity {
    public static Handler handler;
    protected void onCreate(Bundle savedInstanceState) {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            SocketHostRoom client = new SocketHostRoom();
            client.start();
            handler = new Handler(){
                public void handleMessage(Message msg){
                    String txtmsg = msg.obj.toString();
                }
            };
        }
        class SocketHostRoom extends Thread{
            public void run(){
                ClientThread thread = new ClientThread();
                thread.start();
                thread.requestHostRoomList();
            }
        }
    }
}
Matt
  • 3,052
  • 1
  • 17
  • 30
sera
  • 73
  • 1
  • 9
  • Possible duplicate of [NullPointerException in Java with no StackTrace](https://stackoverflow.com/questions/2411487/nullpointerexception-in-java-with-no-stacktrace) – Mathieu de Lorimier Apr 10 '18 at 13:19

1 Answers1

1

Try calling HostRoomListActivity.handler.obtainMessage(); instead of justhandler.obtainMessage();

Sorry for the confusion it still needs to be called this way but also you are calling SocketHostRoom client = new SocketHostRoom(); before you are setting the handler

       handler = new Handler(){ 
              public void handleMessage(Message msg){ 
                         String txtmsg = msg.obj.toString();  
              } 
       };

Try setting the handler first the reason this is the issue is because when you call public static Handler handler; it will set it as null

Matt
  • 3,052
  • 1
  • 17
  • 30
  • java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Message android.os.Handler.obtainMessage()' on a null object reference : – sera Apr 10 '18 at 15:06
  • Are you changing the location of handler and client? I still get the same error. – sera Apr 10 '18 at 15:40
  • Yes are you instantiating the handler before you are creating the SocketRoomHost? – Matt Apr 10 '18 at 15:42
  • yes..! HostRoomactivity is recyclerview. Do you have to do it on the adapter? – sera Apr 10 '18 at 15:48
  • I also looked for runonuithread. HostRoomListActivity.this.runOnUiThread(new Runnable(){ }); In the activity.this, a red line is drawn..do you know why? – sera Apr 10 '18 at 15:58