shafiq ruslan
shafiq ruslan

Reputation: 41

Getting NullPointerException in context

I want to run the program in Android Studio but i got this error.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
    at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:152)
    at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:224)
    at android.app.AlertDialog$Builder.<init>(AlertDialog.java:454)
    at com.example.drowzy.LivePreviewActivity.drowzy_alert(LivePreviewActivity.java:219)
    at com.example.drowzy.LivePreviewActivity.eye_tracking(LivePreviewActivity.java:212)
    at com.example.drowzy.FaceDetectionProcessor.onSuccess(FaceDetectionProcessor.java:90)
    at com.example.drowzy.FaceDetectionProcessor.onSuccess(FaceDetectionProcessor.java:38)
    at com.example.drowzy.VisionProcessorBase$2.onSuccess(VisionProcessorBase.java:114)
    at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:251)
    at android.app.ActivityThread.main(ActivityThread.java:6572)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

It says my context is null. I have try the possible solution in google but its not working. Does my way to initialize context is wrong? Can i call drowzy_alert method in another class without specified context in FaceDetectionProcessor ?

This is my LivePreviewActivity

public void eye_tracking(@NonNull FirebaseVisionFace face){
    if (face.getRightEyeOpenProbability() < 0.1 && face.getLeftEyeOpenProbability() < 0.1) {
        drowzy_alert();
    }
}

public void drowzy_alert(){

    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Drowzy Detected")
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // FIRE ZE MISSILES!
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    // Create the AlertDialog object and return it
    builder.create();
}

and this is my FaceDetectionProcessor

protected void onSuccess(
        @Nullable Bitmap originalCameraImage,
        @NonNull List<FirebaseVisionFace> faces,
        @NonNull FrameMetadata frameMetadata,
        @NonNull GraphicOverlay graphicOverlay
) {

    graphicOverlay.clear();
    if (originalCameraImage != null) {
        CameraImageGraphic imageGraphic = new CameraImageGraphic(graphicOverlay, originalCameraImage);
        graphicOverlay.add(imageGraphic);
    }
    for (int i = 0; i < faces.size(); ++i) {
        FirebaseVisionFace face = faces.get(i);

        int cameraFacing =
                frameMetadata != null ? frameMetadata.getCameraFacing() :
                        Camera.CameraInfo.CAMERA_FACING_BACK;
        FaceGraphic faceGraphic = new FaceGraphic(graphicOverlay, face, cameraFacing);
        LivePreviewActivity livePreviewActivity = new LivePreviewActivity();
        graphicOverlay.add(faceGraphic);
        livePreviewActivity.eye_tracking(face);
    }
    graphicOverlay.postInvalidate();
}

Upvotes: 0

Views: 1332

Answers (1)

Vardaan Sharma
Vardaan Sharma

Reputation: 1165

LivePreviewActivity livePreviewActivity = new LivePreviewActivity();

You cannot instantiate LivePreviewActivity in that manner. If you have an activity you can only use startActivity or some other context method that is managed by android.

The entire idea is that whenever you are using an activity that activity should be on top of the activity stack (i.e visible to your user) and startActivity does that for you. It also passes the application and activity context amongst your activities which is needed by the app to be aware of the state of the application as well as the device. It is not possible for an application to work without this context.

The reason your activity needs to be at the top of the stack is, once an activity is no longer at the top of your stack it can be killed by android at any point to conserve memory and there is nothing you can do to prevent that behavior. You can react to an activity being killed, but not more than that.

From what I gather, you will have to move all this logic to a single activity instead of splitting it across two. If you do want to load multiple activities at the same time, consider using fragments instead.

Upvotes: 1

Related Questions