I am using GraphView appenddata to plot incoming data in real-time in Android.
Android GraphView - official Project Homepage
However, my program crashes when loading it onto my device (Jellybean 4.2.2) upon reaching the line with appenddata. In my logcat at the line with appenddata, I am getting "java.lang.NullPointerException", however I don't see anything that is being returned as NULL. I have added GraphView-3.1.1.jar to my libs folder in Eclipse.
This is my OnCreate
GraphViewSeries data; GraphView graph; double X = 0; //initial graph x value double d1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button openButton = (Button)findViewById(R.id.open); Button closeButton = (Button)findViewById(R.id.close); myLabel = (TextView)findViewById(R.id.myLabel); textView1 = (TextView)findViewById(R.id.textView1); LinearLayout layout = (LinearLayout) findViewById(R.id.graph1); graph = new LineGraphView(this, "Frequency"); GraphViewSeries data = new GraphViewSeries(new GraphView.GraphViewData[] { new GraphView.GraphViewData(X, d1) }); graph.addSeries(data); // data layout.addView(graph); //Open Button openButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { TimeUnit.MILLISECONDS.sleep(500); //delay added to ensure bytes come in the correct order } catch (InterruptedException ie) { //Handle exception } try { findBT(); openBT(); } catch (IOException ex) { } } }); //Close button closeButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { try { closeBT(); } catch (IOException ex) { } } }); }
This is where I read data and append it to the graph.
void beginListenForData() { final Handler handler = new Handler(); stopWorker = false; readBuffer = new byte[2]; //insert # of bytes to read workerThread = new Thread(new Runnable() { public void run() { while(!Thread.currentThread().isInterrupted() && !stopWorker) { try { try { TimeUnit.MILLISECONDS.sleep(100); //delay added to ensure bytes come in the correct order } catch (InterruptedException ie) { //Handle exception } int bytesAvailable = mmInputStream.available(); System.out.println("Bytes available =" +bytesAvailable); if(bytesAvailable > 0){ System.out.println("Bytes available =" +bytesAvailable); byte[] packetBytes = new byte[bytesAvailable]; mmInputStream.read(packetBytes); readBuffer = packetBytes; System.out.println("packetBytes = "+ java.util.Arrays.toString(packetBytes)); BigInteger bi = new BigInteger(readBuffer); System.out.println(bi); d1 = bi.doubleValue(); //format to double for GraphView System.out.println(d1); final String s = bi.toString(); // Format to string for TextView System.out.println(s); handler.post(new Runnable() { public void run() { textView1.setText(s); System.out.println(s); X += 1; //0+1+1, etc. System.out.println(d1); System.out.println(X); data.appendData(new GraphViewData(X, d1), true, 10); //crashing here } }); } } catch (IOException ex) { stopWorker = true; } } } }); workerThread.start(); }
Logcat
03-19 17:04:05.232: D/BluetoothSocket(9590): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[52]} 03-19 17:04:08.132: I/Choreographer(9590): Skipped 203 frames! The application may be doing too much work on its main thread. 03-19 17:04:08.232: I/System.out(9590): Bytes available =0 03-19 17:04:08.332: I/System.out(9590): Bytes available =0 03-19 17:04:08.432: I/System.out(9590): Bytes available =2 03-19 17:04:08.432: I/System.out(9590): Bytes available =2 03-19 17:04:08.432: I/System.out(9590): packetBytes = [47, 42] 03-19 17:04:08.432: I/System.out(9590): 12074 03-19 17:04:08.432: I/System.out(9590): 12074.0 03-19 17:04:08.432: I/System.out(9590): 12074 03-19 17:04:08.432: I/System.out(9590): 12074 03-19 17:04:08.432: I/System.out(9590): 12074.0 03-19 17:04:08.442: I/System.out(9590): 1.0 03-19 17:04:08.442: D/AndroidRuntime(9590): Shutting down VM 03-19 17:04:08.442: W/dalvikvm(9590): threadid=1: thread exiting with uncaught exception (group=0x41dfe930) 03-19 17:04:08.452: E/AndroidRuntime(9590): FATAL EXCEPTION: main 03-19 17:04:08.452: E/AndroidRuntime(9590): java.lang.NullPointerException 03-19 17:04:08.452: E/AndroidRuntime(9590): at com.example.test.MainActivity$3$1.run(MainActivity.java:208) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.os.Handler.handleCallback(Handler.java:725) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.os.Handler.dispatchMessage(Handler.java:92) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.os.Looper.loop(Looper.java:137) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.app.ActivityThread.main(ActivityThread.java:5041) 03-19 17:04:08.452: E/AndroidRuntime(9590): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 17:04:08.452: E/AndroidRuntime(9590): at java.lang.reflect.Method.invoke(Method.java:511) 03-19 17:04:08.452: E/AndroidRuntime(9590): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-19 17:04:08.452: E/AndroidRuntime(9590): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-19 17:04:08.452: E/AndroidRuntime(9590): at dalvik.system.NativeStart.main03-19 17:04:05.232: D/BluetoothSocket(9590): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[52]} 03-19 17:04:08.132: I/Choreographer(9590): Skipped 203 frames! The application may be doing too much work on its main thread. 03-19 17:04:08.232: I/System.out(9590): Bytes available =0 03-19 17:04:08.332: I/System.out(9590): Bytes available =0 03-19 17:04:08.432: I/System.out(9590): Bytes available =2 03-19 17:04:08.432: I/System.out(9590): Bytes available =2 03-19 17:04:08.432: I/System.out(9590): packetBytes = [47, 42] 03-19 17:04:08.432: I/System.out(9590): 12074 03-19 17:04:08.432: I/System.out(9590): 12074.0 03-19 17:04:08.432: I/System.out(9590): 12074 03-19 17:04:08.432: I/System.out(9590): 12074 03-19 17:04:08.432: I/System.out(9590): 12074.0 03-19 17:04:08.442: I/System.out(9590): 1.0 03-19 17:04:08.442: D/AndroidRuntime(9590): Shutting down VM 03-19 17:04:08.442: W/dalvikvm(9590): threadid=1: thread exiting with uncaught exception (group=0x41dfe930) 03-19 17:04:08.452: E/AndroidRuntime(9590): FATAL EXCEPTION: main 03-19 17:04:08.452: E/AndroidRuntime(9590): java.lang.NullPointerException 03-19 17:04:08.452: E/AndroidRuntime(9590): at com.example.test.MainActivity$3$1.run(MainActivity.java:208) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.os.Handler.handleCallback(Handler.java:725) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.os.Handler.dispatchMessage(Handler.java:92) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.os.Looper.loop(Looper.java:137) 03-19 17:04:08.452: E/AndroidRuntime(9590): at android.app.ActivityThread.main(ActivityThread.java:5041) 03-19 17:04:08.452: E/AndroidRuntime(9590): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 17:04:08.452: E/AndroidRuntime(9590): at java.lang.reflect.Method.invoke(Method.java:511) 03-19 17:04:08.452: E/AndroidRuntime(9590): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-19 17:04:08.452: E/AndroidRuntime(9590): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-19 17:04:08.452: E/AndroidRuntime(9590): at dalvik.system.NativeStart.main(Native Method)