在GraphView上设置轴边界时出现NullPointerException

时间:2015-11-19 22:04:47

标签: java android nullpointerexception

我收到以下错误:

select base.*, info.* from TABLENAME base, XMLTABLE('$info/*:Info/*:Output' passing Info as "info" COLUMNS val INTEGER PATH '*:rowValues/*:value/@value', name VARCHAR(200) PATH '*:columnNames/*:name' ) as info

当我尝试在图表视图上设置x和y轴边界时。

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

当我注释掉 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); 行时,它会运行而没有错误,但是边界不会改变,尽管图表工作正常。可能导致这种情况的原因是什么?

2 个答案:

答案 0 :(得分:1)

我解决了这个问题。 在我的情况下

  

.DataPointInterface.getX()

当您将x的最小范围设置为1时,会发生这种情况 并且您的DataPoint数组长度小于1。

例如

// 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());

所以我对此问题的解决方案是:

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());

所以现在我们的代码不会成功。

  

在GraphView

上设置轴边界时出现NullPointerException

就像,当你从0创建图形时,null是&#34; ok&#34;。 从1创建图形时,null不正常。

我知道这不是最佳解决方案,但它可能会对某人有所帮助。

答案 1 :(得分:0)

我解决了这个问题,用一些数据更新了同一GraphView中的所有数据系列。例如,我以这种方式开始了数据系列:

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);
...

并且,在更新一个数据系列时,我也会更新另一个:

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

我希望这可以解决您的问题。