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 arrays for objects of class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays for objects of class" Watch "arrays for objects of class" New topic

arrays for objects of class

midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
I have created an array for object v. ie value[] v=new value[5]; and then i call the method getval of value class like this: v[0].getval()
but this is showing an error and that is
"Exception in thread "main" java.lang.NullPointerException at obj_array.main("

the line 38 is v[0].getval()

what is wrong? here is my code

[Added code tags - see UseCodeTags]
Greg Brannon

Joined: Oct 24, 2010
Posts: 563
Please UseCodeTags <-- press on link for instructions.

You declared an array of value objects at line 36 32:

value[] v = new value[5];

but you didn't initialize any of the array's contents, as in:

v[i] = new value();

So the array is filled with nulls, hence the NPE.

Other points:

In Java, by convention, class names are capitalized.
Underscores are seldom used in Java names, rather "camel casing" is employed to distinguish parts of names. For example, your obj_array would be better titled, ObjArray.

Edit: Line numbers shifted with code tags (I think).

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Matthew Brown

Joined: Apr 06, 2010
Posts: 4544

Hi Midhuna. Welcome to the Ranch!

What you've done there is create an array that can contain value objects, but you haven't actually created any value objects. All the entries are initialised to null to begin with. Think of it like an eggbox that doesn't contain any eggs yet.

Which means when you try and call getval() or display() on v[0] or v[1], you're calling them on null, which gives that exception.

By the way, I'd suggest getting into the habit of following the standard Java conventions. And that means starting your class names with a capital letter: so Value instead of value.
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Array elements are not initialized, they are null, just the array is declared.
So after declaration you can put for loop to initialize each array element by calling default constructor of value class. After you can invoke method on each element of an array.

The quieter you are, the more you are able to hear.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

value[] v=new value[5];

This is saying "Give me an egg carton that can hold five eggs". That is what you get - an empty carton. You need to fill it up with eggs/objects before you can actually use the eggs.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
midhuna peru
Ranch Hand

Joined: Aug 24, 2012
Posts: 48
Got it !!! Thanks everybody.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

just like to add something, array elements are intialized to their default values , whether it is declared as instance variable or local varialbe. also if you Dog class which is subclass of Animal then the statment

Animal[] a = new Dog[5];

this will create a reference a of the type Animal array which will refer to Animal array object. basically this will create a CONTAINER with 5 compartments in it. each of those compartments initially will be null, since that is the default value of objects. these 5 compartments will hold 5 reference of the TYPE ANIMAL, and each will point to Dog object. now a question could pop in your mind what if you add Cat(which IS-A Animal) to the array. you will get ArrayStoreException at runtime .
I agree. Here's the link:
subject: arrays for objects of class
It's not a secret anymore!