Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Exception in thread "main" java.lang.NullPointerException
    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;
    
                    // EAM is a class for reading your input..
    		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++;
    		}
    	}
    Last edited by playn1990; 01-03-2011 at 08:53 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Where are you instantiating each of the 5 people within the Person array?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Man.., well i did say beginner question.. ARGH...

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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by playn1990 View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

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