File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrays for objects of class" Watch "arrays for objects of class" New topic
Author

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(obj_array.java:38)"

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

what is wrong? here is my code



[Added code tags - see UseCodeTags]
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
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).


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

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
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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: 923
    
    1

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 .


OCPJP 6(100 %) OCEWCD 6(91 %) OCPJBCD(93%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arrays for objects of class
 
Similar Threads
Crazy date program
Date Comparison Program - Help!
bit values of an integer
integer bit values
Library System