当前位置: 动力学知识库 > 问答 > 编程问答 >

android - achartengine cause null pointer

问题描述:

I have issue regarding package org.achartengine (both available versions 1.2.0 and 1.1.0).

In my application I call new activity, pass on parameters and then fill data for graph.

All this is in same class:

private void showChart(CustomerTransactionModel ctm, String title) {

try {

if (ctm==null || ctm.getTransactionGroup()==null ||

ctm.getTransactionGroup().getTransactionTemplate()==null ||

ctm.getTransactionGroup().getTransactionTemplate().getAxisXProperty()==null ||

ctm.getTransactionGroup().getTransactionTemplate().getAxisYProperty()==null ||

ctm.getTransactionGroup().getTransactions()==null) {

return;

}

String xKey=ctm.getTransactionGroup().getTransactionTemplate().getAxisXProperty();

String yKey=ctm.getTransactionGroup().getTransactionTemplate().getAxisYProperty();

ArrayList<String[]> v = new ArrayList<String[]>();

for (Transaction tran : ctm.getTransactionGroup().getTransactions()) {

if (tran!=null && tran.getServiceData()!=null) {

Map<Object, Object> serviceData = tran.getServiceData();

if (serviceData.containsKey(xKey) && serviceData.containsKey(yKey)) {

String[] add=new String[2];

add[0]=serviceData.get(xKey).toString();

add[1]=serviceData.get(yKey).toString();

v.add(add);

}

}

}

if (!v.isEmpty()) {

openChartDialog(v, title, xKey, yKey);

}

} catch (Exception e) {

logger.error(e.getLocalizedMessage(), e);

}

}

private void openChartDialog(ArrayList<String[]> v, String title, String xLabel, String yLabel) {

try {

Object[] objects=new Object[4];

objects[0]=xLabel;

objects[1]=yLabel;

objects[2]=title;

objects[3]=v;

OpeningChartTask task = new OpeningChartTask();

task.doInBackground(objects);

} catch (Exception e) {

logger.error(e.getLocalizedMessage(), e);

}

}

private class OpeningChartTask extends AsyncTask<Object, Object, Object> {

@Override

protected Object doInBackground(Object... params) {

String xLabel=(String) params[0];

String yLabel=(String) params[1];

String title=(String) params[2];

ArrayList<String[]> v=(ArrayList<String[]>) params[3];

Intent intent = new Intent(getActivity(), ChartDialogActivity.class);

intent.putExtra("xLabel", xLabel);

intent.putExtra("yLabel", yLabel);

intent.putExtra("title", title);

intent.putExtra("values", v);

startActivityForResult(intent, 0);

return null;

}

}

So far so good. In on create, I read those data (they are set successfuly) and then call method openBarChart() that contains this:

private void openBarChart() {

// Create XY Series for X Series.

XYSeries xSeries = new XYSeries("X series");

// Adding data to the X Series.

for (int i = 0; i < _data.size(); i++) {

xSeries.add(Double.parseDouble(_data.get(i)[0]),

Double.parseDouble(_data.get(i)[1]));

}

// Create XYSeriesRenderer to customize XSeries

XYSeriesRenderer Xrenderer = new XYSeriesRenderer();

Xrenderer.setColor(Color.GREEN);

Xrenderer.setChartValuesTextSize(20);

Xrenderer.setPointStyle(PointStyle.DIAMOND);

Xrenderer.setDisplayChartValues(false);

Xrenderer.setLineWidth(2);

Xrenderer.setFillPoints(true);

// Create XYMultipleSeriesRenderer to customize the whole chart

final XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();

mRenderer.setChartTitle("X Vs Y Chart");

mRenderer.setXTitle("X Values");

mRenderer.setYTitle("Y Values");

mRenderer.setZoomButtonsVisible(false);

mRenderer.setXLabels(0);

mRenderer.setPanEnabled(true);

mRenderer.setAntialiasing(true);

mRenderer.setBackgroundColor(Color.DKGRAY);

mRenderer.setApplyBackgroundColor(true);

mRenderer.setBarWidth(20);

mRenderer.setScale((float) 0.3);

mRenderer.setShowGrid(true);

mRenderer.setClickEnabled(true);

for (int i = 0; i < _data.size(); i++) {

mRenderer.addXTextLabel(i, _data.get(i)[0]);

}

// Adding the XSeriesRenderer to the MultipleRenderer.

mRenderer.addSeriesRenderer(Xrenderer);

LinearLayout chart_container = (LinearLayout) findViewById(R.id.chartLayoutTest);

// Create a Dataset to hold the XSeries.

XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

// Add X series to the Dataset.

dataset.addSeries(xSeries);

// Creating an intent to plot line chart using dataset and

// multipleRenderer

final GraphicalView mChart;

mChart = (GraphicalView) ChartFactory.getBarChartView(this, dataset,

mRenderer, BarChart.Type.DEFAULT);

// Add the graphical view mChart object into the Linear layout .

chart_container.removeAllViews();

chart_container.addView(mChart);

if (mChart != null) {

mChart.repaint();

}

}

This method also survive but when it should be shown on screen it throws error and app die.

stacktrace is below:

E/ExceptionHandler( 8461): [uncaughtException] > ************ CAUSE OF ERROR ************

E/ExceptionHandler( 8461):

E/ExceptionHandler( 8461): java.lang.NullPointerException

E/ExceptionHandler( 8461): at java.util.TreeMap$BoundedMap.<init>(TreeMap.java:1115)

E/ExceptionHandler( 8461): at java.util.TreeMap.headMap(TreeMap.java:713)

E/ExceptionHandler( 8461): at org.achartengine.model.XYSeries.getRange(XYSeries.java:310)

E/ExceptionHandler( 8461): at org.achartengine.chart.XYChart.draw(XYChart.java:261)

E/ExceptionHandler( 8461): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:181)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13826)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13710)

E/ExceptionHandler( 8461): at android.view.ViewGroup.drawChild(ViewGroup.java:2988)

E/ExceptionHandler( 8461): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13829)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13710)

E/ExceptionHandler( 8461): at android.view.ViewGroup.drawChild(ViewGroup.java:2988)

E/ExceptionHandler( 8461): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13829)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13710)

E/ExceptionHandler( 8461): at android.view.ViewGroup.drawChild(ViewGroup.java:2988)

E/ExceptionHandler( 8461): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13708)

E/ExceptionHandler( 8461): at android.view.ViewGroup.drawChild(ViewGroup.java:2988)

E/ExceptionHandler( 8461): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13708)

E/ExceptionHandler( 8461): at android.view.ViewGroup.drawChild(ViewGroup.java:2988)

E/ExceptionHandler( 8461): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2858)

E/ExceptionHandler( 8461): at android.view.View.draw(View.java:13829)

E/ExceptionHandler( 8461): at android.widget.FrameLayout.draw(FrameLayout.java:467)

E/ExceptionHandler( 8461): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2458)

E/ExceptionHandler( 8461): at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2382)

E/ExceptionHandler( 8461): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2275)

E/ExceptionHandler( 8461): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2143)

E/ExceptionHandler( 8461): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1954)

E/ExceptionHandler( 8461): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1108)

E/ExceptionHandler( 8461): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4491)

E/ExceptionHandler( 8461): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:755)

E/ExceptionHandler( 8461): at android.view.Choreographer.doCallbacks(Choreographer.java:585)

E/ExceptionHandler( 8461): at android.view.Choreographer.doFrame(Choreographer.java:555)

E/ExceptionHandler( 8461): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:741)

E/ExceptionHandler( 8461): at android.os.Handler.handleCallback(Handler.java:615)

E/ExceptionHandler( 8461): at android.os.Handler.dispatchMessage(Handler.java:92)

E/ExceptionHandler( 8461): at android.os.Looper.loop(Looper.java:137)

E/ExceptionHandler( 8461): at android.app.ActivityThread.main(ActivityThread.java:4949)

E/ExceptionHandler( 8461): at java.lang.reflect.Method.invokeNative(Native Method)

E/ExceptionHandler( 8461): at java.lang.reflect.Method.invoke(Method.java:511)

E/ExceptionHandler( 8461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)

E/ExceptionHandler( 8461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)

E/ExceptionHandler( 8461): at dalvik.system.NativeStart.main(Native Method)

Any help would be much appreciated!

Oh and this, I did the work and checked content of those .java files and found couple of "tweaks" but I guess it's me that working something wrong here. If I make new project I can normally use this jar, the only difference is that previously I imported jar but now I'm calling this jar with maven pom file (I have my own repository but it should work ok anyway).

Thank you!

***SOLUTION FOUND***

In class org.achartengine.model.XYSeries I created simple Comparator and passed it to both TreeMap constructors. Then I changed constructor in org.achartengine.util.IndexXYMap to accept coparator and passed on to super(comparator).

I don't know exactly yet why this happened in one project and it didn't in previous so I'll leave question open, if anyone has explanation for this.

Thank you @keshav for your efforts.

分享给朋友:
您可能感兴趣的文章:
随机阅读: