aspose file tools*
The moose likes Beginning Java and the fly likes Empty Array as opposed to Null Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Empty Array as opposed to Null Array" Watch "Empty Array as opposed to Null Array" New topic
Author

Empty Array as opposed to Null Array

Thomas Paul Bigbee
Ranch Hand

Joined: Jun 28, 2005
Posts: 71
I'm used to using
if (thisString != null && thisString.length() > 0)

I'm getting back an empty array (from within a serialized object over Sockets), my question is this - how do I check for an empty array?

Example...

public static void main(String[] args) {
String[][] x = new String[0][0];
if ( x == null ) {
System.out.println("x is null");
} else {
System.out.println(x[0]);
// Crash and burn - null pointer exception
}
}
Thanks in advance
Tom
Henrique Boreg
Greenhorn

Joined: Aug 11, 2005
Posts: 23
x[0] does not contain a String object, it contains another array since this is a 2D array..to access a String object, you would have to access it using:


By the way, your code actually gives me an ArrayOutOfBoundsException, since the array is set to null, you could solve it by adding at least one element to it: String[][] x = new String[1][1];
[ August 20, 2005: Message edited by: Henrique Boreg ]

"I wrote an all-Java OS in 1998 but can't be sure if or how it works... it's still booting"
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Thomas,

It's late at night here, so take this with a grain of salt...

I tried out your code and got an ArrayIndexOutOfBoundsException, not a NullPointerException.

Would something like this work for you?




That uses the array's length property.


Pauline
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
There are two steps in creating and populating arrays.
First create an array object: String[] sa = new String[2]; // creates an array that CAN hold 2 String objects
Second add objects to it: sa[0] = "1"; // init first elem, sa[1] is still null
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> how do I check for an empty array

Every array has a length property: if( array.length > 0 ) ... not empty

Using empty collections and arrays versus null references is an excellent guideline to follow. It avoids a lot of needless null or empty checks:

for(int i : array) ...

or

for( int i = 0; i < array.length; i++) ...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Rick O'Shay:

Using empty collections and arrays versus null references is an excellent guideline to follow. It avoids a lot of needless null or empty checks:


Agreed. It even has a name: the Null Object design pattern.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Empty Array as opposed to Null Array