-2

At the beginning I had a perfectly working code; I was creating a 'location' and then I would save it in a database and retrieve it any time I wanted. 'Location' was an object that only contained Strings. Then I wanted to add some icons that could desctribe the location easier and my program crashed.

I am getting an error:

2021-01-07 11:01:17.484 32731-32731/pl.issrfid.isshandheldrfidscanner E/AndroidRuntime: FATAL EXCEPTION: main
Process: pl.issrfid.isshandheldrfidscanner, PID: 32731
java.lang.NullPointerException: Attempt to invoke virtual method 'long pl.issrfid.isshandheldrfidscanner.model.LocationModel.getID()' on a null object reference
    at pl.issrfid.isshandheldrfidscanner.adapter.DBAdapter.deleteLocation(DBAdapter.java:414)
    at pl.issrfid.isshandheldrfidscanner.activity.Api.Synchronization.DownloadLocation.onPostExecute(DownloadLocation.java:123)
    at pl.issrfid.isshandheldrfidscanner.activity.Api.Synchronization.DownloadLocation.onPostExecute(DownloadLocation.java:33)
    at android.os.AsyncTask.finish(AsyncTask.java:755)
    at android.os.AsyncTask.access$900(AsyncTask.java:192)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7948)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)

But when I debugged my app I noticed that the object - product - is not null:

enter image description here

Code for DBAdapter.java:414:

        for(ProductModel product : mainProductList){
        if(product.getLocationModel().getID() == locationModel.getID()){
            Log.e(TAG, "moving productId: " + product.getId() + "; productApiId: " +
                    product.getApiID() + "; to defaultLocation");
            product.setLocationModel(defaultLocation);
            product.setLastEditDateTime();
        }
    }

Code for (DownloadLocation.java:123:

DBAdapter.getInstance(context).deleteLocation(location);

Before I added icons to the 'location' the code worked, I have not however changed the 'product' part of my code and I have no idea what might have changed

Andrea
  • 57
  • 1
  • 7

1 Answers1

0

It's not the product which is being null. It's the LocationModel associated with your product.

The method getLocationModel() returns null which is why you can't call the getID method and get the NPE.

You should insert a check if the LocationModel is not null before querying its ID. Something along these lines:

for(ProductModel product : mainProductList) {
    LocationModel model = product.getLocationModel();
    if (model != null && model.getID() == locationModel.getID()) {
        Log.e(TAG, "moving productId: " + product.getId() + "; productApiId: " +
                    product.getApiID() + "; to defaultLocation");
        product.setLocationModel(defaultLocation);
        product.setLastEditDateTime();
    }
}
oemel09
  • 744
  • 7
  • 15