[英]Object returning null from hashmap
Hi I'm working on a Uni assignment and I currently have a class called Program
that stores a hashmap
called courses
. 嗨,我正在处理Uni作业,目前有一个名为
Program
的类,该类存储一个称为courses
的hashmap
。
inside the Program
class I can easily return objects from the Hashmap
but when I try to retrieve a Course
object from the Hashmap
in another class University
it always returns null
. 在
Program
类中,我可以轻松地从Hashmap
返回对象,但是当我尝试从另一个类University
的Hashmap
中检索Course
对象时,它总是返回null
。
a snippet of my code: 我的代码片段:
Program Class 课程类别
private Map <String, Course> courses = new HashMap<String, Course>();
public void addCourse(Course newCourse) throws ProgramException {
setCode(newCourse.getCode());
courses.put(newCourse.getCode(), newCourse);
}
public Course getCourse(String courseID){
setCode(courseID);
return courses.get(courseID);
}
University Class 大学班
public void enrollIntoCourse(String courseID) {
studentKey = student.getFullName();
course = program.getCourse(courseID);
// print out Course Object to check. But it = null!!
System.out.println(course);
enrolled.put(studentKey, course);
}
Basically I am trying to get a Course Object
from the courses
Hashmap
to store in another Hashmap
that holds Course Objects
but with a different key. 基本上,我试图从
courses
Hashmap
获取一个Course Object
,以存储在另一个包含Course Objects
但具有不同键的Hashmap
中。
I would check that you have properly initialised program
and have added courses to it. 我会检查您是否正确初始化了
program
,并向其中添加了课程。 If you are confident that you've done that, could you show us the code that does so? 如果您确信自己已完成此操作,能否向我们展示执行此操作的代码?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.