Null Pointer Exception occuring during graph generation
Friends.. i am trying to drawing some graph . My application is running fine for first attempt but when i am trying to draw the graph repeatedly for 2,3 attempts then it is showing Null Pointer exception...What will be the solution? Here is my code





public class ECG extends Activity {
	
/** Called when the activity is first created. */
	
Context cnt;

Callback c;
	
Camera mCamera;
	
boolean mFinished;
	
DrawMyGraph g;
	
ImageView clock,graph_Text,heart_Index,graph_View;
	
static long startTime = 0;
	
static int beats = 0;
	
volatile boolean show=false;
	
SurfaceHolder sh;
	
SurfaceView sv;
	
TextView show_BPM;
	
Button on,off;
	
int no,count=0;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		show_BPM=(TextView)findViewById(R.id.showBPM);
		sv=(SurfaceView)findViewById(R.id.surfaceView1);
		clock=(ImageView)findViewById(R.id.clock);
		graph_Text=(ImageView)findViewById(R.id.graphText);
		heart_Index=(ImageView)findViewById(R.id.heartIndex);
		graph_View=(ImageView)findViewById(R.id.graphview);
		graph_Text.setBackgroundResource(R.drawable.graphtext2);
		on=(Button)findViewById(R.id.on);
		off=(Button)findViewById(R.id.off);
 
		Random r=new Random();
		 no=r.nextInt(84-60)+60;
		
		Context context = getApplicationContext();
		CharSequence text = "cover the camera with your index finger......!";
		int duration = 100000;

		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
		///////////////////////////////
		////////////////////////////
		
		
		
		g = new DrawMyGraph(this, 26);
		addContentView(g, new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		startCamPreview();
		
				
		
	}

	public void startCamPreview() {
		SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView1);
		SurfaceHolder sh = sv.getHolder();
		mCamera = Camera.open();
		
		try {
			mCamera.setPreviewDisplay(sh);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sh.addCallback(new Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				System.out.println("Deleted");
				mCamera.setPreviewCallback(null);
				mCamera.stopPreview();
				mCamera.release();
				mCamera = null;
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				System.out.println("Created");

				mCamera.setPreviewCallback(new PreviewCallback() {
					public void onPreviewFrame(byte[] data, Camera camera) {
						
						Camera.Parameters params = camera.getParameters();
			
						int width = params.getPreviewSize().width;
						int height = params.getPreviewSize().height;
						byte[] mYUVData = new byte[data.length];
						int[] mRGBData = new int[width * height];
						System.arraycopy(data, 0, mYUVData, 0, data.length);
						decodeYUV420SP(mRGBData, mYUVData, width, height);
						int[] mRedHistogram = new int[256];
						calculateIntensityHistogram(mRGBData, mRedHistogram,
								width, height, 0);
						double imageRedMean = 0;
						double redHistogramSum = 0;
						for (int bin = 0; bin < 256; bin++) {
							imageRedMean += mRedHistogram[bin] * bin;
							redHistogramSum += mRedHistogram[bin];
						} // bin
						imageRedMean /= redHistogramSum;
						double[] mBinSquared;
						mBinSquared = new double[256];
						for (int bin = 0; bin < 256; bin++) {
							mBinSquared[bin] = ((double) bin) * bin;
						}
						double imageRed2ndMoment = 0;
						for (int bin = 0; bin < 256; bin++) {
							imageRed2ndMoment += mRedHistogram[bin]
									* mBinSquared[bin];
						} // bin
						imageRed2ndMoment /= redHistogramSum;
						double imageRedStdDev = Math.sqrt((imageRed2ndMoment
								- imageRedMean * imageRedMean));
						int val = (new Double(imageRedStdDev * 100000)
								.intValue());
						
						show_BPM.setText(String.valueOf(no));
						int v = new Double(imageRedStdDev).intValue();
						
						for (int l = 0; l < 1; l++) {
							
						}
						int yVal=250;
						if (imageRedStdDev < 15 && val != 0)
						{
							show=true;
							g.addPoint(yVal);
							g.addPoint(yVal);
							float flt=(float)Math.random();
							g.addPoint(yVal+flt*25);
							g.addPoint(yVal);
							g.addPoint(yVal);
							flt=(float)Math.random();
							g.addPoint(yVal-flt*20);
							g.addPoint(yVal);
							g.addPoint(yVal);
							flt=(float)Math.random();
							g.addPoint(yVal-flt*30);
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							flt=(float)Math.random();
							g.addPoint(yVal+flt*35);
							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
							
						} else {
							show=false;
							if(val==0){
								
								count++;
								g.addPoint(yVal);
								g.addPoint(yVal);
								g.addPoint(yVal);
								g.addPoint(yVal);
								show_BPM.setText(String.valueOf(0));
							}else{
							
							count++;
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							show_BPM.setText(String.valueOf(0));
							
							
							}
						}
					}

				});
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
				System.out.println("Changed");
				Camera.Parameters parameters = mCamera.getParameters();
				parameters.setPreviewSize(320, 240);
				parameters.setPreviewFrameRate(15);
				parameters.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);
				parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
				mCamera.setParameters(parameters);
				mCamera.startPreview();
				g.invalidate();
			}
		});
		// fl.addView(b);
	}

	static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width,
			int height) {
		final int frameSize = width * height;

		for (int j = 0, yp = 0; j < height; j++) {
			int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
			for (int i = 0; i < width; i++, yp++) {
				int y = (0xff & ((int) yuv420sp[yp])) - 16;
				if (y < 0)
					y = 0;
				if ((i & 1) == 0) {
					v = (0xff & yuv420sp[uvp++]) - 128;
					u = (0xff & yuv420sp[uvp++]) - 128;
				}

				int y1192 = 1192 * y;
				int r = (y1192 + 1634 * v);
				int g = (y1192 - 833 * v - 400 * u);
				int b = (y1192 + 2066 * u);

				if (r < 0)
					r = 0;
				else if (r > 262143)
					r = 262143;
				if (g < 0)
					g = 0;
				else if (g > 262143)
					g = 262143;
				if (b < 0)
					b = 0;
				else if (b > 262143)
					b = 262143;

				rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
						| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
			}
		}
	}

	static public void calculateIntensityHistogram(int[] rgb, int[] histogram,
			int width, int height, int component) {
		for (int bin = 0; bin < 256; bin++) {
			histogram[bin] = 0;
		} // bin
		if (component == 0) // red
		{
			for (int pix = 0; pix < width * height; pix += 3) {
				int pixVal = (rgb[pix] >> 16) & 0xff;
				histogram[pixVal]++;
			} // pix
		} else if (component == 1) // green
		{
			for (int pix = 0; pix < width * height; pix += 3) {
				int pixVal = (rgb[pix] >> 8) & 0xff;
				histogram[pixVal]++;
			} // pix
		} else // blue
		{
			for (int pix = 0; pix < width * height; pix += 3) {
				int pixVal = rgb[pix] & 0xff;
				histogram[pixVal]++;
			} // pix
		
			
		}
	}

	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		clock.setBackgroundResource(R.anim.clockanimation);
		AnimationDrawable clock_Anim=new AnimationDrawable();
		clock_Anim=(AnimationDrawable)clock.getBackground();
		clock_Anim.start();
		timer.start();
		}
	
	CountDownTimer timer=new CountDownTimer(30000,1000){

		@Override
		public void onFinish()throws NullPointerException {
			// TODO Auto-generated method stub
			mCamera.stopPreview();**//This is the line for which shows NullPointer exception**
			g.invalidate();
			Canvas.freeGlCaches();
			graph_Text.setBackgroundResource(R.drawable.graphtext3);
			if(count>0)
			{
				System.out.println("Hiiiiiii");
			show_BPM.setText(String.valueOf(0));
			}
			else
			{
				System.out.println("Helllooo");
				show_BPM.setText(String.valueOf(no));
			}
		}

		@Override
		public void onTick(long arg0) {
			// TODO Auto-generated method stub
			
		}
		
	};

		class DrawMyGraph extends View {
		public LinkedList<Float> yValues = null;
		private int size = 0;
		private LinkedList<Integer> xValues = null;
		private LinkedList<Long> beatList = new LinkedList<Long>();

		public DrawMyGraph(Context context, int size) {
			super(context);
			yValues = new LinkedList<Float>();
			int inc = 5;
			this.size = size * inc;
			xValues = new LinkedList<Integer>();

			int cnt = 468;
			for (int i = 0; i < this.size * inc; i++) {
				xValues.add(cnt);
				cnt = cnt - (inc - 2);
			}
		}

		@Override
		protected void onDraw(Canvas canvas) {
			Bitmap bitmap=Bitmap.createBitmap(2	, 2, Bitmap.Config.RGB_565);
			graph_View.setImageBitmap(bitmap);
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(2);
			paint.setColor(Color.RED);
			invalidate();
			for (int i = 0, j = 1; i < yValues.size() && j < yValues.size(); i++, j++) {
				
				
				canvas.drawLine(xValues.get(i) + 30, (yValues.get(i) - 10)*2,
						xValues.get(j) + 30, (yValues.get(j) - 10)*2, paint);
				
				
					
				
			}
			super.onDraw(canvas);
	}

		public int getSize() {
			return size;
		}

		public void addPoint(float p) {
			if (yValues.size() > size) {
				yValues.removeLast();
			}
			yValues.addFirst(p);
		}

		public void addBeat(long beat) {
			beatList.add(beat);
		}

	}
		 public void onBackPressed() {
		        
		     
		    	
		    	finish();
		       return;
		    } 	

}

Null Pointer Exception occuring during graph generation
===

Friends.. i am trying to drawing some graph . My application is running fine for first attempt but when i am trying to draw the graph repeatedly for 2,3 attempts then it is showing Null Pointer exception...What will be the solution? Here is my code





public class ECG extends Activity {
	
/** Called when the activity is first created. */
	
Context cnt;

Callback c;
	
Camera mCamera;
	
boolean mFinished;
	
DrawMyGraph g;
	
ImageView clock,graph_Text,heart_Index,graph_View;
	
static long startTime = 0;
	
static int beats = 0;
	
volatile boolean show=false;
	
SurfaceHolder sh;
	
SurfaceView sv;
	
TextView show_BPM;
	
Button on,off;
	
int no,count=0;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		show_BPM=(TextView)findViewById(R.id.showBPM);
		sv=(SurfaceView)findViewById(R.id.surfaceView1);
		clock=(ImageView)findViewById(R.id.clock);
		graph_Text=(ImageView)findViewById(R.id.graphText);
		heart_Index=(ImageView)findViewById(R.id.heartIndex);
		graph_View=(ImageView)findViewById(R.id.graphview);
		graph_Text.setBackgroundResource(R.drawable.graphtext2);
		on=(Button)findViewById(R.id.on);
		off=(Button)findViewById(R.id.off);
 
		Random r=new Random();
		 no=r.nextInt(84-60)+60;
		
		Context context = getApplicationContext();
		CharSequence text = "cover the camera with your index finger......!";
		int duration = 100000;

		Toast toast = Toast.makeText(context, text, duration);
		toast.show();
		///////////////////////////////
		////////////////////////////
		
		
		
		g = new DrawMyGraph(this, 26);
		addContentView(g, new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		startCamPreview();
		
				
		
	}

	public void startCamPreview() {
		SurfaceView sv = (SurfaceView) findViewById(R.id.surfaceView1);
		SurfaceHolder sh = sv.getHolder();
		mCamera = Camera.open();
		
		try {
			mCamera.setPreviewDisplay(sh);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sh.addCallback(new Callback() {

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				System.out.println("Deleted");
				mCamera.setPreviewCallback(null);
				mCamera.stopPreview();
				mCamera.release();
				mCamera = null;
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				System.out.println("Created");

				mCamera.setPreviewCallback(new PreviewCallback() {
					public void onPreviewFrame(byte[] data, Camera camera) {
						
						Camera.Parameters params = camera.getParameters();
			
						int width = params.getPreviewSize().width;
						int height = params.getPreviewSize().height;
						byte[] mYUVData = new byte[data.length];
						int[] mRGBData = new int[width * height];
						System.arraycopy(data, 0, mYUVData, 0, data.length);
						decodeYUV420SP(mRGBData, mYUVData, width, height);
						int[] mRedHistogram = new int[256];
						calculateIntensityHistogram(mRGBData, mRedHistogram,
								width, height, 0);
						double imageRedMean = 0;
						double redHistogramSum = 0;
						for (int bin = 0; bin < 256; bin++) {
							imageRedMean += mRedHistogram[bin] * bin;
							redHistogramSum += mRedHistogram[bin];
						} // bin
						imageRedMean /= redHistogramSum;
						double[] mBinSquared;
						mBinSquared = new double[256];
						for (int bin = 0; bin < 256; bin++) {
							mBinSquared[bin] = ((double) bin) * bin;
						}
						double imageRed2ndMoment = 0;
						for (int bin = 0; bin < 256; bin++) {
							imageRed2ndMoment += mRedHistogram[bin]
									* mBinSquared[bin];
						} // bin
						imageRed2ndMoment /= redHistogramSum;
						double imageRedStdDev = Math.sqrt((imageRed2ndMoment
								- imageRedMean * imageRedMean));
						int val = (new Double(imageRedStdDev * 100000)
								.intValue());
						
						show_BPM.setText(String.valueOf(no));
						int v = new Double(imageRedStdDev).intValue();
						
						for (int l = 0; l < 1; l++) {
							
						}
						int yVal=250;
						if (imageRedStdDev < 15 && val != 0)
						{
							show=true;
							g.addPoint(yVal);
							g.addPoint(yVal);
							float flt=(float)Math.random();
							g.addPoint(yVal+flt*25);
							g.addPoint(yVal);
							g.addPoint(yVal);
							flt=(float)Math.random();
							g.addPoint(yVal-flt*20);
							g.addPoint(yVal);
							g.addPoint(yVal);
							flt=(float)Math.random();
							g.addPoint(yVal-flt*30);
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							flt=(float)Math.random();
							g.addPoint(yVal+flt*35);
							try {
								Thread.sleep(500);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
							
						} else {
							show=false;
							if(val==0){
								
								count++;
								g.addPoint(yVal);
								g.addPoint(yVal);
								g.addPoint(yVal);
								g.addPoint(yVal);
								show_BPM.setText(String.valueOf(0));
							}else{
							
							count++;
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							g.addPoint(yVal);
							show_BPM.setText(String.valueOf(0));
							
							
							}
						}
					}

				});
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
				System.out.println("Changed");
				Camera.Parameters parameters = mCamera.getParameters();
				parameters.setPreviewSize(320, 240);
				parameters.setPreviewFrameRate(15);
				parameters.setSceneMode(Camera.Parameters.SCENE_MODE_NIGHT);
				parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
				mCamera.setParameters(parameters);
				mCamera.startPreview();
				g.invalidate();
			}
		});
		// fl.addView(b);
	}

	static public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, int width,
			int height) {
		final int frameSize = width * height;

		for (int j = 0, yp = 0; j < height; j++) {
			int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
			for (int i = 0; i < width; i++, yp++) {
				int y = (0xff & ((int) yuv420sp[yp])) - 16;
				if (y < 0)
					y = 0;
				if ((i & 1) == 0) {
					v = (0xff & yuv420sp[uvp++]) - 128;
					u = (0xff & yuv420sp[uvp++]) - 128;
				}

				int y1192 = 1192 * y;
				int r = (y1192 + 1634 * v);
				int g = (y1192 - 833 * v - 400 * u);
				int b = (y1192 + 2066 * u);

				if (r < 0)
					r = 0;
				else if (r > 262143)
					r = 262143;
				if (g < 0)
					g = 0;
				else if (g > 262143)
					g = 262143;
				if (b < 0)
					b = 0;
				else if (b > 262143)
					b = 262143;

				rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
						| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
			}
		}
	}

	static public void calculateIntensityHistogram(int[] rgb, int[] histogram,
			int width, int height, int component) {
		for (int bin = 0; bin < 256; bin++) {
			histogram[bin] = 0;
		} // bin
		if (component == 0) // red
		{
			for (int pix = 0; pix < width * height; pix += 3) {
				int pixVal = (rgb[pix] >> 16) & 0xff;
				histogram[pixVal]++;
			} // pix
		} else if (component == 1) // green
		{
			for (int pix = 0; pix < width * height; pix += 3) {
				int pixVal = (rgb[pix] >> 8) & 0xff;
				histogram[pixVal]++;
			} // pix
		} else // blue
		{
			for (int pix = 0; pix < width * height; pix += 3) {
				int pixVal = rgb[pix] & 0xff;
				histogram[pixVal]++;
			} // pix
		
			
		}
	}

	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);
		clock.setBackgroundResource(R.anim.clockanimation);
		AnimationDrawable clock_Anim=new AnimationDrawable();
		clock_Anim=(AnimationDrawable)clock.getBackground();
		clock_Anim.start();
		timer.start();
		}
	
	CountDownTimer timer=new CountDownTimer(30000,1000){

		@Override
		public void onFinish()throws NullPointerException {
			// TODO Auto-generated method stub
			mCamera.stopPreview();**//This is the line for which shows NullPointer exception**
			g.invalidate();
			Canvas.freeGlCaches();
			graph_Text.setBackgroundResource(R.drawable.graphtext3);
			if(count>0)
			{
				System.out.println("Hiiiiiii");
			show_BPM.setText(String.valueOf(0));
			}
			else
			{
				System.out.println("Helllooo");
				show_BPM.setText(String.valueOf(no));
			}
		}

		@Override
		public void onTick(long arg0) {
			// TODO Auto-generated method stub
			
		}
		
	};

		class DrawMyGraph extends View {
		public LinkedList<Float> yValues = null;
		private int size = 0;
		private LinkedList<Integer> xValues = null;
		private LinkedList<Long> beatList = new LinkedList<Long>();

		public DrawMyGraph(Context context, int size) {
			super(context);
			yValues = new LinkedList<Float>();
			int inc = 5;
			this.size = size * inc;
			xValues = new LinkedList<Integer>();

			int cnt = 468;
			for (int i = 0; i < this.size * inc; i++) {
				xValues.add(cnt);
				cnt = cnt - (inc - 2);
			}
		}

		@Override
		protected void onDraw(Canvas canvas) {
			Bitmap bitmap=Bitmap.createBitmap(2	, 2, Bitmap.Config.RGB_565);
			graph_View.setImageBitmap(bitmap);
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setStyle(Paint.Style.STROKE);
			paint.setStrokeWidth(2);
			paint.setColor(Color.RED);
			invalidate();
			for (int i = 0, j = 1; i < yValues.size() && j < yValues.size(); i++, j++) {
				
				
				canvas.drawLine(xValues.get(i) + 30, (yValues.get(i) - 10)*2,
						xValues.get(j) + 30, (yValues.get(j) - 10)*2, paint);
				
				
					
				
			}
			super.onDraw(canvas);
	}

		public int getSize() {
			return size;
		}

		public void addPoint(float p) {
			if (yValues.size() > size) {
				yValues.removeLast();
			}
			yValues.addFirst(p);
		}

		public void addBeat(long beat) {
			beatList.add(beat);
		}

	}
		 public void onBackPressed() {
		        
		     
		    	
		    	finish();
		       return;
		    } 	

}
