0

I know its may be silly to ask but i could not found and relevant search result to meet my requirement, when i am hit the bar chart its getting color perfectly but after second time bar chart color given null pointer exception is there any solution for that, i am using bar chart my code is:

  ArrayList<Integer> colors = new ArrayList<Integer>();
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_blue_bright));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_purple));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_green_dark));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_red_dark));
            colors.add(ContextCompat.getColor(getActivity(),android.R.color.holo_orange_light));

            set1.setColors(colors);
            mChart.setExtraBottomOffset(0);
            BarData data = new BarData(dataSets);
            data.setBarWidth(0.9f);
            data.setValueFormatter(new MyValueFormatter());
            XAxis xAxis =  mChart.getXAxis();
            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
            xAxis.setLabelRotationAngle(30);
            xAxis.setSpaceMax(5f);
            xAxis.setGranularity(1f);
            xAxis.setDrawGridLines(false);
            xAxis.setGranularityEnabled(true);
            Legend l= mChart.getLegend();
            LegendEntry l1=new LegendEntry("Activities", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_blue_bright));
            LegendEntry l2=new LegendEntry("ArmedActivated", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_purple));
            LegendEntry l3=new LegendEntry("Theft Averted", Legend.LegendForm.DEFAULT,10f,1f,null,ContextCompat.getColor(getContext(),android.R.color.holo_green_dark));
            LegendEntry l4=new LegendEntry("Arrest", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_red_dark));
            LegendEntry l5=new LegendEntry("Missed", Legend.LegendForm.DEFAULT,10f,2f,null, ContextCompat.getColor(getContext(),android.R.color.holo_orange_light));
            mChart.setFitBars(true);
            l.setEnabled(true);
            l.setWordWrapEnabled(true);
            l.setCustom(new LegendEntry[]{l1,l2,l3,l4,l5});
            mChart.setData(data);
            mChart.invalidate();

And my Log is:

03-14 16:33:47.407 26479-26479/com.ivis.live E/AndroidRuntime: FATAL EXCEPTION: main
                                                           Process: com.ivis.live, PID: 26479
                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.getColor(int)' on a null object reference
                                                               at android.support.v4.content.ContextCompat.getColor(ContextCompat.java:409)
                                                               at com.ivigil.ivis.fragments.CurrentFragment$1.onResponse(CurrentFragment.java:285)
                                                               at com.ivigil.ivis.fragments.CurrentFragment$1.onResponse(CurrentFragment.java:222)
                                                               at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:83)
                                                               at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
                                                               at android.os.Handler.handleCallback(Handler.java:789)
                                                               at android.os.Handler.dispatchMessage(Handler.java:98)
                                                               at android.os.Looper.loop(Looper.java:164)
                                                               at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Edit: here i come to know the main issue is getActivity only can any one give me solution for that in my case.

ashish
  • 307
  • 4
  • 15

0 Answers0