我有一个类来显示HTTP的错误消息。
根据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()'在null对象引用上 在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);
});
为什么我收到此错误以及为什么尝试/捕获不起作用?
答案 0 :(得分:0)
将try catch块放在else部分,因为发生NullPointerException
appInitialization
即将为空......
写道:
public static void showGeneralErrors(Throwable throwable){ 字符串消息="&#34 ;; 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){ Log.e(">>>>>"," IllegalStateException网络错误处理程序" + e.getMessage()); } catch(NullPointerException e){ Log.e(">>>>>"," NullPointerException网络错误处理程序" + e.getMessage()); }}