0

normal proximity sensor app,that changes the string according to the value of the sensor unless i add the if condition the app crashes as soon as it is launched,after adding the if condition it crashes after using the proximity sensor:

java code:

    public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if(event.values[0]==3.0)
    proxText.setText(String.valueOf(event.values[0]));-->line 86

    }

logcat:

    06-24 02:11:26.185: W/dalvikvm(11580): threadid=1: thread exiting with uncaught exception (group=0x41634d40)
    06-24 02:11:26.186: E/AndroidRuntime(11580): FATAL EXCEPTION: main
    06-24 02:11:26.186: E/AndroidRuntime(11580): Process: com.example.tapera, PID: 11580
    06-24 02:11:26.186: E/AndroidRuntime(11580): java.lang.NullPointerException
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at com.example.tapera.MainActivity.onSensorChanged(MainActivity.java:86)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:448)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at android.os.MessageQueue.nativePollOnce(Native Method)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at android.os.MessageQueue.next(MessageQueue.java:138)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at android.os.Looper.loop(Looper.java:123)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at android.app.ActivityThread.main(ActivityThread.java:5102)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at java.lang.reflect.Method.invokeNative(Native Method)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at java.lang.reflect.Method.invoke(Method.java:515)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    06-24 02:11:26.186: E/AndroidRuntime(11580):    at dalvik.system.NativeStart.main(Native Method)

2 Answers2

0

Very likely that "event" object in method onSensorChanged in null. Thats why exception occurs - there is no possibility to extract value for index "0" in event.values

0

Whenever I have an issue like this, I always check my Views first. Are you sure that in every call of this function your proxText is never null?

You can e reasonably sure that your event isn't null because you aren't crashing in the if Conditional.

Change your if to

if (proxText != null && event.values[0]==3.0)

If proxText being null is an issue you will need to look into why this is being called before your code sets it up.

laochiv
  • 2,433
  • 1
  • 15
  • 17