LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-18-2004, 10:25 PM   #1
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Rep: Reputation: 46
java test if string in string array is null.


This should be all the code you need, tell me if you need more, but there is a lot..

public void DeNull(String[] INS)
{
System.out.println("Inside DeNull");
int Counter = 0;
while (Counter < 100)
{
if (INS[Counter].equals(null))
{
INS[Counter] = new String("Z");
}

Counter++;
}
}

I get this error obviously from inside this function:

Inside DeNull
java.lang.NullPointerException
at NumberType.DeNull(stringMath.java:344)
at NumberType.FuncOO(stringMath.java:235)
at NumberType.SecondStage(stringMath.java:143)
at NumberType.<init>(stringMath.java:91)
at stringMath.paint(stringMath.java:33)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at sun.awt.motif.MComponentPeer.handleEvent(MComponentPeer.java:405)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


so how would i properly test for a blank string and initilize it to a capital Z?

I already tried:
if (INS[Counter].equals(""))
it did not work.
 
Old 02-19-2004, 12:21 AM   #2
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
if (INS[Counter].equals(null))

That is the problem. If the INS[x] is null, you cannot call its method "equals" in the first place. That's why "NullPointer exception" occur.


The correct one is this,

if (INS[Counter] == null)
 
Old 02-19-2004, 12:28 AM   #3
exodist
Senior Member
 
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Gentoo
Posts: 1,372

Original Poster
Rep: Reputation: 46
thanx, I figured that out almost immediately after asking, thanx again.
 
Old 02-21-2004, 01:39 PM   #4
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
never call methods on a variable which could be null. For comparison, your code fragment should have read

Code:
if (INS[Counter] == null)
but if you were testing to see if it were equal to something else, it should have been this:

Code:
if ("aString".equals(INS[Counter]))
because the literal "aString" can never be null, it will always always be "aString". Keep this in mind and you'll save yourself loads of trouble with hard to read error messages and stack traces.

HTH

B.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# convert char array to string exodist Programming 3 09-16-2008 08:06 AM
String Array definiation problem nelnel Programming 1 09-16-2005 04:56 AM
Perl string replacement within an array? Seventh Programming 1 09-07-2004 02:50 PM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM
NullLogic Null Webmail Format String Vulnerability Aivukazz Linux - Security 1 10-09-2002 02:47 PM


All times are GMT -5. The time now is 08:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration