• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

arrays for objects of class

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13081
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it !!! Thanks everybody.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
It's weird that we cook bacon and bake cookies. Eat this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic