0

I am getting an error which says as Java Null Point Exception. I tried debugging and saw that the error is persisting only. I think there is a problem with the printwriter or the client object.

Is it any initialization error ?

I have even closed every socket that i opened up.

Is it something like i have placed few lines at some other place ?
The code is:

    package com.example.temp;

    public class MainActivity extends Activity {
        private Socket client;
        private PrintWriter printwriter;
        private EditText textField;
        private Button button;
        private String messsage;

        StringBuilder sb = new StringBuilder();
        public final static String extra = "com.example.temp.MESSAGE";  
        protected static final long TIME_DELAY = 5000;
        TextView mTextView;
        Handler handler=new Handler();  
        int count =0; String data ="";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTextView = (TextView) findViewById(R.id.text_id);
            messsage= mTextView.getText().toString();
            new Asynctask().execute(messsage);  
            handler.post(updateTextRunnable);       
        }

        Runnable updateTextRunnable = new Runnable() {
            public void run() {
                if (count < 5) {
                    WifiManager mainWifiObj;
                    mainWifiObj = 
                        (WifiManager) getSystemService(Context.WIFI_SERVICE);

                    class WifiScanReceiver extends BroadcastReceiver {
                        public void onReceive(Context c, Intent intent) {
                        }
                    }

                    WifiScanReceiver wifiReciever = new WifiScanReceiver();
                    registerReceiver(wifiReciever, new IntentFilter(
                        WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));                
                    List<ScanResult> wifiScanList = 
                        mainWifiObj.getScanResults();
                    for (ScanResult result : wifiScanList) {
                        if (result.SSID.equals("Khosla ka Ghosla")) {
                            sb.append(result.level);
                        }               
                        count++;
                        mTextView.setText("getting called " +count + sb);                   
                    } else {
                }                                
                handler.postDelayed(this, TIME_DELAY);
            }
        };  

        public class Asynctask extends AsyncTask<String, Void, Void> {
            private static final String IP_ADDRESS = "192.168.0.8";
            private static final int DEST_PORT = 4444;

            private EditText mTextField;

            protected Void doInBackground(String... messages) {
                String message = messages[0];
                Socket client = null;
                try {
                    client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                // Write to server.
                try {
                    printwriter = 
                        new PrintWriter(client.getOutputStream(), true);
                    printwriter.write(messsage); // write the message to output stream
                }
                catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {                   
                        if(client !=null){
                            printwriter.flush();
                            printwriter.close();
                            client.close();
                        }
                    } catch (IOException e) { 
                        e.printStackTrace();
                    }
                }
                return null;
            }
        }
    }

When I run the current code, the error comes in the line : printwriter.write(messsage). Can you please guide me with this.

Thanks in advance

mdewitt
  • 2,464
  • 17
  • 23
  • 1
    Show your stacktrace too. – Amir Raminfar Feb 17 '14 at 23:15
  • why would there be anything in `mTextView.getText().toString();`? – njzk2 Feb 17 '14 at 23:17
  • @njzk2 my signal strength would be placed there. I am confused, where should I keep it then ? – user3316775 Feb 17 '14 at 23:19
  • @AmirRaminfar stacktrace shown – user3316775 Feb 17 '14 at 23:24
  • `printwriter.write(messsage)` cannot throw a NPE in *that* line if the line before is `printwriter = new PrintWriter(..`. because the only thing that could be `null` is `printwriter` and that can't be null after you assigned it something `new`. Are you sure that it is line 106? – zapl Feb 17 '14 at 23:24
  • in linr...printwriter = new PrintWriter(client.getOutputStream(), true); – user3316775 Feb 17 '14 at 23:35
  • 1
    The only thing that can throw in that line is `client.` - I'd check if / why it could be null. You might see then why chaining try catch blocks is a bad idea :) – zapl Feb 17 '14 at 23:40
  • You really need to add your stack trace if you are going to get any useful help. – cistearns Feb 17 '14 at 23:57
  • Client presents null result as well as print writer presents null result. – user3316775 Feb 18 '14 at 03:22
  • there is a try{} catch around the creation of the client. there is probably another exception logged there. you should not continue the treatment if the creation of the client raises an exception. – njzk2 Feb 18 '14 at 13:48

2 Answers2

0

You should do this in the finally block.

if (printwriter != null) {
  printwriter.flush();
  printwriter.close();
}
Amir Raminfar
  • 32,592
  • 7
  • 83
  • 119
0

You really need to debug your code. I can see a couple of possible issues

String message = messages[0];

Are you sure that message get created properly? Most likely it is not the problem as you didn't receive the index out of bounds exception. But have another look any way.

client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server

What is the value of the "client" after you run this line of the code? Is it null?

printwriter = new PrintWriter(client.getOutputStream(), true)

Are you sure that the printwriter got created?

Vlad Spreys
  • 4,239
  • 4
  • 35
  • 41
  • Client presents null result as well as print writer presents null result. – user3316775 Feb 18 '14 at 03:23
  • So the client is the problem then. You didn't manage to create the Socket, so that's why you are getting the error. Try to remove the try catch block around the line and see what kind of exception you are getting – Vlad Spreys Feb 18 '14 at 03:36