Reputation: 33
I have created a static Handler to receive data from a thread that listens for incoming Bluetooth data. This handler calls: measure_finish2() and this method calls a non-static metod called: measure_finish(), inside the last one i need to call the openFileOutput method but i have a nullPointerException, even when i call a simple Toast i have the same nullPointrException, this is part of my code:
public class MainActivity Extends Activity{
...
public void sending_tubes_to_clean(){
stream_cleaning_tubes = "15";
for(int i = 0;i<4;i++){
if(itemsChecked[i] == true){
stream_cleaning_tubes += "," + Integer.toString(i+1);
}
}
if(conectToServerThread != null){
conectToServerThread.commsThread.write(stream_cleaning_tubes);
Toast.makeText(getBaseContext(), "Cleaning the selected Tubes!", Toast.LENGTH_SHORT).show();
cleaning_status();
}else{
Toast.makeText(getBaseContext(), "You must be connected to the Viscometer!...", Toast.LENGTH_SHORT).show();
}
Log.d(tag,stream_cleaning_tubes);
}
public static Handler MainReceiver = new Handler(){
@Override
public void handleMessage(Message msg){
int numOfBytesReceived = msg.arg1;
byte[] buffer = (byte[]) msg.obj;
String strReceived = new String(buffer);
String comand;
strReceived = strReceived.substring(3,numOfBytesReceived);
comand = strReceived.substring(0,2);
if(comand.toString().equals("03")){
//MainActivity o = new MainActivity();
measure_finish2(strReceived.substring(3));
//measure_finish2(strReceived.substring(3));
}
}
};
public static String get_data(String aux){
String aux2 = null;
aux2 = aux.substring(0,aux.indexOf(","));
return aux2;
}
public static void measure_finish2(String data){
MainActivity o = new MainActivity();
o.measure_finish(data);
}
public void measure_finish(String data){
String data_to_save,strdate,tube,temp,time,cte_tube,visco;
String file_name = "results.txt";
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
strdate = sdf.format(c.getTime());
tube = get_data(data);
data = data.substring(data.indexOf(",")+1);
temp = get_data(data);
data = data.substring(data.indexOf(",")+1);
time = get_data(data);
data = data.substring(data.indexOf(",")+1);
cte_tube = get_data(data);
data = data.substring(data.indexOf(",")+1);
visco = get_data(data);
data_to_save = sample_id_var + user_id_var + strdate + "Tube# " + tube + temp + "°C"+ cte_tube + time + "s" + visco + "cSt" + "\n";
try{
FileOutputStream fos = openFileOutput(file_name,MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
try {
osw.write(data_to_save);
osw.flush();
osw.close();
}catch (IOException e) {
e.printStackTrace();
}
//Toast.makeText(getBaseContext(), "F saved", Toast.LENGTH_SHORT).show();
}catch(FileNotFoundException e)
{
e.printStackTrace();
}
samp_id_view.setText(data_to_save);
cleaning_status();
}
please anyone can help me, this is the error that i have:
04-17 17:09:18.926: E/AndroidRuntime(1685): FATAL EXCEPTION: main
04-17 17:09:18.926: E/AndroidRuntime(1685): Process: com.callirgos.viscopi, PID: 1685
04-17 17:09:18.926: E/AndroidRuntime(1685): java.lang.IllegalStateException: Could not execute method of the activity
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View$1.onClick(View.java:4007)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View.performClick(View.java:4756)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View$PerformClick.run(View.java:19749)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.os.Handler.handleCallback(Handler.java:739)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.os.Looper.loop(Looper.java:135)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.app.ActivityThread.main(ActivityThread.java:5221)
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Native Method)
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Method.java:372)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-17 17:09:18.926: E/AndroidRuntime(1685): Caused by: java.lang.reflect.InvocationTargetException
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Native Method)
04-17 17:09:18.926: E/AndroidRuntime(1685): at java.lang.reflect.Method.invoke(Method.java:372)
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.view.View$1.onClick(View.java:4002)
04-17 17:09:18.926: E/AndroidRuntime(1685): ... 10 more
04-17 17:09:18.926: E/AndroidRuntime(1685): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
04-17 17:09:18.926: E/AndroidRuntime(1685): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:181)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.callirgos.viscopi.MainActivity.measure_finish(MainActivity.java:434)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.callirgos.viscopi.MainActivity.measure_finish2(MainActivity.java:415)
04-17 17:09:18.926: E/AndroidRuntime(1685): at com.callirgos.viscopi.MainActivity.test_files(MainActivity.java:478)
04-17 17:09:18.926: E/AndroidRuntime(1685): ... 13 more
Upvotes: 1
Views: 2816
Reputation: 1006554
MainActivity o = new MainActivity();
First, NEVER create an instance of an Activity
yourself. Android's framework creates those, not you. This o
is not properly initialized, and so any attempts to use it, for openFileOutput()
, a Toast
, etc., will generally crash.
Second, do not do disk I/O on the main application thread. The code shown in measure_finish()
should be done either by the Bluetooth thread or by another background thread, not the main application thread.
Upvotes: 1