• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Empty Array as opposed to Null Array

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> 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++) ...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic