![](/img/trans.png)
[英]Android java.lang.NullPointerException inside try/catch
[英]try-catch block not suppressing java.lang.NullPointerException
我收到java.lang.NullPointerException,我不知道為什么。 我試圖使用try-catch塊但它不起作用。 任何人都可以解釋這個塊有什么問題嗎?
public PriceDataAdder(Connection conn) {
try {
this.conn = conn;
statement = conn.prepareStatement("INSERT INTO `blah` (a,b,c,d,e,f,g,h,`i`,j) VALUE( ?,?,?,?,?,?,?,?,?,? )");
} catch (SQLException ex) {
Logger.getLogger(PriceDataAdder.class.getName()).log(Level.SEVERE, null, ex);
}
}
嘗試捕獲Exception
而不是SQLException
。 它將起作用,因為Exception
是所有異常的超類,並且通過捕獲它,您將捕獲可能在try
塊內生成的所有可能的異常。
無論如何,問題可能是因為在調用prepareStatement()
conn
為null
,首先要確保在調用PriceDataAdder
方法時傳遞正確的Connection
實例。
另外,請查看正在生成的日志,應該充分利用Logger
對象。
您只捕獲SQLException
對象, NullPointerException
不是SQLException
的子類。 您可以插入NullPointerException|
抓住那些; 請參閱http://docs.oracle.com/javase/7/docs/technotes/guides/language/catch-multiple.html 。
您應該像下面的代碼一樣管理您的異常
try {
this.conn = conn;
statement = conn.prepareStatement("INSERT INTO `blah` (a,b,c,d,e,f,g,h,`i`,j) VALUE( ?,?,?,?,?,?,?,?,?,? )");
} catch (SQLException ex) {
Logger.getLogger(PriceDataAdder.class.getName()).log(Level.SEVERE, null, ex);
} catch (NullPointerException ex) {
Logger.getLogger(PriceDataAdder.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(PriceDataAdder.class.getName()).log(Level.SEVERE, null, ex);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.