堆棧跟蹤:爲什麼即使通過檢查也會得到NullPointerException?
java.lang.NullPointerException
at TestSlot.isInternetExplorerAvailable(TestSlot.java:274)
at WebProxyHtmlRendererBeta.getSingleSlotHtml(WebProxyHtmlRendererBeta.java:168)
// the rest is irrelevant
TestSlot#isInternetExplorerAvailable()
public Boolean isInternetExplorerAvailable() {
if (currentSession.get("isInternetExplorerAvailable") != null) // line 274
return (Boolean) currentSession.get("isInternetExplorerAvailable");
return false;
}
currentSession.get(String key)
是一個簡單的提取器從HashMap<String,Object>
去取...... getSingleSlotHtml()
else if (browserName.contains(BrowserType.IE) || browserName.contains(BrowserType.IEXPLORE))
if (!s.isInternetExplorerAvailable()) { // line 168
htmlClass += "browserNotAvailable ";
imgSrc = imgSrc.split(".png")[0].concat("_unavailable.png");
title = "browser not available";
} else { htmlClass += "browserAvailable "; }
可疑的事情是,是,我用這個SCCEE測試此相同類型的邏輯:
public class Main {
public static void main(String[] args) {
Map<String, Object> settings = new HashMap<String, Object>();
settings.put("something", true);
if (settings.get("something_else") != null)
System.out.println("it's not null");
else
System.out.println("it's null");
}
}
它放出來"it's null"
這意味着我可以用!=
做的空檢查。任何想法,爲什麼這不會在TestSlot#isInternetExplorerAvailable
方法爲我工作?
@Keppil有它正確的,如果currentSession爲空,你會得到嘗試調用currentSession.get –