File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Setting up an array 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 "Setting up an array of objects" Watch "Setting up an array of objects" New topic

Setting up an array of objects

Joe Lank

Joined: Jun 08, 2004
Posts: 9
I'm having trouble accessing arrays of objects, probably something wrong with my syntax declaring the array.

This gives a java.lang.NullPointerException error. Any help is greatly appreciated.
[ June 08, 2004: Message edited by: Joe Lank ]
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
When you initiate the Student1 array, you are creating a new Student1[] (array) object, you are not creating any Student1 objects, and since all array objects (or primitives) are assigned there default value, each Student1 object in the Student1[] array object is set to null. Try:

I hope this makes sense.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

Hi Joe,
Welcome to JavaRanch� A quick administrative note, it is generally a lot easier if you place any sample code in UBB [code] [/code] tags. It makes reading the code much easier. Take a look hereto learn how to use UBB�

In addition to Jason�s comment, there is another "problem" in your code that should be pointed out so you do not confuse yourself about something. Looking at your code with line numbers for easy referencing:

The code on lines 3-6 is not actually a constructor, as you indicate in your System out comment; based on the way you have it defined, it is a method. Constructors must be named the same as the class and do not have a return type, not even void. I think you just made a typo since you have similarly named classes, Student and Student1. To be a constructor, you need to change line so the name matches the class, and remove the return type:

This will now output the comment when you construct an instance of Student1 using the syntax Jason gave you.

Previously, a Student1 object could still be constructed since by default, all objects have a no argument constructor such as this:

If we change lines 3-6 to be a constructor instead of a method named "Student", it will make line 14 syntactically incorrect if your intent was to call the constructor since you do not call constructors with dot-notation. As Jason indicates, you need to use the keyword new.

If you then wanted to call a method on the object, you use the dot-notation on any methods you have defined. Since having a method named Student inside the class Student1 which is used in the Class Student is rather confusing, let�s instead add a method named sayHello to the Student1 class so you can see how to call a method. So if we take all the above along with Jason�s comments, we would have this:

This should run and give you the output:

Call to constructor complete.
sayHello method called

As you continue to read about Array�s, you should learn how to iterate through the entire array and place an object in each position.

Hope that helps
Joe Lank

Joined: Jun 08, 2004
Posts: 9
Wow! I can't thank you both enough! And thanks for clearing up about methods and constructors.
I agree. Here's the link:
subject: Setting up an array of objects
It's not a secret anymore!