Seeing if a string equals Null  
Author Message
BlackJackal





PostPosted: 2007-2-8 4:22:00 Top

java-programmer, Seeing if a string equals Null How can you test a string to see if it is null? I have tried the
following code but it throws a nullpointerexception.

for (int i = 0; i < names.length; ++i) {
if (names[i].equals("null")) {
nullcount += 1;
}

 
John W. Kennedy





PostPosted: 2007-2-8 4:43:00 Top

java-programmer >> Seeing if a string equals Null BlackJackal wrote:
> How can you test a string to see if it is null? I have tried the
> following code but it throws a nullpointerexception.
>
> for (int i = 0; i < names.length; ++i) {
> if (names[i].equals("null")) {
> nullcount += 1;
> }

You seem to be missing some /very/ important basic concepts of Java (and
almost any other programming language devised since the mid 1960s). What
you are doing in the code above is the rough equivalent of trying to
count the empty spaces in a parking lot by looking at the parked cars
and counting how many have license plate "EMPTY".

Null means there's no String there. The only way to test for it is
if (names[i]==null)

What you're trying to do now is look at the String in names[i] and
compare it to a String containing "null". But where there is no String
in names[i], the "equals" method will fall down dead, because it's
trying to compare nothing to "null", and you can't do anything with nothing.

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
 
BlackJackal





PostPosted: 2007-2-8 4:53:00 Top

java-programmer >> Seeing if a string equals Null Thank you so much for the help! I know my programming leaves much to
be desired but I hope to get better. I am working toward my CS degree
at Troy State online so I am having to teach myself for the most part
since there are no structured classes. Google groups has been a
lifesaver to me since I started programming last August and without it
I would never figure this stuff out so I really appreciate the input.


 
 
Lew





PostPosted: 2007-2-8 6:00:00 Top

java-programmer >> Seeing if a string equals Null BlackJackal wrote:
> I am working toward my CS degree...

The key to the
if ( something.equals( "null" ))

issue is the incredibly stupid, compulsive nature of the computer. It takes
every quote mark, curly brace, parenthesis, period, semicolon, etc., into account.

When you place source characters inside of quote marks, you are signaling the
compiler that the contents will not be a keyword but a String literal. A
keyword will not use quote marks around it.

It is the same with other keywords. If you quoted the "if" in that command you
would have trouble, too.

Incidentally,
something.equals( null )
is a perfectly valid expression. For any non-null "something" it will always
return false. If "something" is null at the time of evaluation, then the
expression will throw a NullPointerException, which is more inconvenient than
using ==.

- Lew
 
 
trippy





PostPosted: 2007-2-8 12:42:00 Top

java-programmer >> Seeing if a string equals Null In article <email***@***.com>,
BlackJackal took the hamburger meat, threw it on the grill, and I said
"Oh Wow"...

> How can you test a string to see if it is null? I have tried the
> following code but it throws a nullpointerexception.
>
> for (int i = 0; i < names.length; ++i) {
> if (names[i].equals("null")) {
> nullcount += 1;
> }
>
>

lose the quotes

if (names[i].equals(null))

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "The American Way" -- Sacred Reich

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"



 
 
Ingo R. Homann





PostPosted: 2007-2-8 17:38:00 Top

java-programmer >> Seeing if a string equals Null Hi,

trippy wrote:
>>How can you test a string to see if it is null? I have tried the
>>following code but it throws a nullpointerexception.
>>
>>for (int i = 0; i < names.length; ++i) {
>>if (names[i].equals("null")) {
>>nullcount += 1;
>>}
>
> lose the quotes
>
> if (names[i].equals(null))

OMG, please do not give such wrong advices! Especially, since Lew just
explained what is wrong with this idea.

Ciao,
Ingo

 
 
phillip.s.powell@gmail.com





PostPosted: 2007-2-8 21:54:00 Top

java-programmer >> Seeing if a string equals Null On Feb 7, 3:22 pm, "BlackJackal" <email***@***.com> wrote:
> How can you test a string to see if it is null? I have tried the
> following code but it throws a nullpointerexception.
>
> for (int i = 0; i < names.length; ++i) {
> if (names[i].equals("null")) {
> nullcount += 1;
>
> }

Actually, to compare any object with null you have to compare only
this way:

if (names[i] == null) nullcount += 1;

Phil

 
 
trippy





PostPosted: 2007-2-9 11:33:00 Top

java-programmer >> Seeing if a string equals Null In article <45caef61$0$30326$email***@***.com>, Ingo
R. Homann took the hamburger meat, threw it on the grill, and I said "Oh
Wow"...

> Hi,
>
> trippy wrote:
> >>How can you test a string to see if it is null? I have tried the
> >>following code but it throws a nullpointerexception.
> >>
> >>for (int i = 0; i < names.length; ++i) {
> >>if (names[i].equals("null")) {
> >>nullcount += 1;
> >>}
> >
> > lose the quotes
> >
> > if (names[i].equals(null))
>
> OMG, please do not give such wrong advices! Especially, since Lew just
> explained what is wrong with this idea.
>
> Ciao,
> Ingo
>
>

Hi, null is a reserved keyword in java. Just thought you should know.

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "The American Way" -- Sacred Reich

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"



 
 
Ingo R. Homann





PostPosted: 2007-2-9 15:49:00 Top

java-programmer >> Seeing if a string equals Null Hi,

trippy wrote:
>>>if (names[i].equals(null))
>>
>>OMG, please do not give such wrong advices! Especially, since Lew just
>>explained what is wrong with this idea.
>
> Hi, null is a reserved keyword in java. Just thought you should know.

Then, you should have said that (and not give such a wrong advice)! ;-/

Ciao,
Ingo

 
 
trippy





PostPosted: 2007-2-9 16:16:00 Top

java-programmer >> Seeing if a string equals Null In article <45cc2789$0$5728$email***@***.com>, Ingo
R. Homann took the hamburger meat, threw it on the grill, and I said "Oh
Wow"...

> Hi,
>
> trippy wrote:
> >>>if (names[i].equals(null))
> >>
> >>OMG, please do not give such wrong advices! Especially, since Lew just
> >>explained what is wrong with this idea.
> >
> > Hi, null is a reserved keyword in java. Just thought you should know.
>
> Then, you should have said that (and not give such a wrong advice)! ;-/

Um, from Lew's post:

"Incidentally, something.equals( null )
is a perfectly valid expression."



--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Back For More" -- Ratt

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"



 
 
Gordon Beaton





PostPosted: 2007-2-9 16:54:00 Top

java-programmer >> Seeing if a string equals Null On Fri, 9 Feb 2007 02:16:24 -0600, trippy wrote:
> Um, from Lew's post:
>
> "Incidentally, something.equals( null )
> is a perfectly valid expression."

Valid is not the same as useful or even correct. Read the rest of his
post, the expression can *never* evaluate to true.

/gordon

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e