Question

i cannot find the null pointer exception in my code, i was wondering if anyone could help me find what is giving an error. code: package com.dingle.ubat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.Toast;

public class UltraBrightAndroidTorchActivity extends Activity {
/** Called when the activity is first created. */
 PowerManager pm;
 PowerManager.WakeLock wl;
 public boolean flash = false;
 public boolean enableFlash = false;
 public boolean dimDisplay = false;
 Display display = getWindowManager().getDefaultDisplay(); 
 public int windowWidth = display.getWidth();
 public int windowHeight = display.getHeight();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
    flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    if(flash == true && enableFlash == true){
    try {
            DroidLED led = new DroidLED();
            led.enable(!led.isEnabled());
    }
    catch(Exception e) {
            Toast.makeText(this, "Error interacting with LED.", Toast.LENGTH_SHORT).show();
            throw new RuntimeException(e);
    }  }

    wl.acquire();

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    //int tx = (int) event.getRawX();
    int ty = (int) event.getRawY();
    if (ty <= (windowHeight/2)){
        dimDisplay = !dimDisplay;
    }
    if (ty >= (windowHeight/2)){
        enableFlash = !enableFlash;
    }
    return super.onTouchEvent(event);
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    wl.release();
}

@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    super.onNewIntent(intent);
    wl.release();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    wl.release();
}

@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    wl.release();
}

}

Error list:

    12-10 20:40:42.224: W/dalvikvm(274): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-10 20:40:42.232: E/AndroidRuntime(274): Uncaught handler: thread main exiting due to uncaught exception
12-10 20:40:42.282: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dingle.ubat/com.dingle.ubat.UltraBrightAndroidTorchActivity}: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.os.Looper.loop(Looper.java:123)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-10 20:40:42.282: E/AndroidRuntime(274):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274):  at java.lang.reflect.Method.invoke(Method.java:521)
12-10 20:40:42.282: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-10 20:40:42.282: E/AndroidRuntime(274):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-10 20:40:42.282: E/AndroidRuntime(274):  at dalvik.system.NativeStart.main(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): Caused by: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274):  at com.dingle.ubat.UltraBrightAndroidTorchActivity.<init>(UltraBrightAndroidTorchActivity.java:20)
12-10 20:40:42.282: E/AndroidRuntime(274):  at java.lang.Class.newInstanceImpl(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274):  at java.lang.Class.newInstance(Class.java:1479)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-10 20:40:42.282: E/AndroidRuntime(274):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
12-10 20:40:42.282: E/AndroidRuntime(274):  ... 11 more

any help and criticism is greatly appreciated code is being compiled for android 2.1. code is a basic, crappily coded torch app. thanx

Was it helpful?

Solution

The code is bailing out on one of these two lines:

Display display = getWindowManager().getDefaultDisplay(); 
public int windowWidth = display.getWidth();

Either getWindowManager() is returning a null and that first initializer will fail, or getDefaultDisplay() is, and the second one will.

You should probably move these initializations to the onCreate handler. Having them at instance initialization sounds a bit risky - the activity's "environment" might not be completely set up yet at that point.

(And check the return values.)

OTHER TIPS

Activity is full available only after it has been created. This line does not do the intended purpose:

 Display display = getWindowManager().getDefaultDisplay(); 

Move this line and subsequent ones inside onCreate method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top