Results 1 to 4 of 4

Thread: Compostion

  1. #1
    ehsan is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Compostion

    Hi Every one . I created two class Human and Employee and then create one object from human in side employee , but when run my program compiler return error
    Exception in thread "main" java.lang.NullPointerException
    at Employee.main(Employee.java:15)

    Java Code:
    public class Human{
    	private String name;
    	public void setName(String n){
    	 name = n;
    	}
    	public String getName(){
    		return name;
    	}
    }
    public class Employee{
    	private String id;
    	public Human h;
    	private void setEmployee(String id){
    		this.id = id;
    	}
    	private String getEmployee() {
    		return id;
    	}
    	
    	public static void main(String[] args){
    		Employee emp = new Employee();
            	emp.h.setName("ehsan");
    	}
    }
    Last edited by pbrockway2; 08-17-2011 at 11:14 PM. Reason: code tags added

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default

    you never initialized your human. You declared it with "public Human h;" But you have to initialize it before you can make reference to it, and the constructor is a good place to do that.

    Java Code:
    public class Employee{
    private String id;
    public Human h;
    
    public Employee(){
    h = new Human();
    }
    
    private void setEmployee(String id){
    this.id = id;
    }
    private String getEmployee() {
    return id;
    }
    
    public static void main(String[] args){
    Employee emp = new Employee();
    emp.h.setName("eshan");
    }
    }
    Last edited by sehudson; 08-17-2011 at 11:17 PM.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Please use code tags: put [code] at the start of the code and [/code] at the end. That way the code will be readable when it appears here.

    Exception in thread "main" java.lang.NullPointerException
    at Employee.main(Employee.java:15)
    This exception means that you are trying to use something as if it had a value when it is really null. Common cases look like

    Java Code:
    foo[i];  // exception when foo is null
    bar.baz(); // exception when bar is null
    I am guessing line 15 is

    Java Code:
    emp.h.setName("eshan");
    Check, by using System.out.println(), what the values of emp and h are. One of them is null! Once you have found the culprit (or by using sehudson's advice, but its good to check) search for whereever you thought you had assigned it a non null value and see why that didn't happen.

  4. #4
    ehsan is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Compostion
    By ehsan in forum New To Java
    Replies: 1
    Last Post: 08-16-2011, 07:17 PM

Posting Permissions

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