GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Arrays of Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays of Objects" Watch "Arrays of Objects" New topic
Author

Arrays of Objects

Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Lets say I have a class foo defined like this:
class foo {
String a;
String b;
public foo(String a, String b)
{
this.a = a;
this.b = b;
}
}
Now I would like to make an array of class foo like this:
foo myFoo[][];
How do I initialize this array and store foo objects in this it?
I've tried everything I can think of but I keep getting a NullPointerException.
Thanks,
Frank

Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
<PRE>
public class Test {
String a;
String b;
public void foo(String a, String b) {
this.a = a;
this.b = b;
}
public static void main (String[] args) {
Test t[] = new Test[5];
for (int i=0; i<t.length; i++) {
t[i] = new Test();
t[i].foo("A", "B");
}
}
}
</PRE>

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Surely Tony's code only builds and initializes a one-dimensional array, where the question calls for a two-dimensional array.
It looks as if the step you are missing is to call new for each "row" of the array. Here's my suggestion:


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Randi Meilach
Greenhorn

Joined: May 26, 2000
Posts: 11
Hi, I'm just starting out with java and played around with this problem. I have the following code which gives me an ArrayIndexOutOfBounds exception when I run it. Can anyone explain why?
class foo {
String a;
String b;
public static void main (String args[])
{
foo myFoo[][] = new foo [1][1];
myFoo[1][1] = new foo ("a", "b");
}
public foo(String a, String b)
{
this.a = a;
this.b = b;
}
}
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Your problem is that arrays start from 0 not 1, so any array with a size of 1 (created by new Something[1]) only has "slot" 0. To get a second "slot", numbered 1, you need to use new Something[2].
Randi Meilach
Greenhorn

Joined: May 26, 2000
Posts: 11
Frank,
Thanks, I realized that pretty much immediately after I posted. I am coming from a language where array indexing begins at 1 and I 'keep forgetting' this little fact.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Duane,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I think someone could best help you figure this one out if you were to post an example of the code that you are trying to use?
Don't forget to surround your code with the [ code ] and [ /code ] ubb tags.
Good Luck.
Duane Eddy
Greenhorn

Joined: May 30, 2002
Posts: 3
Thanks, but I answered my own question.
 
GeeCON Prague 2014
 
subject: Arrays of Objects