2017-12-18 124 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()); }}}