java - How to solve this error - i want create MPAndroidChart dynamic multiple line -


i tried mpandroidchart dynamic multiple line

private linechart mchart;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mchart = (linechart) findviewbyid(r.id.chart1);     mchart.setdrawgridbackground(false);      mchart.setdescription("");     mchart.setnodatatextdescription("you need provide data chart.");      mchart.settouchenabled(true);      mchart.setdragenabled(true);     mchart.setscaleenabled(true);      mchart.setpinchzoom(true);      limitline llxaxis = new limitline(10f, "index 10");     llxaxis.setlinewidth(4f);     llxaxis.enabledashedline(10f, 10f, 0f);     llxaxis.setlabelposition(limitline.limitlabelposition.right_bottom);     llxaxis.settextsize(10f);      xaxis xaxis = mchart.getxaxis();     xaxis.enablegriddashedline(10f, 10f, 0f);      yaxis leftaxis = mchart.getaxisleft();     leftaxis.removealllimitlines();     leftaxis.enablegriddashedline(10f, 10f, 0f);     leftaxis.setdrawzeroline(false);      leftaxis.setdrawlimitlinesbehinddata(true);      mchart.getaxisright().setenabled(false);     float[] linedata_0 = {37500, 38000, 38500, 39000, 39500, 40000, 40500};     float[] linedata_1 = {3000, 3500, 3800, 3600, 3400, 2000, 2800};     float[] linedata_2 = {0, 0, 6000, 25000, 24800, 24750, 5000};     float[] linedata_3 = {0, 6500, 11000, 12500, 13000, 14000, 2000};     float[] linedata_4 = {0, 9000, 12400, 12450, 9300, 8000, 6000};      float[][] all_line_data = {linedata_0, linedata_1, linedata_2, linedata_3, linedata_4};      arraylist<ilinedataset> datasets = null;      arraylist<entry>[] values = (arraylist<entry>[]) new arraylist[all_line_data.length];      (int j = 0; j < all_line_data.length; j++) {         (int = 0; < all_line_data[j].length; i++) {             values[j].add(new entry(all_line_data[j][i], i));         }          linedataset[] set0 = new linedataset[j];         set0[j].enabledashedline(10f, 5f, 0f);         set0[j].enabledashedhighlightline(10f, 5f, 0f);         set0[j].setcolor(color.black);         set0[j].setcirclecolor(color.black);         set0[j].setlinewidth(1f);         set0[j].setcircleradius(3f);         set0[j].setdrawcirclehole(false);         set0[j].setvaluetextsize(9f);         set0[j].setdrawfilled(true);          datasets = new arraylist<ilinedataset>();         datasets.add(set0[j]);     }     linedata data = new linedata(datasets);     mchart.setdata(data); } 

i getting following error....

  • caused by: java.lang.nullpointerexception: attempt invoke virtual method 'boolean java.util.arraylist.add(java.lang.object)' on null object reference

i solved problem

    linechart mchart = (linechart) findviewbyid(r.id.chartline);     mchart.setdrawgridbackground(true);      // no description text     mchart.setdescription("");     mchart.setnodatatextdescription("you need provide data chart.");      // enable touch gestures     mchart.settouchenabled(true);      // enable scaling , dragging     mchart.setdragenabled(true);     mchart.setscaleenabled(true);      // if disabled, scaling can done on x- , y-axis separately     mchart.setpinchzoom(true);      custommarkerview mv = new custommarkerview (mainactivity.this, r.layout.line_graph_marker_view, colors);     mchart.setmarkerview(mv);      // x-axis limit line     limitline llxaxis = new limitline(10f, "index 10");     llxaxis.setlinewidth(4f);     llxaxis.setlabelposition(limitline.limitlabelposition.right_bottom);     llxaxis.settextsize(10f);      xaxis xaxis = mchart.getxaxis();     xaxis.setposition(xaxis.xaxisposition.bottom);      yaxis leftaxis = mchart.getaxisleft();     leftaxis.removealllimitlines(); // reset limit lines avoid overlapping lines     leftaxis.setaxisminvalue(0);     leftaxis.setdrawzeroline(true);      // limit lines drawn behind data (and not on top)     leftaxis.setdrawlimitlinesbehinddata(true);      mchart.getaxisright().setenabled(false);      float[] linedata_0 = {37500, 38000, 38500, 39000, 39500, 40000, 40500};     float[] linedata_1 = {3000, 3500, 3800, 3600, 3400, 2000, 2800};     float[] linedata_2 = {0, 0, 6000, 25000, 24800, 24750, 5000};     float[] linedata_3 = {0, 6500, 11000, 12500, 13000, 14000, 2000};     float[] linedata_4 = {0, 9000, 12400, 12450, 9300, 8000, 6000};      float[][] all_line_data = {linedata_0, linedata_1, linedata_2, linedata_3, linedata_4};       int[] graph_linear_background = {r.drawable.graph_background_0, r.drawable.graph_background_1, r.drawable.graph_background_2, r.drawable.graph_background_3, r.drawable.graph_background_4};     int[] graph_linear_point_color = {color.rgb(100, 199, 255), color.rgb(109, 220, 124), color.rgb(236, 91, 102), color.rgb(184, 85, 240), color.rgb(70, 119, 191)};      list<arraylist<entry>> values_all = new arraylist<arraylist<entry>>();     (int k = 0; k < all_line_data.length; k++) {         arraylist<entry> list = new arraylist<>();         (int = 0; < all_line_data[k].length; i++) {             list.add(new entry(i, all_line_data[k][i]));         }         values_all.add(list);     }      arraylist<ilinedataset> datasets = new arraylist<ilinedataset>();      string[] arr = new string[]{"a", "b", "c", "d", "e", "f"};     map<string, linedataset> map = new hashmap<>();     for(int = 0; < all_line_data.length; i++) {          map.put("set0" + i, new linedataset(values_all.get(i), arr[i]));          // set line drawn "- - - - - -"         map.get("set0" + i).setcolor(graph_linear_point_color[i]);         map.get("set0" + i).setcirclecolor(graph_linear_point_color[i]);         map.get("set0" + i).setlinewidth(3f);         map.get("set0" + i).setdrawvalues(false);         map.get("set0" + i).setcircleradius(4f);         map.get("set0" + i).setdrawcirclehole(false);         map.get("set0" + i).setdrawfilled(true);          datasets.add(map.get("set0" + i));          if (utils.getsdkint() >= 18) {             // fill drawable supported on api level 18 , above             drawable drawable = contextcompat.getdrawable(this, graph_linear_background[i]);             map.get("set0" + i).setfilldrawable(drawable);         } else {             map.get("set0" + i).setfillcolor(color.transparent);         }     }     // create data object datasets     linedata line_data = new linedata(datasets);     // set data     mchart.setdata(line_data); 

output is....

enter image description here


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -