0

Why it gives me NULLPointerException on this line

outputStream.write(array);

the WHOLE MainActivity

package com.example.phoneclient;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

import android.R.string;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity  {
    public Button privateButton; 
    public Socket CommandSocket,receivingSocket,sendingSocket; 
    public OutputStream outputStream;
    DataOutputStream doDataOutputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            CommandSocket = new Socket("134.129.125.126",62431);
        receivingSocket = new Socket("134.129.125.126",52341);
        sendingSocket = new Socket("134.129.125.126",42311);
        } catch (Exception e) {
            // TODO: handle exception
        }


privateButton = (Button)findViewById(R.id.private_Mode);
privateButton.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
    try {
        outputStream = CommandSocket.getOutputStream();

    } catch (Exception e) {
        // TODO: handle exception
    }
    String command = "1";
    byte[] array = command.getBytes();
    System.out.println("byte: "+ array);
    try {
        outputStream.write(array);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    }
});

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }




}

I only want to send this "1" to the server C# by click the button for testing. I don't know why.

user2137886
  • 61
  • 1
  • 7
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Simon Feb 17 '15 at 18:56

1 Answers1

0

If you'd logged the Exception and watches the stacktrace, you would have seen a NetworkOnMainThreadException

You can't open network sockets on the UI thread (in onCreate), so you need to create either an AsyncTask or a Service.