2014-09-26 61 views
-1

嗨,我得到以下錯誤:NumberFormatException的錯誤

2014-09-26T14:17:40.779-0300|Grave: 'java.lang.NumberFormatException' recebido ao invocar escuta de a��o '#{comentario.cancelarAtendimento}' para o componente 'j_idt140' 
2014-09-26T14:17:40.780-0300|Grave: java.lang.NumberFormatException: For input string: "" 

我創造了這個格式化:

DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale("pt", "BR")); 

,我想以檢索從會話這樣的日期:

if (sessao.getAttribute("dataInicial") != null) 
     { 

      dataInicial = (String) sessao.getAttribute("dataInicial"); 


      dataIni = new java.sql.Date(formatter.parse(dataInicial).getTime()); 

     } 

然後我通過dataIni這裏

ocorrencias = cadastradorOcorrecia.pesquisarAvancada(usuario.getCodigo(), Integer.parseInt(pesqCodigo), pesquisaCliente, pesquisaStatus, pesquisaDepartamento, pesquisaSolicitante, pesquisaUltimoAtend, pesquisaSistema, dataIni, dataFi, pesquisaCriador,Integer.parseInt(pesquisaProduto),Integer.parseInt(pesquisaModulo)); 

在這裏,我得到了NumberFormatException異常--->誰能有什麼i'm做錯了的想法?

在此先感謝

,然後將其轉到DAO實例retrive與數據庫中的信息。

+0

什麼是'dataInicial'的價值?空字符串? – gtgaxiola 2014-09-26 17:39:02

+0

在這種情況下爲空 – marco 2014-09-26 17:40:41

+0

如果它爲空格式化程序將給出NullPointerException ..如果它是空的,它將是ParseException ..你得到一個NumberFormatException – gtgaxiola 2014-09-26 17:41:46

回答

1

的的Integer.parseInt拋出NumberFormatException的,如果它不能將字符串轉換爲整數。在你的情況,我會檢查pesqCodigo,pesquisaProduto & pesquisaModulo的值。

的JavaDoc Integer.parseInt(String s)

例子

Integer.parseInt(" 234"); ---> Throws NumberFormatException as it has space 
Integer.parseInt(""); ---> Throws NumberFormatException as it is not parseable for an integer 
Integer.parseInt(null); ---> Throws NumberFormatException and not npe 
Integer.parseInt(" 234 ".trim()); ---> Doesnt throw NumberFormatException as it trims before trying to parse it. 

編輯:

這並不奇怪,這是行不通的。這可能會更好地工作:

SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy", 
              Locale.ENGLISH); 

然後用您所需的格式打印您需要第二個SimpleDateFormat的:

Date parsedDate = sdf.parse(date); 
SimpleDateFormat print = new SimpleDateFormat("MMM d, yyyy HH:mm:ss"); 
System.out.println(print.format(parsedDate)); 

注: 你應該包括語言環境,如果你的語言環境不是英語時,日名稱可能不被識別 IST是模糊的,並可能導致問題,所以你應該在你的輸入使用,如果可以正確的時區的名稱。

+0

現在我得到了2014-09-26T15:00:32.178-0300 | Grave:java.text。ParseException:無法解析的日期:「」 – marco 2014-09-26 18:04:29

+0

@marco請閱讀Java文檔進行日期解析,瞭解爲何出現此錯誤,解決問題的速度要比繼續詢問SO快得多。檢查編輯的答案。 – StackFlowed 2014-09-26 18:06:50

0

pesqCodigopesquisaProdutopesquisaModulo中的至少一個是不表示數字的字符串。

我覺得跟dataInicial沒有關係,因爲DateFormat不應該扔NumberFormatException。 Integer.parseInt()方法可以拋出這個異常,所以只需看看該函數的用法。

+0

2014-09-26T15:00:32.178-0300 | Grave:java.text.ParseException:Unparseable date:「」 – marco 2014-09-26 18:05:57

+0

你在你的問題中說你有一個NumberFormatException ...:「這裏我得到了numberFormatException --- >任何人都可以知道我做錯了什麼嗎?「。這是問題的標題! – eternay 2014-09-26 18:08:24