Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Problem with ArrayObject (Beginner Question)

1. ## Problem with ArrayObject (Beginner Question)

Hey, i have a problem with setting the ArrayObject,
if i use a normal Object it works, with an array it doesn't
well see for yourself.. would like some help =)
the Constructor must be without parameter or any setter-methodes...
i get the
at ProbeKlausur2.Person.main(Person.java:21)

Code:
```public class Person {

private String 	name;
private double 	groesse;
private double 	bmi;
private int 	gewicht;

public Person(){
}

public static void main(String[] args) {

Person[] p = new Person[5];
// if i >>use Person p = new Person();<< it works ...
int n = 0;

while(n<3){
p[n].name = EAM.rString();
p[n].groesse = EAM.rDouble();
p[n].gewicht = EAM.rInt();

p[n].bmi = p[n].gewicht / (p[n].groesse * p[n].groesse);
System.out.println(p[n].bmi);

n++;
}
}```

• Where are you instantiating each of the 5 people within the Person array?

• Man.., well i did say beginner question.. ARGH...

Code:
`p[n] = new Person();`
thx for the help ...

• Originally Posted by playn1990
Man.., well i did say beginner question.. ARGH...

Code:
`p[n] = new Person();`
thx for the help ...
You betcha, that was far more valuable than I telling you what the problem wasn't it
To actually answer the problem in detail - a creation of a `new Object[5];` array is simply telling it that the assigned variable will be 5 * sizeof(Object) in size. It doesn't yet have any data within it. This is the same with a standard definition of a variable: `Person p;`, where we know that 'p' is reserved for a 'Person', but we don't yet have a person in there. The call to the `new Object();` is what creates an actual object; `new Object[4];` differs in that it is not instantiating the object, just allocating for it.

More often than not this is what you well see is the cause of a NullPointerException. When you try to access a property or method of an un-instantiated object. We know that the object is referenced by the variable, but we don't know when it gets there (could be manipulated externally from a different class). This is why a compiler isn't to choke on this error, and you get a runtime exception instead.

• ## Users who have thanked Fou-Lu for this post:

playn1990 (01-06-2011)

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•