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
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