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 8921433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:32:32+00:00 2026-06-15T06:32:32+00:00

i am practicing with android examples, in the one to create your own camera

  • 0

i am practicing with android examples, in the one to create your own camera application
i am getting the error java.lang.NullPointerException when i create my own surfaceview to display the camera images

Here is my code:

package com.example.prueba;

import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;



    /** A basic Camera preview class */
    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
        private static final String TAG = "CameraPreview";
        private SurfaceHolder mHolder;
        private Camera mCamera;

        public CameraPreview(Context context, Camera camera) {
            super(context);
            mCamera = camera;

            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed.
            mHolder = getHolder();
            mHolder.addCallback(this);
            // deprecated setting, but required on Android versions prior to 3.0
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        public void surfaceCreated(SurfaceHolder holder) {
            // The Surface has been created, now tell the camera where to draw the preview.
            try {
                mCamera.setPreviewDisplay(holder);
                mCamera.startPreview();
            } catch (IOException e) {
                Log.d(TAG, "Error setting camera preview: " + e.getMessage());
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            // empty. Take care of releasing the Camera preview in your activity.
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // If your preview can change or rotate, take care of those events here.
            // Make sure to stop the preview before resizing or reformatting it.

            if (mHolder.getSurface() == null){
              // preview surface does not exist
              return;
            }

            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e){
              // ignore: tried to stop a non-existent preview
            }

            // set preview size and make any resize, rotate or
            // reformatting changes here

            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();

            } catch (Exception e){
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());
            }
        }
    }

//And here the error log:


//11-28 13:19:24.451: E/AndroidRuntime(870): FATAL EXCEPTION: main
//11-28 13:19:24.451: E/AndroidRuntime(870): java.lang.NullPointerException
//11-28 13:19:24.451: E/AndroidRuntime(870):    at com.example.prueba.CameraPreview.surfaceCreated(CameraPreview.java:34)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.SurfaceView.access$000(SurfaceView.java:81)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1820)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.Choreographer.doFrame(Choreographer.java:525)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.os.Handler.handleCallback(Handler.java:615)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.os.Handler.dispatchMessage(Handler.java:92)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.os.Looper.loop(Looper.java:137)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at android.app.ActivityThread.main(ActivityThread.java:4745)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at java.lang.reflect.Method.invokeNative(Native Method)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at java.lang.reflect.Method.invoke(Method.java:511)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
//11-28 13:19:24.451: E/AndroidRuntime(870):    at dalvik.system.NativeStart.main(Native Method)
  • 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-06-15T06:32:33+00:00Added an answer on June 15, 2026 at 6:32 am

    The proble was that mCamera was null because i didn´t put into the manifest this:

    <uses-permission android:name="android.permission.CAMERA" />
    

    Now that problem is solved, but the surfaceview is black i don´t see camera image and i get camera error 100

    To solve the camera error 100 i changed surfaceChanged adding lines to set the new size:

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
            // If your preview can change or rotate, take care of those events here.
            // Make sure to stop the preview before resizing or reformatting it.
    
            if (mHolder.getSurface() == null){
              // preview surface does not exist
              return;
            }
    
            // stop preview before making changes
            try {
                mCamera.stopPreview();
            } catch (Exception e){
              // ignore: tried to stop a non-existent preview
            }
    
            // set preview size and make any resize, rotate or
            // reformatting changes here
            Parameters parameters= mCamera.getParameters();
            parameters.setPictureSize(w, h);
            mCamera.setParameters(parameters);
    
            // start preview with new settings
            try {
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
    
            } catch (Exception e){
                Log.d(TAG, "Error starting camera preview: " + e.getMessage());
            }
        }
    

    Now works properly, but i don´t know why i have to add those lines because in the manifest i put screenorientation landscape like in the example of android sdk:

    <activity android:name=".CameraActivity"
          android:label="@string/app_name"
    
          android:screenOrientation="landscape">
          <!-- configure this activity to use landscape orientation -->
    
          <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    

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

Sidebar

Related Questions

practicing programming with MyProgrammingLab and getting following compile error: ApartmentBuilding.java:4: error: expected its also
I am practicing at getting consistent with my error handling, and I keep hoping
I'm practicing on Android development by watching Thenewboston's videos. From one of his videos,
For practicing purposes, I’m about to create a new ASP.NET MVC 3.0 application. My
I am practicing the Android maps now i am getting the maps successfully in
I am a new bee to Android App Development . I am practicing the
Started practicing with XML and C# and I have an error message of There
I'm practicing my gui skills with Java and I have been doing menus and
I am basically practicing with Java socket programming by building client and server (not
I just started developing on Android. I'm practicing with a Tablayout tutorial in API

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.