2017-12-18 43 views
2

我有一個類來顯示HTTP的錯誤消息。Try/catch不起作用

根據throwable它顯示一條消息。

但有些時候我得到空指針異常

public static void showGeneralErrors(Throwable throwable) { 
    String message = ""; 
    AppInitialization appInitialization = AppInitialization.getInstance(); 

    if (appInitialization == null) { 
     return; 
    } 

    try { 
     if (throwable instanceof HttpException) { 
      if (((HttpException) throwable).code() == 500) { 
       message = appInitialization.getString(R.string.server_error); 
      } else { 
       message = appInitialization.getString(R.string.parsing_problem); 
      } 
     } else if (throwable instanceof IOException) { 
      message = appInitialization.getString(R.string.internet_error); 
     }else if(throwable instanceof SSLHandshakeException){ 
      message = appInitialization.getString(R.string.internet_error); 
     } 
     if (!TextUtils.isEmpty(message)) { 
      Toast.makeText(appInitialization, message, Toast.LENGTH_SHORT).show(); 
     } 
    } catch (Exception e) { 
     Log.e(">>>>>", "Exception network error handler " + e.getMessage()); 
    } catch (IllegalStateException e) { 
     Log.e(">>>>>", "IllegalStateException network error handler " + e.getMessage()); 
    } catch (NullPointerException e) { 
     Log.e(">>>>>", "NullPointerException network error handler " + e.getMessage()); 
    } 
} 

和錯誤消息是:

產生的原因:顯示java.lang.NullPointerException:試圖調用虛擬方法android.content.res的.resources android.content.Context.getResources()」上的空對象引用 在android.widget.Toast.makeText(Toast.java:298)

和公共AppInitialization是:

public class AppInitialization extends Application { 
private static AppInitialization mInstance; 

public static synchronized AppInitialization getInstance() { 
    return mInstance; 
} 

public void onCreate() { 
    super.onCreate(); 

    mInstance = this; 
} 

它來自改造onFailure處方法:

GeneralRepo.getCountryFromIp(getContext()) 
.observeOn(AndroidSchedulers.mainThread()) 
.subscribeOn(Schedulers.io()) 
.subscribe(countryFromIPResponse -> { 
     //do something 
    }, throwable -> { 
     // Where i got error 
     NetworkErrorHandler.showGeneralErrors(throwable); 
    }); 

爲什麼我得到這個錯誤,爲什麼的try/catch不起作用?

+0

因爲您的錯誤未在該代碼塊上觸發,請將更多的logcat放入您的答案中以查看原始錯誤。 – diegoveloper

+0

同意@diegoveloper,同時檢查throwable對象是否爲空 –

+0

@diegoveloper更新 –

回答

0

把你的try catch塊上的其他部分,因爲NullPointerException異常發生

appInitialization

即將空,因此..

寫:

公共靜態無效showGeneralErrors (可拋投){ Stri ng message =「」; AppInitialization appInitialization = AppInitialization.getInstance();

if (appInitialization == null) { 
    return; 
}else{ 

    try {   if (throwable instanceof HttpException) { 
      if (((HttpException) throwable).code() == 500) { 
       message = appInitialization.getString(R.string.server_error); 
      } else { 
       message = appInitialization.getString(R.string.parsing_problem); 
      }   } else if (throwable instanceof IOException) { 
      message = appInitialization.getString(R.string.internet_error);    }else if(throwable instanceof SSLHandshakeException){ 
      message = appInitialization.getString(R.string.internet_error);    }   if (!TextUtils.isEmpty(message)) { 
      Toast.makeText(appInitialization, message, Toast.LENGTH_SHORT).show();   }  } catch (Exception e) { 
     Log.e(">>>>>", "Exception network error handler " + 

e.getMessage()); } catch(IllegalStateException e){LogF(「>>>>>」,「IllegalStateException網絡錯誤處理程序」+ e.getMessage()); ();} catch(NullPointerException e)Log.e(「>>>>>」,「NullPointerException網絡錯誤處理程序」+ e.getMessage()); }}}