-2

私はこのからLINKを使用しようとしています。ここでNullPointerExceptionソケット - Android?

は、クライアントのコードです:

public class MainActivity extends AppCompatActivity { 

    private Socket socket; 

    private static final int SERVERPORT = 5001; 
    private static final String SERVER_IP = "127.0.0.1"; 
    Button myButton; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     myButton = (Button)findViewById(R.id.myButton); 
     myButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        EditText et = (EditText) findViewById(R.id.EditText01); 
        String str = et.getText().toString(); 

        PrintWriter out = new PrintWriter(new BufferedWriter(
          new OutputStreamWriter(socket.getOutputStream())), 
          true); 
        out.println(str); 
       } catch (UnknownHostException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
     new Thread(new ClientThread()).start(); 
    } 

    class ClientThread implements Runnable { 

     @Override 
     public void run() { 

      try { 
       InetAddress serverAddr = InetAddress.getByName(SERVER_IP); 

       socket = new Socket(serverAddr, SERVERPORT); 

      } catch (UnknownHostException e1) { 
       e1.printStackTrace(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 

     } 

    } 
} 

そしてここで、サーバコード:

public class MainActivity extends AppCompatActivity { 

    private ServerSocket serverSocket; 

    Handler updateConversationHandler; 

    Thread serverThread = null; 

    private TextView text; 

    public static final int SERVERPORT = 6001; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     text = (TextView) findViewById(R.id.text2); 

     updateConversationHandler = new Handler(); 

     this.serverThread = new Thread(new ServerThread()); 
     this.serverThread.start(); 

    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     try { 
      serverSocket.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    class ServerThread implements Runnable { 

     public void run() { 
      Socket socket = null; 
      try { 
       serverSocket = new ServerSocket(SERVERPORT); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      while (!Thread.currentThread().isInterrupted()) { 

       try { 

        socket = serverSocket.accept(); 

        CommunicationThread commThread = new CommunicationThread(socket); 
        new Thread(commThread).start(); 

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

    class CommunicationThread implements Runnable { 

     private Socket clientSocket; 

     private BufferedReader input; 

     public CommunicationThread(Socket clientSocket) { 

      this.clientSocket = clientSocket; 

      try { 

       this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 

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

     public void run() { 

      while (!Thread.currentThread().isInterrupted()) { 

       try { 

        String read = input.readLine(); 

        updateConversationHandler.post(new updateUIThread(read)); 

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

    } 

    class updateUIThread implements Runnable { 
     private String msg; 

     public updateUIThread(String str) { 
      this.msg = str; 
     } 

     @Override 
     public void run() { 
      text.setText(text.getText().toString()+"Client Says: "+ msg + "\n"); 
     } 
    } 
} 

は私に怒鳴るのエラーを取得し、私は、サーバー上の任意のメッセージを見ることができない。

enter image description here

+0

入れて全体のコードを、これは例外 – Vickyexpert

+0

ソースコードのリンクはこちらをチェックするだけでは十分ではない:https://での例を.javacodegeeks.com/android/core/socket-core/android-socket-example/ –

+0

リンクが不十分で、コードを表示して、例外を確認できるようにします。 – Vickyexpert

答えて

1

クライアントコードでは、クライアントソケットは作成されていませんが、 socket.getOutputStream()を使用してそのソケットの出力ストリームを取得しようとしています。ソケットはnullなので、コードはNullPointerExceptionをスローします。だから、

、以下に示すようにサーバに接続するために、クライアントのソケットオブジェクトを作成します。

public class MainActivity extends AppCompatActivity { 

private Socket socket; 

private static final int SERVERPORT = 5001; 
private static final String SERVER_IP = "127.0.0.1"; 
Button myButton; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    myButton = (Button)findViewById(R.id.myButton); 
    myButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      try { 
       EditText et = (EditText) findViewById(R.id.EditText01); 
       String str = et.getText().toString(); 
//create socket object as shown below before retrieving the OutputStream object. 
       socket = new Socket(MainActivity.SERVER_IP,MainActivity.SERVERPORT); 
       PrintWriter out = new PrintWriter(new BufferedWriter(
         new OutputStreamWriter(socket.getOutputStream())), 
         true); 
       out.println(str); 
      } catch (UnknownHostException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    new Thread(new ClientThread()).start(); 
} 
//.... and your code goes on! 
関連する問題