Problem in implementing an array of objects

  • Thread starter Paulo Meneghelli Jr
  • Start date
P

Paulo Meneghelli Jr

Hello,

I am new at programming in Java and I would like to ask you about a
problem which I have been facing for a whole week with no progress in
it.
The objective is to build an array of objects of a class. The code I
wrote succeeds in declaring the array, but when I try to use it, this
message appears:

Exception in thread "main" java.lang.NullPointerException
at ArrayOfAClass.main(0010b_Exemplo.java:28)

I believe this means there is a pointer pointing to no object
(ItemMyClass[0] to ItemMyClass[9]) but I still didn´t find a way to
associate it to an object.

The program is the following:

/* My purpose in this program is to build an array of objects of
MyClass */
class MyClass {

int IntValue = 0;

public void SetValue (int IntValue) {
this.IntValue = IntValue;
}

public int IntValue () {
return this.IntValue;
}

}

class ArrayOfAClass {

/* The method main is the method which should create the array */
public static void main (String[] args) {

/* Here the intention is to build an array of objects of
MyClass */
MyClass[] ItemMyClass = new MyClass[10];

for (int i = 0; (i < 10); i++) {
/* The next line seems to be the point where the error
happens.
** It seems that the pointer ItemMyClass is null. How to
initialize it?
*/
ItemMyClass.SetValue(i);
System.out.println(ItemMyClass.IntValue);
}
}
}

As I am learning Java, I would like to know whether this way is
completely wrong or if there is just few lines lacking in it for
working properly. I would like to know alternative ways to solve the
problem of creating an array of objects too.

So, any idea is welcome.

Thanks in advance,

Paulo Meneghelli Jr
 
J

Jarmo

Paulo Meneghelli Jr said:
The objective is to build an array of objects of a class.

MyClass[] ItemMyClass = new MyClass[10];

for (int i = 0; (i < 10); i++) {
ItemMyClass.SetValue(i);
System.out.println(ItemMyClass.IntValue);


Your code creates an array without any elements in it. It has allocated
space to store 10 references but doesn't actually allocate the objects to be
referenced.

See
http://www.ictp.trieste.it/~manuals/programming/Java/tutorial/java/data/arraysOfObjects.html for more info.
 
P

Pat Ryan

You need to create instances of MyClass to put into the array, before you
access them.

MyClass[] ItemMyClass = new MyClass[10];

will initialise the array elements to null

To load the array with objects, you need to do something like

for (int i = 0; (i < 10); i++) {
ItemMyClass = new MyClass();
}

- then you'll be able to access the objects
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,610
Members
45,255
Latest member
TopCryptoTwitterChannels

Latest Threads

Top