2014-11-03 59 views
0

我一直在这个挠我的头一阵子..不知道该怎么办。Nullpointerexception当使用parseDouble

String tempprice = null; 
String findPriceCommand = "SELECT sellingprice FROM `item` WHERE itemNo = '" + itemNo + "'"; 
try 
{ 
    tempprice = viewValue(conn, findPriceCommand); 
} 
catch (SQLException e) 
{ 
    e.printStackTrace(); 
} 
Double price = Double.parseDouble(tempprice); 
Double temp = (quantity.get(count) * price); 
finalprice.add(temp); 

的所有IM试图做到这一点得到通过货号数据库的价格。由于即时获得这个值作为一个字符串值,我使用Double price = Double.parseDouble(tempprice);将其更改为一个双值。 这一切工作,我转身前finalpriceArrayList(这样我可以同时插入多个值。(上面给出的代码是一个循环...的一部分)反正现在即时得到一个NullPointerException

就像这样:

int count = 0; 
     if (action.getSource() == btnAdd) 
     { 
     //other ArrayList variables 
     ArrayList<Integer> quantity = new ArrayList<Integer>(); 
     ArrayList<Double> finalprice = new ArrayList<Double>(); 
     //.....previous code... 

     count++; 
     } 

任何线索什么,我在这里失踪?/

+0

tempprice为null,这就是为什么你得到一个NullPointerException – ryekayo 2014-11-03 19:48:40

+0

代码中的循环在哪里? – manouti 2014-11-03 19:50:36

+4

您需要开始使用调试器 – Lovis 2014-11-03 19:50:42

回答

2

看一看的API文档:

http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#parseDouble%28java.lang.String%29

它说除其他事情之外,当s为NULL时,Double.parseDouble(String s)将抛出NullPointerException。

只是一个提示。当你得到任何异常时,请查看堆栈跟踪。它会显示你的NullPointerException首先出现在你的案例中的异常。从那里开始,只有很小的一步才能看到问题出在你的tempprice变量的某个地方。拿一个调试器,并通过你的代码来检查为什么tempprice是NULL。

+0

谢谢,我将阅读关于调试的教程..从来没有真正打扰过。 – Tsar 2014-11-03 20:13:22

+1

@ShifaTsar:这是使用Eclipse进行调试的良好介绍: http://www.vogella.com/tutorials/EclipseDebugging/article.html 如果您对程序设计非常感兴趣,那么您必须知道调试器是必须的将帮助您开发一个更好的模型,了解您的计划中发生了什么。祝你好运! – markus 2014-11-04 07:18:23

相关问题