1

I am getting the following error:

java.lang.NullPointerException: Attempt to invoke interface method 'double com.jjoe64.graphview.series.DataPointInterface.getX()' on a null object reference

When I try to set the x and y axis bounds on my graph view.

    graph = (GraphView) findViewById(R.id.session_graph);

    graph.getViewport().setXAxisBoundsManual(true); // These lines seem to be causing it
    graph.getViewport().setMinX(1);
    graph.getViewport().setMaxX(50);

    graph.getViewport().setYAxisBoundsManual(true); // These lines seem to be causing it
    graph.getViewport().setMinY(2.0);
    graph.getViewport().setMaxY(15.0);

When I comment out the setX/YBoundsManual() lines, it runs without an error, but the bounds do not change, although the graph works fine. What could be causing this?

Brejuro
  • 3,421
  • 8
  • 34
  • 61
  • There's not enough information here for us to help. Please add the full stack trace, or if you suspect this is a bug in the graph library you are using, you can file a bug report with them. – Logan Pickup Nov 19 '15 at 22:20
  • Try to setX/Y bounds after Min and Max X/Y. – George Nov 22 '15 at 13:40
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Nov 30 '15 at 21:29

2 Answers2

1

I solved this issue. In my case

.DataPointInterface.getX()

Is happening when you set your minimum range of x from 1 and your DataPoint Array length is smaller than 1.

example

// list.size() return null
// for example, no data from Sqlite is assigned to the list
// cus query return nothing.

DataPoint [] statsArray = new DataPoint[list.size()];
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(statsArray);

// set manual X bounds
    graph.getViewport().setXAxisBoundsManual(true);
    graph.getViewport().setMinX(1.0); //when 0 it work, just show nothing.
// when statsArray is null and you try setMinX(1.0) you get this error.
    graph.getViewport().setMaxX(list.size());

So my SOLUTION to this problem is:

List<Integer> list = DatabaseObject.someQuery("SOME EXAMPLE QUERY");
DataPoint [] statsArray;
    if(list.size() > 0) {
        // Code for list longer than 0, query return something
        statsArray = new DataPoint[list.size()]; // so this is not null now
        for (int i = 0; i < statsArray.length; i++) {
            statsArray[i] = new DataPoint(i+1, list.get(i));
            // i+1  to start from x = 1
        }
    }else{
        // Query return nothing, so we add some fake point
        // IT WON'T BE VISIBLE cus we starts graph from 1
        statsArray = new DataPoint[] {new DataPoint(0, 0)};
    }

    GraphView graph = (GraphView) findViewById(R.id.graph);
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(statsArray);
     // set manual X bounds
    graph.getViewport().setXAxisBoundsManual(true);
    graph.getViewport().setMinX(1.0);
    graph.getViewport().setMaxX(list.size());

So now our code won't make.

NullPointerException when setting axis bounds on GraphView

It's like, when you create graph from 0, null is "ok". When you create graph from 1, null is not ok.

I know it's not best solution, but it might help somebody.

Jakub S.
  • 5,580
  • 2
  • 42
  • 37
0

I solved this issue updating all data series in same GraphView with some data. For instance, I started the data series this way:

graphData = new LineGraphSeries<DataPoint>();
graphDataRemote = new LineGraphSeries<DataPoint>();
GraphView graph = new GraphView(this);
graph.addSeries(graphData);
graph.addSeries(graphDataRemote);
graph.getViewport().setMaxY(graphMaxY);
graph.getViewport().setMaxX(graphMaxX);
...

and, when updating one data serie, I also update the other:

graphData.appendData(new DataPoint(x, newvalue), true, graphMaxX);
graphDataRemote.appendData(new DataPoint(x, 0), true, graphMaxX);
x++;

I hope this may solve your issue.

Leo
  • 1
  • 1