Search code examples
javaandroidsamsung-mobileandroid-developer-api

Toast cause NPE on Samsung Galaxy S5 with Android 5


Here's code of Splash Screen (it's not my desire, but client) - the only thing I've done - just to replace sone code with loading data to this activity:

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    GetAll getAllProducts = retrofit.create(GetAll.class);
    Call<ArrayList<Products>> call = getAllProducts.getAll(Constants.PARTNER_ID,
            WaterFragment.getMD5Hash(Constants.PARTNER_ID + Constants.API_KEY));
    call.enqueue(new Callback<ArrayList<Products>>() {
        @Override
        public void onResponse(Response<ArrayList<Products>> response, Retrofit retrofit) {
            if (response.body() != null) {
                ActiveAndroid.beginTransaction();
                try {
                    ProductsDMC product;
                    for (Products p : response.body()) {
                        if (new Select().from(ProductsDMC.class).where("product_id = ?", p.id).executeSingle() != null) {
                            product = new Select().from(ProductsDMC.class).where("product_id = ?", p.id).executeSingle();
                            product.price = p.price;
                        } else {
                            product = new ProductsDMC(p.id, p.article, p.title, p.price, p.amount, p.category_id,
                                    p.brand_id, p.description, p.photo[0], p.option != null ? p.option.bottle_price : "0");
                        }
                        product.save();
                    }
                    ActiveAndroid.setTransactionSuccessful();
                } finally {
                    ActiveAndroid.endTransaction();
                }
            }else{
                Toast.makeText(SplashActivity.this, "Try later!", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFailure (Throwable t){
            Toast.makeText(SplashActivity.this, "Check your connection!", Toast.LENGTH_SHORT).show();
            Log.d("LoloPolo", t.getMessage().toString());
        }
    });
    Intent intent = new Intent(this, ListOrdersActivity.class);
    startActivity(intent);
    finish();
}
}

Tonight my crashlytic send info about crash on app:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
   at ru.luny.aqualuxe.activity.SplashActivity$3.onFailure(SplashActivity.java:142)
   at retrofit.ExecutorCallAdapterFactory$ExecutorCallback$2.run(ExecutorCallAdapterFactory.java:94)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:5832)
   at java.lang.reflect.Method.invoke(Method.java)

It's about this:

Toast.makeText(SplashActivity.this, "Try later!", Toast.LENGTH_SHORT).show();

I really don't understand - how Toast.makeText().show can cause NPE))

Could you help me please?

EDIT

It seems strange but crashlytics shows me wrong place of error code string. It's not about toast but debug-loggin with getting message from exception.


Solution

  • Problem is t.getMessage().toString()

    You are getting NPE in onFailure (Throwable t) section . For testing case add ""+

     @Override
        public void onFailure (Throwable t){
            Toast.makeText(SplashActivity.this, "Check your connection!", Toast.LENGTH_SHORT).show();
             Log.d("LoloPolo",""+ t.getMessage().toString());
        }