0

I have an app that sometimes get crushed, it has never happened to me, some users have reported it... this is the stack trace that google sends me:

java.lang.NullPointerException: 
at com.mal.saul.preciosbitcoinmexico.Fragment.FragmentPrincipal. 
   realizarCambioPrecio(FragmentPrincipal.java:181)  
at com.mal.saul.preciosbitcoinmexico.Fragment.FragmentPrincipal.
   access$200(FragmentPrincipal.java:42)
at com.mal.saul.preciosbitcoinmexico.Fragment.FragmentPrincipal$2.
   onResponse(FragmentPrincipal.java:160)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.
   run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method:0)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller. 
   run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)   

i think my error is on the onResponse Method, here it is:

@Override
        public void onResponse(Call<BtcValuesResponse> call, 
        Response<BtcValuesResponse> response) {                
            BtcValuesResponse btcValuesResponse = 
            response.body();
            if(btcValuesResponse != null){
                btcValues = 
                btcValuesResponse.getBtcValues();
                showBtcValues();                                                         
            }
            else {
                Toast.makeText(getActivity(), "Bitso No Está Disponible Por 
            el Momento", Toast.LENGTH_SHORT).show();
            }
        }                                                    

any idea?

Saul_programa
  • 341
  • 1
  • 4
  • 13
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – sushildlh Jun 02 '17 at 04:21

2 Answers2

0

You are requesting data by Retrofit asynchronously, after request is completed, you show data by calling showBtcValues().

In a case that your request has not completed yet but users change to other screen. So, when it finish, it still calls showBtcValues, then you will get crash because there is no UI component there.

Please try that to recreate the crash. I think it is the issue here.

If it is, the solution is that you should check the state of Fragment (or Activity) where you call request.

Quang Nguyen
  • 2,600
  • 2
  • 17
  • 24
  • thanks, how can i check if the fragment is being displayed? – Saul_programa Jun 10 '17 at 02:27
  • Firstly, please confirm whether that is the reason or not. – Quang Nguyen Jun 10 '17 at 04:00
  • i cannot confirm it, my phone doesn't throw this exception, i have tried to recreate it with no success. Some users have reported it to google (my app is on the play store) and i'm trying to fix it...but i have been thinking for a while and think that is the only possible option – Saul_programa Jun 10 '17 at 04:17
0

As stated before maybe your there is no UI component there

or

You are just assuming the the response Response<BtcValuesResponse> response is valid, but you should check if the API response is not rate limited, or due connectivity errors, the response was an invalid nonce or something else. You need to verify response status before unwrapping it.

if(response.isSuccessful()){
  // Get the body, where the BTCValueResponse is
  BtcValuesResponse responseBody = response.body();
  btcValuesResponse.getBtcValues();
  showBtcValues(); 
}else{
  // Check the error stream
 System.out.println(Utils.inputStreamToString(wrappedServiceResponse.errorBody().byteStream()));
}
vicco
  • 181
  • 2
  • 7