File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays for objects of class

 
midhuna peru
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 563
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
midhuna peru
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it !!! Thanks everybody.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic