• 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

Arrays of Objects

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Desperado
Posts: 3226
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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>
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, but I answered my own question.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic