Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

Rep: Reputation: 47
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");


I get this error obviously from inside this function:

Inside DeNull
at NumberType.DeNull(
at NumberType.FuncOO(
at NumberType.SecondStage(
at NumberType.<init>(
at stringMath.paint(
at sun.awt.RepaintArea.paint(
at sun.awt.motif.MComponentPeer.handleEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Container.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(
at java.awt.EventDispatchThread.pumpEventsForHierarchy(
at java.awt.EventDispatchThread.pumpEvents(
at java.awt.EventDispatchThread.pumpEvents(

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
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
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374

Original Poster
Rep: Reputation: 47
thanx, I figured that out almost immediately after asking, thanx again.
Old 02-21-2004, 01:39 PM   #4
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

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

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.




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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 PM.

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