Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3941338
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:31:46+00:00 2026-05-20T00:31:46+00:00

I’m trying to code an app that uses the camera. I’m getting a NullPointerException

  • 0

I’m trying to code an app that uses the camera. I’m getting a NullPointerException when trying to get the surfaceHolder that i eventually pass to the surfaceCreated() that starts up the camera. is there any situation when the getHolder() returns NULL?

thanks.

package com.tecmark;

import java.io.IOException;

import android.app.Activity;

import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class cameraView extends Activity implements SurfaceHolder.Callback{

    SurfaceView mSurfaceView;
    SurfaceHolder mSurfaceHolder;
    Camera mCamera;
    boolean mPreviewRunning;

 /** Called when the activity is first created. */
    @Override 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

        WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //setContentView(R.layout.camera_surface);   

        mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
        Log.i("surfaceholder = ", "about to get surface holder");
        try{
        mSurfaceHolder = mSurfaceView.getHolder();
        }catch(Exception e){
         e.printStackTrace();
        }

        Log.i("surfaceholder = ", ""+mSurfaceHolder.toString());

        mSurfaceHolder.addCallback(this);

        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        setContentView(R.layout.camera_surface);

        surfaceCreated(mSurfaceHolder);







    }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

  if (mPreviewRunning) {

   mCamera.stopPreview();

   }

   Camera.Parameters p = mCamera.getParameters();

   p.setPreviewSize(w, h);

   mCamera.setParameters(p);

   try {

   mCamera.setPreviewDisplay(holder);

   } catch (Exception e) {

   e.printStackTrace();

   }

   mCamera.startPreview();

   mPreviewRunning = true;

   }






 @Override
 public void surfaceCreated(SurfaceHolder holder) {




  try{
   Log.i("camera ", "about to open camera");
         mCamera = Camera.open();
         Log.i("camera ", " camera opened");
         mCamera.getParameters();
         mCamera.setPreviewDisplay(holder);
         mCamera.startPreview();
   } catch (IOException e) {

    e.printStackTrace();
   }
  Log.i("camera ", "ok");
 }

 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
  mCamera.stopPreview();

  mPreviewRunning = false;

  mCamera.release();

 }




}//end of activity



01-31 15:29:17.773: WARN/System.err(9144): java.lang.NullPointerException
01-31 15:29:17.778: WARN/System.err(9144):     at com.tecmark.cameraView.onCreate(cameraView.java:42)
01-31 15:29:17.778: WARN/System.err(9144):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 15:29:17.778: WARN/System.err(9144):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
01-31 15:29:17.783: WARN/System.err(9144):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-31 15:29:17.783: WARN/System.err(9144):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-31 15:29:17.783: WARN/System.err(9144):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-31 15:29:17.783: WARN/System.err(9144):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 15:29:17.783: WARN/System.err(9144):     at android.os.Looper.loop(Looper.java:123)
01-31 15:29:17.783: WARN/System.err(9144):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-31 15:29:17.783: WARN/System.err(9144):     at java.lang.reflect.Method.invokeNative(Native Method)
01-31 15:29:17.788: WARN/System.err(9144):     at java.lang.reflect.Method.invoke(Method.java:521)
01-31 15:29:17.788: WARN/System.err(9144):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-31 15:29:17.788: WARN/System.err(9144):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-31 15:29:17.788: WARN/System.err(9144):     at dalvik.system.NativeStart.main(Native Method)
01-31 15:29:17.793: DEBUG/AndroidRuntime(9144): Shutting down VM
01-31 15:29:17.793: WARN/dalvikvm(9144): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
01-31 15:29:17.793: ERROR/AndroidRuntime(9144): Uncaught handler: thread main exiting due to uncaught exception
01-31 15:29:17.803: ERROR/AndroidRuntime(9144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tecmark/com.tecmark.cameraView}: java.lang.NullPointerException
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.os.Looper.loop(Looper.java:123)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.app.ActivityThread.main(ActivityThread.java:4363)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at java.lang.reflect.Method.invokeNative(Native Method)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at java.lang.reflect.Method.invoke(Method.java:521)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at dalvik.system.NativeStart.main(Native Method)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144): Caused by: java.lang.NullPointerException
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at com.tecmark.cameraView.onCreate(cameraView.java:47)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
01-31 15:29:17.803: ERROR/AndroidRuntime(9144):     ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:31:46+00:00Added an answer on May 20, 2026 at 12:31 am

    Starting a new answer to hopefully be more clear. Again, I think the root issue is the amount of stuff youre doing in onCreate vs. onResume.

    I think the root issue youre having is that some parts of the view/activity lifecycle need to run their course before you start dealing with the surfaceView and starting prevew with it.

    I’m not certain of that but here’s the breakdown of the various places I deal with various camera and surface view stuff in an augmented reality app that works:

    onCreate()
    {
        // just set content view. do nothing with the camera or surfaceView yet
        setContentView(R.layout.main);
    }
    
    onResume()
    {
            // open camera
        mCamera = Camera.open();
    
        // init surface view
        sv = (SurfaceView)this.findViewById(R.id.SurfaceView01);
                mHolder = sv.getHolder(); 
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
                mHolder.setSizeFromLayout();
                mHolder.addCallback(this); 
    }
    
    surfaceChanged(SurfaceHolder holder, int format, int w, int h)
    {
        mCamera.setPreviewDisplay(holder);
        // set any cam params you need...
    
        mCamera.startPreview();
    }
    

    Try restructuring your code to do similar. My eyes got bleary trying to count out where the NPE was happening from your source but I think a few problems are:

    • you seem to be trying to get reference to the surfaceview BEFORE youve called setContentView()
    • youre directly calling the surfaceCreated method, rather than letting it be called via the callback.

    Hope that helps. We’ll get this figured out yet!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.