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