HashMap报空指针,求帮助啊!!! public static void main(String[] args) {
StudentService studentService = new StudentService();
Student student=null;
for (int i=1; i<=10;i++){
if(i<5){
student = new Student(i,"student"+i,"s100"+i);
}
else{
student = new Student(i,"studnet"+i,"s200"+i);
}
studentService.addStudent(student);//执行到这边的时候报错了。
}
public class StudentService {
public HashMap<String, Student> students = null ;
public void StudnetService(){
students = new HashMap<String, Student>();
}
public void addStudent(Student student) {
if(student==null){
System.out.println("error!!!!!!!!!!");
}else{
System.out.println(student);
}
if(student.getStuName()==null){
System.out.println("error2222!!!!!!!!!!");
}else{
System.out.println(student.getStuName());
}
students.put(student.getStuName(), student);
}
}
定义了一个Student类
public class Student {
private String StuName;
private int stuNo;
private String ClassName;
public Student(int stuNo, String stuName, String ClassName ) {
super();
this.StuName = stuName;
this.stuNo = stuNo;
this.ClassName = ClassName;
}
public String getStuName() {
return StuName;
}
还有有其他参数的get和set就没贴了。
}