2016-10-19 91 views
0

我的代码是:NumberFormatException的解析字符串为BigDecimal

String sql="insert into L_EC(AMOUNT)values(?)"; 

    pst=con.prepareStatement(sql); 


    for(int i=0;i<dtm.getRowCount();i++){ 
    BigDecimal nbr= parsing(dtm.getValueAt(i, 1).toString()); 
    prst.setBigDecimal(1, nbr); 
    prst.addBatch(); 
} 
prst.executeBatch(); 

信息: 列的类型的Currency

private static BigDecimal parsing(String str) { 
    String normalized=""; 
    if(str==null){ 
     normalized="0"; 
    }else{ 
    normalized = str.replaceAll("\\s", "").replace(',', '.'); 
    } 
    return new BigDecimal(normalized); 

问题是,当从表中的单元格包含NULL,我得到这个消息:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException 
    at java.math.BigDecimal.<init>(BigDecimal.java:545) 
    at java.math.BigDecimal.<init>(BigDecimal.java:739) 
    at Vista.TCRCG.parse(TCRCG.java:130) 

我认为问题是在同等水平当解析器找到一个没有任何文本或值的单元格时使用sing方法。

+1

请包括完整的异常堆栈跟踪 –

+0

我有这个功能,我的代码: 私有静态BigDecimal的解析(字符串str){ 字符串标准化=“”; if(str == null){normalized =“0”; } else { normalized = str.replaceAll(“\\ s”,“”).replace(',','。'); } return new BigDecimal(normalized); – Squero27

+0

请勿使用评论,**编辑**您的问题。并用完整的异常消息发布堆栈跟踪。 –

回答

1

如果str是空字符串("")或空格(" "),则parsing方法将抛出该异常。你可能需要做一些额外的检查,例如,

if (str == null || str.trim().length() == 0) { 
    normalized = "0"; 
+0

编辑代码与您的答案后,它完美的工作,但是当我想要在数据库中插入数据时,我得到此消息: 线程线程0异常java.lang.arrayindexoutofboundsexception:10 – Squero27

+0

最后,它运作良好,在查询时忘记了一个专栏名称,谢谢@Gord Thompson对你的帮助 – Squero27