MPAndroidChart Null object referance

1.3k views Asked by At

I am having null object reference in displaying MPAndroidChart in a adapter view if any one can assist , I am using a separate Adapter because of using different views in one layout,

Here is the adapter :

public class VerticalListAdapter extends RecyclerView.Adapter<VerticalListAdapter.ViewHolder> {

private Activity activity;

BarChart chart ;
ArrayList<BarEntry> BARENTRY = new ArrayList<BarEntry>();
ArrayList<String> BarEntryLabels = new ArrayList<String>(); ;
BarDataSet Bardataset ;
BarData BARDATA ;

public VerticalListAdapter(Activity activity) {
    this.activity = activity;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = activity.getLayoutInflater();
    View view = inflater.inflate(R.layout.item_recycler_view, parent, false);
    BARENTRY = new ArrayList<BarEntry>();
    BarEntryLabels = new ArrayList<String>();

    AddValuesToBARENTRY();
    AddValuesToBarEntryLabels();

    Bardataset = new BarDataSet(BARENTRY, "Projects");

    BARDATA = new BarData(BarEntryLabels, Bardataset);

    Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
    chart.setData(BARDATA);
    chart.animateY(3000);
    return new ViewHolder(view);
}

private void AddValuesToBarEntryLabels() {

    BarEntryLabels.add("January");
    BarEntryLabels.add("February");
    BarEntryLabels.add("March");
    BarEntryLabels.add("April");
    BarEntryLabels.add("May");
    BarEntryLabels.add("June");

}

private void AddValuesToBARENTRY() {
    BARENTRY.add(new BarEntry(2f, 0));
    BARENTRY.add(new BarEntry(4f, 1));
    BARENTRY.add(new BarEntry(6f, 2));
    BARENTRY.add(new BarEntry(8f, 3));
    BARENTRY.add(new BarEntry(7f, 4));
    BARENTRY.add(new BarEntry(3f, 5));

}


@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    viewHolder.BarChart.setData(BARDATA);

}

@Override
public int getItemCount() {
    return 1;
}

/**
 * View holder to display each RecylerView item
 */
protected class ViewHolder extends RecyclerView.ViewHolder {

    BarChart BarChart;

    public ViewHolder(View view) {
        super(view);
        BarChart = (BarChart) view.findViewById(R.id.chart1);

    }

}

}

And XML

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">

    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/chart1"
        android:layout_width="match_parent"
        android:layout_height="200dp" />

And Here is LogCaT:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.BarChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
                                                                                at info.devexchanges.recyclerview.VerticalListAdapter.onCreateViewHolder(VerticalListAdapter.java:32)
                                                                                at info.devexchanges.recyclerview.VerticalListAdapter.onCreateViewHolder(VerticalListAdapter.java:18)
                                                                                at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5482)
                                                                                at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4707)
                                                                                at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
                                                                                at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
                                                                                at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
                                                                                at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
                                                                                at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
                                                                                at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
                                                                                at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2625)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1283)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                at android.widget.ScrollView.onMeasure(ScrollView.java:340)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                                at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                                at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                                at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                                at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
                                                                                at android.view.View.measure(View.java:18788)
                                                                                at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                                                at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                                at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
2

There are 2 answers

0
Mohtashim On

I have solve the issue and wanted to post the code , I was having null reference because I have not initiate layout in onCreateViewHolder , after adding this two lines in my layout shows graph view inside a card view (graph is using in a list view having dynamic view types)

BarChart chart;
chart = (BarChart) view.findViewById(R.id.chart1);

in onCreateViewHolder

0
Zafar Hussain On
  CombinedData data = new CombinedData();

switch(data)
{
// Only for BarChart 
case BarChart
   LineData ld = new LineData();
        ld.clearValues();
        chartData.setData(ld);
        chartData.setData(barData);
break;

// Only for LineChart 
case LineChart
    BarData bd = new BarData();
        bd.clearValues();
        chartData.setData(bd);
        chartData.setData(lineData);
break;

// Both the Chart (Line & Bar Chart)
case BothChart 
 chartData.setData(lineData);
 chartData.setData(barData);
}