简体   繁体   中英

How to access a private object within a private object in Java?

I am trying to access an object within an object here. Below are the three classes. I simplified this that it makes the same error as in the full program. This is the main class.

import java.util.Scanner;
public class TestMain
{
    public static void main (String[] args) 
    {
        createStudent();
    }

    public static Student createStudent()
    {
        Student another = new Student();
        another.depart(101,"CS");
        return another;
    }
}

The second one,

public class Student
{
    private int sid;
    private String sname;
    private Department department;

    public int getSid()
    {
        return sid;
    }
    public String getSname()
    {
        return sname;
    }
    public void depart(int departid, String departname)
    {
        department.setDid(departid);
        department.setDname(departname);
    }

    public void setSid(int stusid)
    {
        this.sid = stusid;
    }
    public void setSname(String stusname)
    {
        this.sname = stusname;
    }

}

The third one,

public class Department
{
    private int did;
    private String dname;

    public int getDid()
    {
        return did;
    }
    public String getDname()
    {
        return dname;
    }

    public void setDid(int deptdid)
    {
        this.did = deptdid;
    }
    public void setDname(String deptdname)
    {
        this.dname = deptdname;
    }
}

No matter what I do, this program returns a run time error,

Exception in thread "main" java.lang.NullPointerException
    at Student.depart(Student.java:17)
    at TestMain.createStudent(TestMain.java:13)
    at TestMain.main(TestMain.java:7)

What is NullPointerException and how to avoid this? Please help me.

The problem is that when you create a Student object, you need to initialize each member object ie the department object is null, so when you do department.setDid(101) , it returns an exception.

To fix this, create a custom constructor for the Student class as so:

Student()
{
    department = new Department();
    sid = 0;
    sname = "";
}

Edit: As Sebastian has rightly pointed out in the comment below, it's actually pretty unnecessary to initialize primitive types in constructors. However, please note that you must do this for String types, as their default value is null , not "" , which could cause problems later on.

The exception is occurring because you did't create the object in the depart method. You can use this:

public void depart(int departid, String departname)
    {
        department = new Department();  
        department.setDid(departid);
        department.setDname(departname);
    }

in your department class in depart method you don't create instance of department and department field is null use this instead: department = new Department();

public void depart(int departid, String departname){
   department = new Department();
   department.setDid(departid);
   department.setDname(departname);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM