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();
}