-1

I'm creating a simple Android Class that update a variable with the most recent value readed from some specific sensors (Light, temp and more), which I access it(sensor value) by getData() method, but i'm getting an error. I hope u can help me!

Here is my generic sensor class.

MySensor

public class mySensor extends Service implements SensorEventListener {

private SensorManager mSensorManager =null;
private Sensor s=null;

float data = 0;


public void onCreate(){
    super.onCreate();
    mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);


}


public mySensor(int type){

    //HERE I'M GETTING THE ERROR
    s= mSensorManager.getDefaultSensor(type);




}

public String getData(){

    return String.valueOf(data);
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_LIGHT) {
        data = event.values[0];
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int i) {

}
protected void onResume() {

    mSensorManager.registerListener((SensorEventListener) this, s, SensorManager.SENSOR_DELAY_NORMAL);


}

protected void onPause() {
   // super.onPause();
    mSensorManager.unregisterListener((SensorEventListener) this);
}
}

Here is the log

 ...
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: project.luke.com.sensorreader, PID: 25297
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{project.luke.com.sensorreader/project.luke.com.sensorreader.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Sensor android.hardware.SensorManager.getDefaultSensor(int)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
                      at android.app.ActivityThread.access$800(ActivityThread.java:169)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
                      at android.os.Handler.dispatchMessage(Handler.java:111)
                      at android.os.Looper.loop(Looper.java:194)
                      at android.app.ActivityThread.main(ActivityThread.java:5546)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.hardware.Sensor android.hardware.SensorManager.getDefaultSensor(int)' on a null object reference
                      at project.luke.com.sensorreader.mySensor.<init>(mySensor.java:39)
                      at project.luke.com.sensorreader.MainActivity.onCreate(MainActivity.java:32)
                      at android.app.Activity.performCreate(Activity.java:5975)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
                      at android.app.ActivityThread.access$800(ActivityThread.java:169) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
                      at android.os.Handler.dispatchMessage(Handler.java:111) 
                      at android.os.Looper.loop(Looper.java:194) 
                      at android.app.ActivityThread.main(ActivityThread.java:5546) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 

In my MainActivity class I simple call mLight=new mySensor(Sensor.TYPE_LIGHT); but this trigger the error, cause mSensorManager=null, always.

  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Pradeep Simha Nov 21 '16 at 19:01
  • Do not create a random subclass of `Service` and use it via some constructor. Most likely, using a `Service` is not necessary -- move this logic into your existing `Activity`. Even if you really do want a `Service`, you need to use it properly (e.g., `startService()`), not creating some instance yourself. – CommonsWare Nov 21 '16 at 19:04
  • @Simze The question isn't "What is an NPE?" but "Why am I getting *this* NPE after I try to initialize it? – Sam Nov 21 '16 at 19:12
  • @Sam it can be argued. There is no good reason in the OP's code to think that the `mSensorManager` should be anything but null, even with no knowledge of android. It is simply a matter of knowing how an object is initialized, and that's what an NPE really is all about. – njzk2 Nov 21 '16 at 19:49

1 Answers1

0

From your MainActivity:

Intent intent = new Intent(this, mySensor.class);
intent.putExtra("type", Sensor.TYPE_LIGHT);

and within your service:

@Override
public IBinder onBind(Intent intent) {
    mSensorManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    int type = intent.getExtras().get("type");
    s= mSensorManager.getDefaultSensor(type);
    return null;
}

then you can remove:

public mySensor(int type){
   ...
}

and eventually the code you wrote within the onCreate.

Lino
  • 3,968
  • 3
  • 14
  • 32