[resolvido]validar Integer em HibernateValidator

5 respostas
G

tenho a propriedade:

@Min(5)

<a class="mention" href="/u/max">@Max</a>(5)

private int codigo;

porém se eu digito no meu campo a, ou b ou c…recebo um erro…o Hibernate nao teria que fazer isso automaticamente, se ele ve que a propriedade é do tipo integer?

Como resolver esse problema?

<blockquote>Exception occurred during event dispatching:

java.lang.NumberFormatException: For input string: aat java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:447)

at java.lang.Integer.valueOf(Integer.java:553)

at GUI.DlgKunde.btnSpeichernActionPerformed(DlgKunde.java:270)

at GUI.DlgKunde.access$000(DlgKunde.java:24)

at GUI.DlgKunde$1.actionPerformed(DlgKunde.java:117)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6134)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)

at java.awt.Component.processEvent(Component.java:5899)

at java.awt.Container.processEvent(Container.java:2023)

at java.awt.Component.dispatchEventImpl(Component.java:4501)

at java.awt.Container.dispatchEventImpl(Container.java:2081)

at java.awt.Component.dispatchEvent(Component.java:4331)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)

at java.awt.Container.dispatchEventImpl(Container.java:2067)

at java.awt.Window.dispatchEventImpl(Window.java:2458)

at java.awt.Component.dispatchEvent(Component.java:4331)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:178)

at java.awt.Dialog$1.run(Dialog.java:1047)

at java.awt.Dialog$3.run(Dialog.java:1099)

at java.security.AccessController.doPrivileged(Native Method)

at java.awt.Dialog.show(Dialog.java:1097)

at java.awt.Component.show(Component.java:1447)

at java.awt.Component.setVisible(Component.java:1400)

at java.awt.Window.setVisible(Window.java:824)

at java.awt.Dialog.setVisible(Dialog.java:987)

at GUI.GuiKunde.btnNeuActionPerformed(GuiKunde.java:335)

at GUI.GuiKunde.access$300(GuiKunde.java:24)

at GUI.GuiKunde$4.actionPerformed(GuiKunde.java:283)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)

at java.awt.Component.processMouseEvent(Component.java:6134)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)

at java.awt.Component.processEvent(Component.java:5899)

at java.awt.Container.processEvent(Container.java:2023)

at java.awt.Component.dispatchEventImpl(Component.java:4501)

at java.awt.Container.dispatchEventImpl(Container.java:2081)

at java.awt.Component.dispatchEvent(Component.java:4331)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)

at java.awt.Container.dispatchEventImpl(Container.java:2067)

at java.awt.Window.dispatchEventImpl(Window.java:2458)

at java.awt.Component.dispatchEvent(Component.java:4331)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

BUILD SUCCESSFUL (total time: 13 seconds)</blockquote>

5 Respostas

marcelo.bellissimo

O que tem na linha 270 do seu arquivo DlgKunde.java ?

G

bom acho que ja achei o problema…estou fazendo Integer.valueOf(minha_string) e é claro que o Hibernate ainda nao trabalha ai…o jeito vai ser anular o digitos de letras!

G

Exception occurred during event dispatching: java.lang.NumberFormatException: For input string: "a" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:4 at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.valueOf(Integer.java:553) at GUI.DlgKunde.btnSpeichernActionPerformed(DlgKunde.java:270)

Na verdade esse erro acontece muito antes de chegar no hibernate validator.

Provavelmente na linha 270 da classe DlgKunde você possui algo como Integer.valueOf(xxxx). O problema de fazer isso é que você pode, por exemplo, receber um “a” como é no seu caso, e então o Java estoura uma NumberFormatException, já que “a” não é um número.

Você deve tratar esse código, algo como:

try { int xxx = Integer.valueOf(aquiVemOValor); } catch (NumberFormatException e) { // aqui você pode tratar exibindo, por exemplo, um JDialog dizendo "Não sou um número" }

Documentação do método: http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#valueOf(java.lang.String)

marcelo.bellissimo

Viu, nem precisei dar a resposta… era só ler o stackTrace:

java.lang.NumberFormatException: For input string: "a" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:4 at java.lang.Integer.parseInt(Integer.java:447) at java.lang.Integer.valueOf(Integer.java:553) at GUI.DlgKunde.btnSpeichernActionPerformed(DlgKunde.java:270) // <- aqui eu vi que era seu codigo, na linha 270 que estava disparando o erro

G

obrigado pessoal…

Criado 14 de maio de 2010
Ultima resposta 14 de mai. de 2010
Respostas 5
Participantes 3