[英]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?什么是
NullPointerException
以及如何避免这种情况? 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.问题是当你创建一个Student对象时,你需要初始化每个成员对象,即
department
对象为null,所以当你执行department.setDid(101)
,它会返回一个异常。
To fix this, create a custom constructor for the Student class as so:要解决此问题,请为 Student 类创建一个自定义构造函数,如下所示:
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.但是,请注意,您必须对 String 类型执行此操作,因为它们的默认值是
null
,而不是""
,这可能会在以后导致问题。
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();在你的部门类中,你没有创建部门的实例,部门字段为空,而是使用这个:department = new Department();
public void depart(int departid, String departname){
department = new Department();
department.setDid(departid);
department.setDname(departname);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.