谁帮我看下这里为什么报空指针异常?
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
public Map<String,Student> students;
public MapTest(){
this.students=new HashMap<String,Student>();
}
public void put(){
Scanner console=new Scanner(System.in);
int i =0;
while(i<3){
System.out.println("请输入学生id:");
String ID=console.next();
Student st=students.get(ID);
if(st==null){
System.out.println("请输入学生姓名:");
String name = console.next();
Student newStudent = new Student(ID,name);
students.put(ID, newStudent);
System.out.println("成功添加学生:"+newStudent.id+":"+newStudent.name);
i++;
}else{
System.out.println("该学生ID已被占用!");
continue;
}
}
}
// 测试Map keySet方法
public void testKeySet(){
Set<String> keySet = students.keySet();
System.out.println("总共有:"+students.size()+"个学生");
for (String stuId : keySet) {
Student st=students.get(stuId);
if(st!=null){
System.out.println("学生:"+st.name);
}
}
}
public void testRemove(){
System.out.println("请输入要删除的学生ID:");
Scanner sc = new Scanner(System.in);
while(true){
String ID = sc.next();
if(students.get(ID) != null){
students.remove(ID);
System.out.println("成功删除学生:"+students.get(ID).name);
break;
}else{
System.out.println("输入的ID不存在!");
continue;
}
}
}
public void testEntrySet(){
Set<Entry<String,Student>> entrySet = students.entrySet();
for (Entry<String, Student> entry : entrySet) {
System.out.println("取得key:"+entry.getKey());
System.out.println("取得value"+entry.getValue().name);
}
}
public static void main(String[] args){
MapTest mt=new MapTest();
mt.put();
mt.testKeySet();
mt.testRemove();
mt.testEntrySet();
}
}
请输入学生id:
1
请输入学生姓名:
aaa
成功添加学生:1:aaa
请输入学生id:
2
请输入学生姓名:
bbb
成功添加学生:2:bbb
请输入学生id:
3
请输入学生姓名:
ccc
成功添加学生:3:ccc
总共有:3个学生
学生:aaa
学生:bbb
学生:ccc
请输入要删除的学生ID:
7
输入的ID不存在!
1
Exception in thread "main" java.lang.NullPointerException
at MapTest.testRemove(MapTest.java:54)
at MapTest.main(MapTest.java:79)