1

I am using jjoe64.graphview.GraphView library for plotting graph.

I am appending some data from firebase database but it is showing me error.

here is my code:

 FirebaseDatabase realdata = FirebaseDatabase.getInstance();
        DatabaseReference real = realdata.getReference("weight");
        ValueEventListener workplz = new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

//point value=62,pointvalue1=76
                pointValue = Integer.parseInt(String.valueOf(dataSnapshot.child("currentweight").getValue()));
                pointValue1 = Integer.parseInt(String.valueOf(dataSnapshot.child("goalweight").getValue()));
                Log.i("xValue", String.valueOf(pointValue));
                Log.i("yValue", String.valueOf(pointValue1));


                valuedata = Double.parseDouble(String.valueOf(dataSnapshot.child("currentweight").getValue()));
                hello();
                }
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }};
        real.addValueEventListener(workplz);

        GraphView graph = findViewById(R.id.graph);
        LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
                new DataPoint(0, 1),
                new DataPoint(1, 5),
                new DataPoint(2, 3)
        });
        graph.addSeries(series);
        graph.getViewport().setXAxisBoundsManual(true);
        graph.getViewport().setYAxisBoundsManual(true);
        graph.getViewport().setMinY(0);
        graph.getViewport().setMaxY(200);
        graph.getViewport().setMaxX(0);
        graph.getViewport().setMaxX(200);
        //appending data 
        series.appendData(new DataPoint(pointValue, pointValue1), true, 200);

While running the app i am getting this error .

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
    at com.example.itsandpro.Signup.GraphActivity.onCreate(GraphActivity.java:97)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

i want to add those two values to the graph.I also tried to use those values in double but still the error is same like:

   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
    at com.example.itsandpro.Signup.GraphActivity.onCreate(GraphActivity.java:85)
    at android.app.Activity.performCreate(Activity.java:6679)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
KENdi
  • 7,576
  • 2
  • 16
  • 31
itsandpro
  • 33
  • 8

1 Answers1

1

According to your comment where you say that you are getting those values but still getting the errors, is it because the Firebase API is asynchronous, meaning that by the time you are creating the following object:

new DataPoint(pointValue, pointValue1), true, 200

The data that is coming from the database isn't finished loading yet. A quick solve for this, would be to move all these lines of code:

GraphView graph = findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
    new DataPoint(0, 1),
    new DataPoint(1, 5),
    new DataPoint(2, 3)
});
graph.addSeries(series);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setYAxisBoundsManual(true);
graph.getViewport().setMinY(0);
graph.getViewport().setMaxX(0);
graph.getViewport().setMaxX(200);
//appending data 
series.appendData(new DataPoint(pointValue, pointValue1), true, 200);

Inside the onDataChange() method otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193