2017-12-18 12 views
2

私はHTTPのエラーメッセージを表示するクラスを持っています。試してみるとキャッチがうまくいかない

投げ札によると、メッセージが表示されます。

しかし、私は、nullポインタ例外に

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を起動しようとandroid.widget.Toast.makeText(Toast.java:298)でヌルオブジェクト参照 に.resourcesファイルのandroid.content.Context.getResources()」

public class AppInitialization extends Application { 
private static AppInitialization mInstance; 

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

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

    mInstance = this; 
} 

そして、それは方法ONFAILURE改造から来ている:や公共AppInitializationがある私は、このエラーを持って、なぜ試みる/キャッチが動作しない理由を

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

+0

エラーがそのコードブロックで発生していないため、より多くのlogcatを答えに入れて、起点エラーを確認してください。 – diegoveloper

+0

@diegoveloperと同意する場合は、throwableオブジェクトがnullであるかどうかもチェックしてください –

+0

@diegoveloper –

答えて

0

NullPointerExceptionがヌル来..だから

書き込みさ

appInitialization

を発生するため、他の部分であなたのtry catchブロックを置く:

公共の静的な無効showGeneralErrors (スロー可){ Striメッセージ= "";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()); }}}

関連する問題