Q:

How create Socket in new Thread

I want create Socket connection but i have some problem. I must create it in new Thread but i can not.
public class SocketManager {
    private static SocketManager instance;

    private BufferedReader in;
    private PrintWriter out;
    private Socket mSocket = null;

    public static SocketManager me() {
        if (instance == null) {
            instance = new SocketManager();
        }
        return instance;
    }

    public void connection() {
        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    mSocket = new Socket(Constants.CHAT_SERVER_URL, 4444);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute();
    }

    public boolean isConnected() {
        return mSocket.isConnected();
    }

and 
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        status = (TextView) findViewById(R.id.status);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        SocketManager.me().connection();
        if (SocketManager.me().isConnected()) {
            status.setText("Connected");
        } else {
            status.setText("Disconnected");
        }

I have error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.net.Socket.isConnected()' on a null object reference

Becouse mSocket create in new Thread and when i call it it == null; How can create mSocket in new Thread and use it?

A:

Best way is to use an interface.
Create an interface in your Socketmanager class
public class SocketManager {
    private static SocketManager instance;
    private SocketListner listner;

    public interface SocketListner {
        void onConnectionSuccess();
        void onConnectionFailed();
    }

    public void connection(SocketListner listner) {
        this.listner = listner;
        new ConnectionTask().execute();
    }

And return a boolean value from doInBackGround() method to check if the connection is success or not
    class ConnectionTask new AsyncTask<Void, Void, Boolean>() {
        @Override
        protected Void doInBackground(Void... params) {
            //...your code

        }
        @Override
        protected void onPostExecute(Boolean result) {
            if(result) {
                listner.onConnectionSuccess();
            } else {
                listner.onConnectionFailed();
            }
        }
    }

And in your activity implement the interface
 public class YourActivity imlpements SocketListner {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
     //your code
      ..
    }
    @Override
    void onConnectionSuccess() {
        //your socket is connected
        status.setText("Connected");
    }

    @Override
    void onConnectionFailed() {
        status.setText("Disconnected");
    }
}

