aspose file tools*
The moose likes Struts and the fly likes DynaValidatorForm - Double fields are never null! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "DynaValidatorForm - Double fields are never null!" Watch "DynaValidatorForm - Double fields are never null!" New topic
Author

DynaValidatorForm - Double fields are never null!

Catalin Mihalache
Greenhorn

Joined: Oct 16, 2004
Posts: 16
Hello,

I have a DynaValidatorForm with field salary with type Double. Sometimes, the user types nothing in that field (isn't interested by that field) but after the validation process on server I get 0.0 as salary's value. Why ? How can I prevent this ? Is there a hidden setting for validator plug-in ?

Thanks
Cata


Regards,<br />Catalin Mihalache<br /> <br />SCJP 1.5<br />SCWCD 1.4<br />SCDJWS 1.4
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Since this is a Struts question, I'm moving to the Web Application Frameworks forum.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Maybe it is the Java itself feature, the default value for double.


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
It should be the default value of double. Just like other types. For example, for objects, they are set to null initially if nothing to set. For numeric data types, they are either 0, or 0.0d. The primitive data types cannot be null because they are NOT objects!

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

I have a DynaValidatorForm with field salary with type Double

By the way, what do you mean by type Double? Is it the wrapper object or primitive data type?

If it is the latter case, the default value should be null, NOT 0.0d.

Nick
Sheldon Fernandes
Ranch Hand

Joined: Aug 18, 2004
Posts: 157
Originally posted by Catalinescu Mihai:
I have a DynaValidatorForm with field salary with type Double. Sometimes, the user types nothing in that field (isn't interested by that field) but after the validation process on server I get 0.0 as salary's value. Why ? How can I prevent this ? Is there a hidden setting for validator plug-in ?


Actually the setting of 0.0 for Double attributes has nothing to do with the validator framework. It is the struts framework which sets 0.0 for the attribute when it tries to populate the form bean from the request parameters.

I remember there being an init-param of the ActionServlet through which you can turn off this behaviour, i don't remember the name. It would then leave it as "null".

But there is a problem using "Double"s as attributes of form beans. If the user enters a non numeric value, the framework tries to convert it to Double, it fails, and it sets 0.0. Now, if you want to show the user his error, you end up with 0.0 and not the actual value entered.

The best way to get roung this is to keep all form attributes as "String" and once validation is completed you can convert it to the desired type for your use. The conversion in this case would not fail as validation is already completed.

Sheldon Fernandes
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DynaValidatorForm - Double fields are never null!