Weird null value problem

Hi,

I have this very weird problem (which I can't understand).

I have this applet that takes in values from the webpage.

private String[] collectParameters(){

String[] keys ={

"zero",

"one",

"two",

"three",

"four"

};

parameterArray =new String[keys.length];

for (int i = 0; i < keys.length; i++)

parameterArray[i] = getParameter(keys[i]);

return parameterArray;

}

Now parameter 3 might be a null value (i.e. ""), so I presume that it would have a null value.

Now this is the weird part. When I try to check if it is a null value, I keep getting very inconsistent answers.

I've try the following:

if (parameterArray[3] !=null)

//do whatever

if (!parameterArray[3].equals(null))

//do whatever

if (!String.valueOf(parameter[3]).equals(null))

//do whatever

but none of them work. I mean, if parameter 3 has no value some of them work and when it has a value they don't work (a null exception error occurs), which (I have to say) means that the if statement is not working.

And this code really is the weirdest

if (parameterArray[3] !=null)

//do whatever

it work on NetBeans but when I create the jar file and try to run it, I get a null exception error (this I really don't understand).

I've solved the problem by doing this

if (String.valueOf(parameter[3].length() > 4)

//do whatever

Which by itself is not elegant but hey it works.

I find it very odd. Is there a value stored in parameterArray[3] that I can't see because when I do a JOptionPane.showMessageDialog..., it come out empty (which doesn't mean it is null).

And the other thing is this, why if the length of parameter[3] = 4 when there is no value and >4 when there is?

What am I doing wrong?

Thanks.

Desmond

[2769 byte] By [sandridera] at [2007-11-27 8:57:32]
# 1
Hey Read teh problem breiflyHave u tried if (!parameterArray[3].equalsIgnoreCase( "" ))HTH:)
monk3ya at 2007-7-12 21:22:17 > top of Java-index,Desktop,Core GUI APIs...
# 2

Offhand, it sounds like in some environment, the empty parameter is being represented as a null, and in others it's being represented as an empty (zero-length) string.

Try a check like this:

if (parameterArray[3] != null && parameter[3].length() > 0) {

paulcwa at 2007-7-12 21:22:17 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks paulcw. It works now.
sandridera at 2007-7-12 21:22:17 > top of Java-index,Desktop,Core GUI APIs...