Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Use of GraphView apenddata

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Use of GraphView apenddata

    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)

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Use of GraphView apenddata

    java.lang.NullPointerException
    at com.example.test.MainActivity$3$1.run(MainActivity .java:208)
    What variable is null on line 208?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    baseball07 (March 21st, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: Use of GraphView apenddata

    I'm not sure what is null, the values of X and d1 are defined. True is boolean. I even tried defining int numx = 10, so that it now becomes

    data.appendData(new GraphViewData(X, d1), true, numx);

    but it is still giving me the same error.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,349
    Thanks
    49
    Thanked 2,204 Times in 2,177 Posts

    Default Re: Use of GraphView apenddata

    What is the value in the variable: data?
    If you don't understand my answer, don't ignore it, ask a question.