title: NullPointerException when drawing graph with the data retrieve from database
body: <p>the BrokenLineCusVisit class and BrokenLineCUsVisitView class are used to draw the line graph,Dashboardfragment is the place that i want generate graph. Well when i run the code, the following error occur:
  java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference.
  on this part:</p>
  <pre><code>for (int i = 0; i &lt; mdata.size(); i++) 
  </code></pre>
  <p>which means that &quot;mdata&quot; is null, but i tried run the retrieving data part seperately, it was working fine. So is it because i didnt handle sync problem correctly?(plz ignore the chinese comment in my code)</p>
  <pre><code>public class BrokenLineCusVisit {
      private String date;
      private int data;
  
      public BrokenLineCusVisit(String date, int data) {
          this.date = date;
          this.data = data;
      }
  
      public String getDate() {
          return date;
      }
  
      public void setDate(String date) {
          this.date = date;
      }
  
      public int getData() {
          return data;
      }
  
      public void setData(int data) {
          this.data = data;
      }
  }
  
  
  public class BrokenLineCusVisitView extends View {
      private int width;
      private int heigh;
      public enum LockObj{
          INSTANCE;
      }
  
      //网格的宽度与高度
      private int gridspace_width;
      private int gridspace_heigh;
      //底部空白的高度
      private int brokenline_bottom;
  
  
      //灰色背景的画笔
      private Paint mPaint_bg;
      //灰色网格的画笔
      private Paint mPaint_gridline;
      //文本数据的画笔
      private Paint mPaint_text;
  
      //折线圆点的蓝色背景
      private Paint mPaint_point_bg;
      //折线圆点的白色表面
      private Paint mPaint_point_sur;
      //阴影路径的画笔
      private Paint mPaint_path;
      //折线的画笔
      private Paint mPaint_brokenline;
      //路径
      private Path mpath=new Path();
      //客户拜访的折线（BrokenLineCusVisit）数据
      private List&lt;BrokenLineCusVisit&gt; mdata;
  
      public BrokenLineCusVisitView(Context context) {
          super(context);
      }
  
      public BrokenLineCusVisitView(Context context, AttributeSet attrs) {
          super(context, attrs);
          inite(context);
      }
  
      private void inite(Context context) {
  
          mPaint_bg=new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint_bg.setColor(Color.argb(0xff,0xef,0xef,0xef));
  
          mPaint_gridline=new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint_gridline.setColor(Color.argb(0xff,0xce,0xCB,0xce));
  
          mPaint_brokenline=new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint_brokenline.setColor(Color.argb(0xff,0x91,0xC8,0xD6));
          mPaint_brokenline.setTextSize(18);
          mPaint_brokenline.setTextAlign(Paint.Align.CENTER);
  
          mPaint_point_bg=new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint_point_bg.setColor(Color.argb(0xff, 0x91, 0xC8, 0xD6));
          //注意path的画笔的透明度已经改变了
          mPaint_path=new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint_path.setColor(Color.argb(0x33,0x91,0xC8,0xD6));
  
  
          mPaint_point_sur=new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint_point_sur.setColor(Color.WHITE);
  
  
          mPaint_text=new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint_text.setColor(Color.BLACK);
          mPaint_text.setTextAlign(Paint.Align.CENTER);
  
          invalidate();
      }
      //data的set/get方法，用于设置数据
      public List&lt;BrokenLineCusVisit&gt; getMdata() {
          return mdata;
      }
  
      public void setMdata(List&lt;BrokenLineCusVisit&gt; mdata) {
          this.mdata = mdata;
          requestLayout();
          invalidate();
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          synchronized (LockObj.INSTANCE) {
  
              super.onDraw(canvas);
              //绘制白色背景
              canvas.drawColor(Color.WHITE);
              //绘制灰色矩形区域
              canvas.drawRect(10, 0, width, heigh - brokenline_bottom, mPaint_bg);
              for (int j = 0; j &lt; 4; j++) {
                  canvas.drawLine(10, gridspace_heigh * (j + 1), width, gridspace_heigh * (j + 1), mPaint_gridline);
              }
  
  
              for (int i = 0; i &lt; mdata.size(); i++) {
                  if (i == 0) {
                      mpath.moveTo(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100);
                  }
                  canvas.drawLine(gridspace_width * i + 10, 0, gridspace_width * i + 10, heigh - brokenline_bottom, mPaint_gridline);
  
                  if (i != mdata.size() - 1) {
                      canvas.drawLine(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100, gridspace_width * (i + 1) + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i + 1).getData() / 100, mPaint_brokenline);
                      mpath.quadTo(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100, gridspace_width * (i + 1) + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i + 1).getData() / 100);
                  }
                  canvas.drawCircle(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100, 10, mPaint_point_bg);
                  canvas.drawCircle(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100, 5, mPaint_point_sur);
                  String data = mdata.get(i).getData() + &quot;&quot;;
                  canvas.drawText(data, gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100 - mPaint_brokenline.measureText(data), mPaint_brokenline);
                  String date = mdata.get(i).getDate();
                  canvas.drawText(date, gridspace_width * i + 10, heigh - brokenline_bottom / 2, mPaint_text);
                  if (i == mdata.size() - 1) {
                      mpath.quadTo(gridspace_width * i + 10, heigh - brokenline_bottom - (heigh - brokenline_bottom) * mdata.get(i).getData() / 100, gridspace_width * i + 10, heigh - brokenline_bottom);
                      mpath.quadTo(gridspace_width * i + 10, heigh - brokenline_bottom, 10, heigh - brokenline_bottom);
                      mpath.close();
                  }
              }
              canvas.drawPath(mpath, mPaint_path);
          }
      }
  
  public class DashboardFragment extends Fragment {
  
  private DashboardViewModel dashboardViewModel;
  private FragmentDashboardBinding binding;
  
  private Spinner spinner;
  private List&lt;BrokenLineCusVisit&gt; mdata=new ArrayList&lt;&gt;();
  private BrokenLineCusVisitView brokenline;
  private float data;
  private String UserID;
  
  
  
  public View onCreateView(@NonNull LayoutInflater inflater,
                           ViewGroup container, Bundle savedInstanceState) {
      synchronized (BrokenLineCusVisitView.LockObj.INSTANCE) {
          dashboardViewModel =
                  new ViewModelProvider(this).get(DashboardViewModel.class);
  
          binding = FragmentDashboardBinding.inflate(inflater, container, false);
          View root = binding.getRoot();
  
  
          final TextView textView = binding.textDashboard;
          dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer&lt;String&gt;() {
              @Override
              public void onChanged(@Nullable String s) {
                  textView.setText(s);
              }
          });
  
          spinner = (Spinner) root.findViewById(R.id.spinner);
          brokenline = (BrokenLineCusVisitView) root.findViewById(R.id.brokenline);
          FirebaseAuth auth = FirebaseAuth.getInstance();
          UserID = auth.getCurrentUser().getUid();
          FirebaseDatabase rootNode = FirebaseDatabase.getInstance(&quot;https://comfy-aa480-default-rtdb.asia-southeast1.firebasedatabase.app/&quot;);
          DatabaseReference reference = rootNode.getReference(&quot;Users&quot;);
          spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  String s = parent.getItemAtPosition(position).toString();
                  if (s.equals(&quot;Oct&quot;)) {
                      for (int i = 15; i &lt; 16; i++) {
                          int finalI = 15;
                          reference.child(UserID).child(&quot;10&quot;).child(String.valueOf(i)).child(&quot;AM&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;DataSnapshot&gt;() {
                              @Override
                              public void onComplete(@NonNull @NotNull Task&lt;DataSnapshot&gt; task) {
                                  if (!task.isSuccessful()) {
                                      task.getException();
                                      BrokenLineCusVisit brokenline = new BrokenLineCusVisit(finalI + &quot;&quot;, 0);
                                      mdata.add(brokenline);
  
  
                                  } else {
                                      int num = Integer.parseInt(task.getResult().getValue().toString());
                                      BrokenLineCusVisit brokenline = new BrokenLineCusVisit(finalI + &quot;&quot;, num);
                                      mdata.add(brokenline);
                                  }
                                  brokenline.setMdata(mdata);
                              }
                          });
  
  
                      }
  
                  }
  
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
                  //                for ( int i=1;i &lt;31 ; i++){
                  //                    int finalI = i;
                  //                    reference.child(UserID).child(&quot;10&quot;).child(String.valueOf(i)).child(&quot;AM&quot;).get().addOnCompleteListener(new OnCompleteListener&lt;DataSnapshot&gt;() {
                  //                        @Override
                  //                        public void onComplete(@NonNull @NotNull Task&lt;DataSnapshot&gt; task) {
                  //                            if(!task.isSuccessful()){
                  //                                task.getException();
                  //                                BrokenLineCusVisit brokenline=new BrokenLineCusVisit(finalI +&quot;&quot;,0);
                  //                                mdata.add(brokenline);
                  //
                  //
                  //                            }
                  //                            else{
                  //                                int num= (int) task.getResult().getValue();
                  //                                BrokenLineCusVisit brokenline=new BrokenLineCusVisit(finalI+&quot;&quot;,num);
                  //                                mdata.add(brokenline);
                  //                            }
                  //                        }
                  //                    });
                  //
                  //
                  //                }
                  //                System.out.println(mdata);
  
              }
          });
  
          //
          //        for (int i = 0; i &lt;30 ; i++) {
          //            BrokenLineCusVisit brokenline=new BrokenLineCusVisit(i+&quot;&quot;,1);
          //            mdata.add(brokenline);
          //        }
          //        brokenline.setMdata(mdata);
          //        System.out.println(mdata);
          //        System.out.println(brokenline);
  
          return root;
      }
  }
  
  @Override
  public void onDestroyView() {
      super.onDestroyView();
      binding = null;
  }
  </code></pre>
  <p>}</p>