Try/catch不起作用

我有一个类来显示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不起作用?

回答:

把你的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()); }}}

以上是 Try/catch不起作用 的全部内容, 来源链接: utcz.com/qa/263502.html

回到顶部