2017-05-16 65 views
-2

我想通過(通過setter)對象到另一個類來稍後做一些事情,但是當我調用這個對象方法時,我得到空指針異常。當傳遞對象的方法被調用時拋出空指針異常

當然,對象的值賦值比調用setFlexInfo函數要早。

Tab1Preview.java

DatabaseTierFlex dbFlex = new DatabaseTierFlex(getContext()); 
tier = dbFlex.getTier(summoner.getSummonerId()); 

jsInterface.setTierFlex(tier); // passing object 

jsInterface.callJavaScript(webView, "setServer", summoner.getServer()); 

WebAppInterface.java

private Tier tierFlex; // declaration 
private Context mContext; 
private NestedWebView webView; 

public WebAppInterface(NestedWebView webView, Context c) { 
     this.mContext = c; 
     this.webView = webView; 
    } 


public void setTierFlex(Tier tierFlex) { // Setter 
    this.tierFlex = tierFlex; 
} 


@JavascriptInterface 
public void setFlexInfo() { // method what I'm calling when I click a certain div 

    webView.post(new Runnable() { 
     @Override 
     public void run() { 
      // I'm getting NullPointerException here 
      callJavaScript(webView, "setWinsLosses", tierFlex.getWins(), tierFlex.getLosses()); 
      if (tierFlex.getSeriesTarget() != -1) { 
       callJavaScript(webView, "setPromo", tierFlex.getSeriesTarget(), tierFlex.getSeriesProgress()); 
      } 
      if (tierFlex.isFreshBlood()) { 
       callJavaScript(webView, "setEmblemFreshBlood"); 
      } 
      if (tierFlex.isVeteran()) { 
       callJavaScript(webView, "setEmblemVeteran"); 
      } 
      if (tierFlex.isHotStreak()) { 
       callJavaScript(webView, "setEmblemHotStreak"); 
      } 
     } 
    }); 
} 

public void callJavaScript(WebView view, String methodName, Object... params) { 
    StringBuilder stringBuilder = new StringBuilder(); 
    stringBuilder.append("javascript:try{"); 
    stringBuilder.append(methodName); 
    stringBuilder.append("("); 
    String separator = ""; 
    for (Object param : params) { 
     stringBuilder.append(separator); 
     separator = ","; 
     if (param instanceof String) { 
      stringBuilder.append("'"); 
     } 
     stringBuilder.append(param); 
     if (param instanceof String) { 
      stringBuilder.append("'"); 
     } 

    } 
    stringBuilder.append(")}catch(error){console.error(error.message);}"); 
    final String call = stringBuilder.toString(); 
    Log.i(TAG, "callJavaScript: call=" + call); 


    view.loadUrl(call); 
} 

編輯:堆棧跟蹤

05-16 22:56:38.481 27438-27438/COM。 example.krulkamyl.accountinfo E/AndroidRuntime:致命例外:main 進程:com.example.krulkamyl.accountinfo,PID:27438 java.lang.NullPointerException:試圖在空對象引用 處調用虛擬方法'int com.example.krulkamyl.accountinfo.model.Tier.getWins()' com.example.krulkamyl.accountinfo.interfaces.WebAppInterface $ 3.run(WebAppInterface.java:164) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java :95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6934) at java.lang.reflect.Method.invoke(Native Method) at java。 lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(調用JavScript(webView,「setWinsLosses」,tierSolo.getWins(),tierSolo.getLosses());

+1

這將是一個好主意,實際上我們展示的堆棧跟蹤... – jingx

+0

添加堆棧跟蹤 –

+2

你有沒有考慮'dbFlex.getTier(summoner.getSummonerId())的可能性;'是'返回null'? – Zircon

回答

1

NPE被拋入一個可能異步執行的runnable中。確保setter不會被調用參數爲null,這會導致tierFlex未被設置。 最終確定它可能是個好主意。這可能需要一些額外的重構。