NullPointerException on Accelerometer object

Hey,

I followed the documentation provided by you and made
all the changes. But now, when I start scanning the accelerometer data, I
am getting a NullPointerException saying the accelerometer module is
null.

Can you tell me how do I rectify this?

Thanks,
Deepak

Comments

  • You need to have an active ble connection before calling getModule.

  • I am having an active BLE connection with the MetaWear R board before calling the getModule. I am connecting to the board in the previous activity and passing it on to the accelerometer activity.
  • edited August 2015
    Hey Eric, I managed to come out of this issue. But bumped into another issue. The app now works fine with android lollipop. But when I run it on a phone running Android Kitkat, I get a NullPointerException at
    accelCtrllr.setOutputDataRate(100.f);
    I don't know what the issue is related to but the old API was working perfectly with the same phone. I would appreciate if you could help me get past this issue.

    Thanks,
    Deepak
  • You will need to provide some more information about your code flow.  What happens between retrieving an accelerometer module and calling setOutputDataRate?
  • There's absolutely nothing in between both of them. Both are coded together. But I am getting a NullPointerException when I call the setOutPutDataRate.
  • The code flow goes like this
    public void onServiceConnected(ComponentName name, IBinder service) {
    Log.i("Service", "Connected");

    serviceBinder = (MetaWearBleService.LocalBinder) service;
    mwBoard= serviceBinder.getMetaWearBoard(MW_Board);
    if (mwBoard != null) {
    try {
    accelCtrllr = mwBoard.getModule(Accelerometer.class);
    // Set the sampling frequency to 100Hz, or closest valid ODR
    accelCtrllr.setOutputDataRate(100.f);
    // Set the measurement range to +/- 8g, or closet valid range
    accelCtrllr.setAxisSamplingRange(8.f);

    // enable axis sampling
    accelCtrllr.enableAxisSampling();

    ....
  • How are you getting to this activity (or fragment)?  The only way I can see you running into a null pointer exception is that you are not connected to the board when you call getModule.  So, to make sure, does mwBoard.isConnected() return true on both devices?  It sounds like connection to the board is lost on the KitKat phone when you get to this point of code.
  • I am using the same method that I was using with the old API. I am giving the bluetooth device connection to the activity using parcelable data. This was working fine in the kitkat phone but with the new API, it is not working anymore
  • Yes, which is why I am asking if "mwBoard.isConnected()" returns true when you are calling "getModule"

    if (!mwBoard.isConnected()) {
      Log.e("Test", "Device is not connected to a MetaWear board");
    } else {
      accelModule= mwBoard.getModule(Accelerometer.class);
    }
  • Hey Eric, I tried the above method. I put the mwBoard.connect(); method before the code and then this seems to do the work inside the onServiceConnected method. But I am now getting a Null Pointer Exception at accelCtrllr.start(); which is called on button click.
  • edited August 2015
    Again, I am asking if "mwBoard.isConnected()" returns true when you are calling "getModule".  If you see the error message in the logger, then accelModule is not being assigned a value, therefore is still null when you press the button.
  • The mwBoard.isConnected() is returning true. And that's the reason a call to the accelCtrllr.start() is being made. And another reason for me to believe that it is connected is because I have added an else condition that logs a message saying mwBoard not connect which I am not able to see being logged.
  • Hey Eric,

    I made some changes to my code and made it in this way:
    if (mwBoard.isConnected()) {
    accelCtrllr.start();
    }
    else{
    Log.i("mwBoard", "Not Connected");
    }
    and
    public void onServiceConnected(ComponentName name, IBinder service) {
    Log.i("Service", "Connected");
    serviceBinder = (MetaWearBleService.LocalBinder)service;
    mwBoard=serviceBinder.getMetaWearBoard(MW_Board);
    mwBoard.connect();
    if (mwBoard.isConnected()) {
    try {

    accelCtrllr = mwBoard.getModule(Accelerometer.class);
    ....

    Even now I am getting the same issue.
    I am getting a NullPointerException at
    accelCtrllr.start();

  • At this point, I'm not sure what could be wrong.  You can take a look at one of our demo projects we are doing as a tutorial to compare against your project, link here.
This discussion has been closed.