Reputation: 17
I am trying to create a pie chart from the data stored in the database using MPAndroidChart. The size of the database is not fixed so it might have 2 to 7 data.
This is how I'm taking the data from the database.
myDb = new DatabaseHelper(this);
Cursor pie = myDb.showKharcha();
String[] xData = new String[pie.getCount()];
float[] yData = new float[pie.getCount()];
int i = 0;
float total = 0;
while (pie.moveToNext()){
String kname = pie.getString(1);
Float kh= pie.getFloat(2);
float kharcha = kh.floatValue();
total = total + kharcha ;
xData[i]=kname;
yData[i]=kharcha;
i++;
}
And this is the part of using the data for pie chart.
ArrayList<PieEntry> yEntry= new ArrayList<>();
ArrayList<String> xEntry = new ArrayList<>();
for( i =0;i < yData.length;i++){
yData[i] = yData[i]*100/total;
yEntry.add(new PieEntry(yData[i],i));
}
for(i =0;i < xData.length;i++){
xEntry.add(xData[i]);
}
The error I get is null pointer exception error and this to be exact
Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.PieChart.setRotationEnabled(boolean)' on a null object reference
The part containing setRotationEnabled(boolean) is as follows which is before calling the addDataSet method where everything I've provided takes place.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.piechart);
Log.d(TAG, "onCreate: starting to create chart");
pieChart.setRotationEnabled(true);
pieChart.setHoleRadius(25f);
pieChart.setTransparentCircleAlpha(0);
pieChart.setCenterText("Kharcha");
pieChart.setCenterTextSize(10);
addDataSet();}
I think I'm not being able to pass the float data to be used in a piechart. Any help would be appreciated.
Upvotes: 0
Views: 990
Reputation: 5339
you forgot to get the PieChart
from the layout
pieChart = (PieChart) findViewById(R.id.chart1)
;
Upvotes: 1