title: How to draw a bitmap from AChartEngine GraphicalView
body: <p>I have an app that receives in real time messages from a range sensor and I want to update a chart with the values received.
  I decided to use a <code>GraphicalView</code> from the library <code>AChartEngine</code>.
  I thought to update the <code>Graphicalview</code> every time that I receive a message then to convert it in a <code>BitMap</code> with the <code>toBitmap()</code> function and then to draw the bitmap on a canvas.</p>
  
  <p>I'm trying to see if I can do this (so I'm not using messages yet but only trying to draw a bitmap from  a graphical view) but when I run my code I get a <code>Nullpointer exception</code>. This is my code:</p>
  
  <pre><code>public class DrawChartView extends View {
  Paint paint = new Paint();
  Rect rect = new Rect(130, 130, 80, 80);
  Context context;
  XYSeries currentDataset = new XYSeries("range data series");
  XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
  XYSeriesRenderer currentRenderer = new XYSeriesRenderer();
  XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
  LineChart lc;
  GraphicalView gv;
  Bitmap bm;
  
  public DrawChartView(Context context_) {
      super(context_);
      context = context_;
      //imposto i parametri del renderer
      mRenderer.setApplyBackgroundColor(true);
      mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
      mRenderer.setAxisTitleTextSize(16);
      mRenderer.setChartTitleTextSize(20);
      mRenderer.setLabelsTextSize(15);
      mRenderer.setLegendTextSize(15);
      mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
      mRenderer.setZoomButtonsVisible(true);
      mRenderer.setPointSize(10);
      mRenderer.addSeriesRenderer(currentRenderer);
      //imposto il dataset
      currentDataset.add(1,1);
      currentDataset.add(2,2);
      currentDataset.add(3,3);
      mDataset.addSeries(currentDataset);
      //inizializzo il line chart
      lc = new LineChart(mDataset, mRenderer);
      //inizializzo la grahical view
      gv = new GraphicalView(context, lc);
      bm=gv.toBitmap();
  }
  
  public DrawChartView(Context context_, AttributeSet attrs) {
      super(context_, attrs);
      // TODO Auto-generated constructor stub
  }
  
  public DrawChartView(Context context_, AttributeSet attrs, int defStyle) {
      super(context_, attrs, defStyle);
      // TODO Auto-generated constructor stub
  }
  @Override
  public void onDraw(Canvas canvas) {
      paint.setColor(Color.BLACK);
      paint.setStrokeWidth(3);
      //canvas.drawRect(rect, paint);
     // canvas.drawBitmap(bm,new Rect(0,0,100,100),rect,null);
  
  }
  
  public void setRect(int h, int b)
  {
      rect.set(130,130,30+b,30+h);
  
  }
  
  public void setChart(XYSeries xys)
  {
      mDataset.addSeries(xys);
      lc = new LineChart(mDataset, mRenderer);
      gv= new GraphicalView(context, lc);
      bm = gv.toBitmap();
  
  }
  </code></pre>
  
  <p>The activity:</p>
  
  <pre><code>public class FragActivity extends Activity {
  
      Canvas canvas = new Canvas();   
      XYSeries currentSeries = new XYSeries("range data series1");
      DrawChartView dcv/* = new DrawChartView(this)*/;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_frag);
  
  
          dcv = (DrawChartView)findViewById(R.id.SurfaceView01);
          dcv.onDraw(canvas);
          currentSeries.add(11, 11);
          dcv.setChart(currentSeries);
          dcv.onDraw(canvas);
      }
  
  
      }
  </code></pre>
  
  <p>And the layout</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      &gt;
  &lt;com.cami.robot_chooser.DrawChartView 
      android:id="@+id/SurfaceView01"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      /&gt;
  
  &lt;TextView
      android:id="@+id/show_message1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:text="@string/hello_world"
      android:layout_weight="1"
      /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Can you help to figure what's wrong and how to fix the problem?</p>
  
  <p>LogCat said:</p>
  
  <pre><code>    10-22 12:52:06.115: I/Choreographer(3061): Skipped 71 frames!  The application may be doing too much work on its main thread.
  10-22 12:52:06.155: D/gralloc_goldfish(3061): Emulator without GPU emulation detected.
  10-22 12:52:08.406: I/Choreographer(3061): Skipped 34 frames!  The application may be doing too much work on its main thread.
  10-22 12:52:08.581: D/AndroidRuntime(3061): Shutting down VM
  10-22 12:52:08.581: W/dalvikvm(3061): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
  10-22 12:52:08.675: E/AndroidRuntime(3061): FATAL EXCEPTION: main
  10-22 12:52:08.675: E/AndroidRuntime(3061): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.cami.robot_chooser/com.cami.robot_chooser.FragActivity}: java.lang.NullPointerException
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.os.Handler.dispatchMessage(Handler.java:99)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.os.Looper.loop(Looper.java:137)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.main(ActivityThread.java:4745)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.reflect.Method.invokeNative(Native Method)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.reflect.Method.invoke(Method.java:511)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at dalvik.system.NativeStart.main(Native Method)
  10-22 12:52:08.675: E/AndroidRuntime(3061): Caused by: java.lang.NullPointerException
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.view.View.&lt;init&gt;(View.java:3228)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.cami.robot_chooser.DrawChartView.&lt;init&gt;(DrawChartView.java:36)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at com.cami.robot_chooser.FragActivity.&lt;init&gt;(FragActivity.java:41)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.Class.newInstanceImpl(Native Method)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at java.lang.Class.newInstance(Class.java:1319)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
  10-22 12:52:08.675: E/AndroidRuntime(3061):     ... 11 more
  10-22 12:52:13.545: I/Process(3061): Sending signal. PID: 3061 SIG: 9
  </code></pre>