cancel
Showing results for 
Search instead for 
Did you mean: 

Validation of field values in Java

Former Member
0 Kudos

Hi Everybody,

I have a issue with checking the field values entered in a form in WD Java. The values entered in the form are submitted by pressing a button (onAction). Furthermore, the an action is assigned to onEnter of the field. Both, the button (onAction) and the field itself (onEnter), have the same action assigned. First of all we do a check on the values entered in this action. When the field does contain any values, everything works fine, that means the checks (format, length, etc.) are done properly. But there are two situations where I get a NullPointerException and really do not no how to get rid of it:

(1) The cursor is placed in an input field, nothing is entered, and enter key is pressed.

(2) One of the checks was not succesfull and an error message is shown. When the values are now deleted from the field and enter is pressed, the NullPointerExeption is shown again.

The NullPointerException is caused by the following if-statement:

if (strTPersnr.equals(""))

For me it looks like as soon as the cursor was placed in the field, the field value is not blank any more. I also tried to check the length of the field but this does not work either.

I am quite confident I missed something, but I cannot see the wood for the trees anymore. Could someone please help??

Cheers,

Steph

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Stephan,

if you get a NullPointerException at this line, then the value of strTPersnr is NULL.

To test against NULL or empty string you can write for example

if (null == strTPersnr || strTPersnr.trim().length() == 0)
{
  /* empty */
}
else
{
  /* not empty */
}

Another possibility is to create a new DDIC type with length constraint > 0, then the framework performs this check automatically.

Armin

Answers (3)

Answers (3)

sridhar_k2
Active Contributor
0 Kudos

Hi Stephan,

NullPointerException you will get, when you try to compare Null with something. In your code (strTPersnr.equals("")) it gives you NullPointerException.

Before comparing it with something, Check with null.

like,

ex:- String strTPersnr= wdContext.getName();

if(strTPersnr!= null ){

if (strTPersnr.equals(""))

}

Regards,

Sridhar

Former Member
0 Kudos

No, unfortunaltely this does not work out neither ... any other ideas?

Former Member
0 Kudos

Hi,

Ok try this,

if(!strTPersnr.equals("") || strTPersnr!=null)

{

//put your validation code here.

}

else{

// show error message;

}

Former Member
0 Kudos

Hai,

try this code

if(strTPersnr.equals("") || strTPersnr==null)

instead of if(strTPersnr.equals(""))

hope it will help

regards

Former Member
0 Kudos

Hi,

If(strTPersnr.equals("") || strTPersnr==null||strTPersnr.equals("null"))

else

{

proceed normally

}

Regards

Saravanan K