请老师指教,怎么避免空指针错误,我每次都是这个错误
package com.imooc.homework4; public class Student { private String stuNum; private String stuName; private float math; private float chinese; public Student() { } public Student(String stuNum, String stuName, float math, float chinese) { this.setStuNum(stuNum); this.setStuName(stuName); this.setMath(math); this.setChinese(chinese); } public String getStuNum() { return stuNum; } public void setStuNum(String stuNum) { this.stuNum = stuNum; } public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public float getMath() { return math; } public void setMath(float math) { this.math = math; } public float getChinese() { return chinese; } public void setChinese(float chinese) { this.chinese = chinese; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + Float.floatToIntBits(chinese); result = prime * result + Float.floatToIntBits(math); result = prime * result + ((stuName == null) ? 0 : stuName.hashCode()); result = prime * result + ((stuNum == null) ? 0 : stuNum.hashCode()); return result; } @Override public boolean equals(Object obj) { if(this==obj) { return true; } if(obj.getClass()==Student.class)// 判断类型 { Student stu = (Student)obj; return (stu.getStuNum().equals(stuNum)&&stu.getStuName().equals(stuName)); } return false; } @Override public String toString() { return "学生信息: 学号为" + stuNum + ", 姓名为" + stuName + ", 数学成绩为" + math + ", 语文成绩为" + chinese ; } }
package com.imooc.homework4; import java.util.ArrayList; import java.util.List; public class BanJi { private String classId; private String className; private List<Student> stuList = new ArrayList<Student>(); public BanJi() { } public BanJi(String classId, String className) { this.setClassId(classId); this.setClassName(className); stuList = new ArrayList<Student>(); } public String getClassId() { return classId; } public void setClassId(String classId) { this.classId = classId; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public List<Student> getStuList() { return stuList; } public void setStuList(List<Student> stuList) { this.stuList = stuList; } /** * 用于添加你想添加的学生。 * @param stu要添加的学生 */ public void addStudent(Student stu1) { //我们要排除重复添加的情况 boolean flag = false; for(Student stu2:stuList) { if(stu2.equals(stu1)) { flag = true; break; } } if(flag) { System.out.println("该学生已存在于学生列表中,请不要重复添加!"); } else { stuList.add(stu1); System.out.println("添加成功!"); } } /** * 用于通过学号查询学生信息 * @param stuNum我们想要查询的学生的学号。 * @return 我们查询的学生的信息 */ public Student searchStudentByNum(String stuNum) { Student stu1 = null; for(Student stu2:stuList) { if(stu2.getStuNum().equals(stuNum)) { stu1 = stu2; break; } } return stu1; } /** * 用于录入语文成绩。 * @param stuNum我们想要输入成绩的学生的学号 * @param score我们要录入的学生的成绩。 */ public void insertChineseScore(String stuNum,float score) { Student stu1 = searchStudentByNum(stuNum); if(stu1!=null) { stu1.setChinese(score); } else { System.out.println("没有找到此学生。"); } } /** * 用于录入数学成绩。 * @param stuNum我们想要输入成绩的学生的学号 * @param score我们要录入的学生的成绩。 */ public void insertMathScore(String stuNum,float score) { Student stu1 = searchStudentByNum(stuNum); if(stu1!=null) { stu1.setMath(score); } else { System.out.println("没有找到此学生。"); } } /** * 用于删除学生信息 * @param stuNum我们想要删除的学生的学号 */ public void deleteStudent(String stuNum) { Student stu1 = searchStudentByNum(stuNum); if(stu1!=null) { stuList.remove(stu1); } else { System.out.println("没有找到此学生。"); } } /** * 用于显示播放列表中的所有学生。 */ public void displayAllStudent() { System.out.println("班级中的所有学生为:"); for(Student stu:stuList) { System.out.println(stu); } } }
package com.imooc.homework4; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class School { Map<String, BanJi> banJiMap = new HashMap<String, BanJi>();//存放班级的集合。 public School() { banJiMap = new HashMap<String, BanJi>(); } public Map<String, BanJi> getBanJiMap() { return banJiMap; } public void setBanJiMap(Map<String, BanJi> banJiMap) { this.banJiMap = banJiMap; } /** * 用于向学校添加班级 * @param banji 我们要添加的班级 */ public void addBanji(BanJi banji) { banJiMap.put(banji.getClassName(),banji); System.out.println("添加成功!"); } /** * 用于删除我们想要删除的班级 * @param banji 我们想要删除的班级 */ public void deleteBanji(BanJi banji) { banJiMap.remove(banji.getClassName()); System.out.println("删除成功!"); } /** * 通过班级名称来查询班级 * @param className 我们需要查询的班级的名称 * @return 我们查询的班级。 */ public BanJi searchByName(String className) { BanJi bj = null; Set<String> banJiListSet = banJiMap.keySet(); for(String s:banJiListSet) { if(s.equals(className)) { bj = banJiMap.get(s); break; } } return bj; } /** * 用于对语文成绩进行排序 */ public void sortChineseByAverage() { BanJi bj = null; Map<String, Float> scoreMap = new HashMap<String, Float>(); Set<String> classIdSet = banJiMap.keySet(); boolean flag = false; for(String id : classIdSet) { bj = this.searchByName(id); if(bj.getStuList().isEmpty()) { System.out.println(bj.getClassName()+"中没有学生,请先添加"); flag = true; } else { float sum = 0.0f; int i = 0; List<Student> studentList = bj.getStuList(); Iterator<Student> it = studentList.listIterator();//迭代求平均 while(it.hasNext()) { i++; sum = sum + it.next().getChinese(); } float ave = sum/i; scoreMap.put(bj.getClassName(), ave); } } if(!flag) { List<Map.Entry<String, Float>> lm = new ArrayList<Map.Entry<String, Float>>(scoreMap.entrySet()); Collections.sort(lm, new Comparator<Map.Entry<String, Float>>() { @Override public int compare(Entry<String, Float> o1, Entry<String, Float> o2) { return o2.getValue().compareTo(o1.getValue()); } }); System.out.println("各班语文成绩按平均数排列为:"); for(Map.Entry<String, Float> me : lm) { System.out.println(me.getKey()+"的语文平均分为:"+me.getValue()); } } } public void sortMathByAverage() { BanJi bj = null; Map<String, Float> scoreMap = new HashMap<String, Float>(); Set<String> classIdSet = banJiMap.keySet(); boolean flag = false; for(String id : classIdSet) { bj = this.searchByName(id); if(bj.getStuList().isEmpty()) { System.out.println(bj.getClassName()+"中没有学生,请先添加"); flag = true; } else { float sum = 0.0f; int i = 0; List<Student> studentList = bj.getStuList(); Iterator<Student> it = studentList.listIterator();//迭代求平均 while(it.hasNext()) { i++; sum = sum + it.next().getMath(); } float ave = sum/i; scoreMap.put(bj.getClassName(), ave); } } if(!flag) { List<Map.Entry<String, Float>> lm = new ArrayList<Map.Entry<String, Float>>(scoreMap.entrySet()); Collections.sort(lm, new Comparator<Map.Entry<String, Float>>() { @Override public int compare(Entry<String, Float> o1, Entry<String, Float> o2) { return o2.getValue().compareTo(o1.getValue()); } }); System.out.println("各班数学成绩按平均数排列为:"); for(Map.Entry<String, Float> me : lm) { System.out.println(me.getKey()+"的数学平均分为:"+me.getValue()); } } } /** * 用于显示学校中所有的班级的名称。 */ public void displayBanJiName() { System.out.println("学校中的所有班级为:"); Set<String> banJiList = banJiMap.keySet(); for(String banJi:banJiList ) { System.out.println(banJi); } } }
package com.imooc.test; import java.util.InputMismatchException; import java.util.List; import java.util.Scanner; import java.util.Set; import com.imooc.homework4.BanJi; import com.imooc.homework4.School; import com.imooc.homework4.Student; public class TestDemo { public void testDemo() { Student stu1 = new Student("13号", "张三", 78f, 82.5f); Student stu2 = new Student("14号", "李四", 98f, 69.4f); Student stu3 = new Student("15号", "王五", 77.8f, 81.5f); System.out.println(stu1); System.out.println(stu1.equals(stu2)); } public void testBanJi(){} { Student stu1 = new Student("13号", "张三", 78f, 82.5f); Student stu2 = new Student("14号", "李四", 98f, 69.4f); Student stu3 = new Student("15号", "王五", 77.8f, 81.5f); Student stu4 = new Student("15号", "王五", 77.8f, 81.5f); BanJi bj1 = new BanJi("A003", "1(3)班"); bj1.addStudent(stu1); bj1.addStudent(stu2); bj1.addStudent(stu3); bj1.addStudent(stu4); bj1.displayAllStudent(); Student stu5 =bj1.searchStudentByNum("14号"); if(stu5!=null) { System.out.println("根据学号查询的歌曲信息为"); System.out.println(stu5); } else { System.out.println("该学生不存在。"); } bj1.deleteStudent("15号"); bj1.displayAllStudent(); bj1.insertChineseScore("15号", 87f); bj1.displayAllStudent(); bj1.insertMathScore("13号", 99.87f); bj1.displayAllStudent(); } public void testSchool() { Student stu1 = new Student("13号", "张三", 78f, 62.5f); Student stu2 = new Student("14号", "李四", 58f, 79.4f); Student stu3 = new Student("15号", "王五", 77.8f, 81.5f); Student stu4 = new Student("13号", "张六", 78f, 52.5f); Student stu5 = new Student("14号", "李七", 95f, 69.4f); Student stu6 = new Student("15号", "李八", 77.8f, 81.5f); Student stu7 = new Student("13号", "张九", 78f, 82.5f); Student stu8 = new Student("14号", "李十", 88f, 69.4f); Student stu9 = new Student("15号", "王十一", 77.8f, 51.5f); School sc = new School(); BanJi bj1 = new BanJi("A001", "1(1)班"); BanJi bj2 = new BanJi("A002", "1(2)班"); BanJi bj3 = new BanJi("A003", "1(3)班"); bj1.addStudent(stu1); bj1.addStudent(stu2); bj1.addStudent(stu3); bj2.addStudent(stu4); bj2.addStudent(stu5); bj2.addStudent(stu6); bj3.addStudent(stu7); bj3.addStudent(stu8); bj3.addStudent(stu9); sc.addBanji(bj1); sc.addBanji(bj2); sc.addBanji(bj3); sc.displayBanJiName(); sc.deleteBanji(bj3); sc.displayBanJiName(); BanJi bj4 = sc.searchByName("1(1)班"); if(bj4!=null) { bj4.displayAllStudent(); } else { System.out.println("没有找到此班级!"); } sc.sortChineseByAverage(); sc.sortMathByAverage(); } public void mainMenu() { System.out.println("******************************"); System.out.println(" **主菜单** "); System.out.println(" 1--班级管理 "); System.out.println(" 2--学校管理 "); System.out.println(" 0--退出 "); } public void banJiMenu() { System.out.println("******************************"); System.out.println(" 班级列表 "); System.out.println(" 1--添加学生信息到主学生列表 "); System.out.println(" 2--添加学生信息到普通班级 "); System.out.println(" 3--通过学号查询学生信息 "); System.out.println(" 4--输入班级的语文成绩 "); System.out.println(" 5--输入班级的数学成绩 "); System.out.println(" 6--删除学生信息 "); System.out.println(" 6--显示所有学生信息 "); System.out.println(" 9--返回上级菜单 "); System.out.println("******************************"); } public void schoolMenu() { System.out.println("******************************"); System.out.println(" 学校列表 "); System.out.println(" 1--创建班级 "); System.out.println(" 2--删除班级 "); System.out.println(" 3--通过班级名称查询班级信息 "); System.out.println(" 4--对各班语文成绩平均分进行由大到小排序 "); System.out.println(" 5--对各班数学成绩平均分进行由大到小排序 "); System.out.println(" 6--显示所有班级名称 "); System.out.println(" 9--返回上级菜单 "); System.out.println("******************************"); } public void test() { TestDemo td = new TestDemo(); Scanner sc = new Scanner(System.in); int input1=0, input2=0, input3=0; School banJiList = new School(); BanJi mainStuList = new BanJi("0000", "主学生列表"); banJiList.addBanji(mainStuList); while(true) { td.mainMenu(); System.out.println("请输入对应数字进行操作"); input1 = sc.nextInt(); if(input1==0) { break; } switch(input1) { case 1: while(true) { td.banJiMenu(); System.out.println("请输入对应数字进行操作"); input2 = sc.nextInt(); if(input2==9) { break; } switch(input2) { case 1: System.out.println("添加学生信息到主学生列表中"); System.out.println("请输入要添加的学生个数"); try { int m = sc.nextInt(); if(m<0) { System.out.println("您输入的数字有误,请重新输入。"); } else { for(int i=1; i<=m; i++) { Student stu1 = new Student(); System.out.println("请输入第" + i +"个学生信息"); System.out.println("请输入学生ID"); String id = sc.next(); stu1.setStuNum(id); System.out.println("请输入学生姓名"); String name = sc.next(); stu1.setStuName(name); mainStuList.addStudent(stu1); } System.out.println("主学生列表中信息为"); List<Student> stuList = mainStuList.getStuList(); for(Student stu : stuList) { System.out.println("学生信息:[学号:" + stu.getStuNum() + ",姓名:" + stu.getStuName() + "]"); } } } catch(InputMismatchException e) { System.out.println("请输入正确的数字"); String s = sc.next(); } break; case 2: System.out.println("添加学生信息到普通班级 "); System.out.println("请输入要添加的班级名称"); try { BanJi bj1 = null; String className = sc.next(); Set<String> banJiNameSet = banJiList.getBanJiMap().keySet(); for(String s : banJiNameSet) { if(s.equals(className)) { System.out.println("请输入要添加的学生个数"); int stuNumber = 0; stuNumber = sc.nextInt(); if(stuNumber<0) { System.out.println("您输入的数字有误,请重新输入。"); } else { for(int i=1 ; stuNumber<=i; i++) { System.out.println("请输入要添加的学生ID"); String id =null; id = sc.next(); Student stu1; stu1 = mainStuList.searchStudentByNum(id); if(stu1 != null) { bj1 = banJiList.searchByName(s); bj1.addStudent(stu1); } else { System.out.println("该学生信息在主学生列表中不存在,请输入学生的其他信息。"); System.out.println("请输入要添加的学生姓名"); String stuName = sc.next(); List<Student> stuList = mainStuList.getStuList(); for(Student stu2 : stuList) { if(stu2.getStuName().equals(stuName)) { bj1 = banJiList.searchByName(s); bj1.addStudent(stu2); } else { System.out.println("该学生信息在主学生列表中不存在"); } } } } } System.out.println("主学生列表:"); List<Student> stuList = mainStuList.getStuList(); for(Student stu3 : stuList) { System.out.println("学生信息:[学号:" + stu3.getStuNum() + ",姓名:" + stu3.getStuName() + "]"); } System.out.println(bj1.getClassName() + "班级列表"); List<Student> stuList2 = bj1.getStuList(); for(Student stu4 : stuList2) { System.out.println("学生信息:[学号:" + stu4.getStuNum() + ",姓名:" + stu4.getStuName() + "]"); } } else { System.out.println("未查询到此班级!"); } } } catch(InputMismatchException e) { System.out.println("请输入正确的数字"); String s = sc.next(); continue; } break; case 3: System.out.println("录入语文成绩"); break; case 4: System.out.println("录入数学成绩"); break; case 5: System.out.println("删除学生信息"); break; case 6: System.out.println("显示列表中所有学生"); break; default: System.out.println("请输入指定的数字"); break; } } break; case 2: while(true) { td.schoolMenu(); System.out.println("请输入对应数字进行学校列表管理"); input3 = sc.nextInt(); if(input3==9) { break; } switch(input3) { case 1: System.out.println("向学校中添加班级"); System.out.println("请输入班级编号"); try { String classId = sc.next(); System.out.println("请输入班级名称"); String className = sc.next(); BanJi s = banJiList.searchByName(className); if(s != null) { System.out.println("班级已存在,请重新输入"); } else { mainStuList = new BanJi(classId, className); banJiList.addBanji(mainStuList); } } catch(InputMismatchException e) { System.out.println("请输入正确的数字"); String s = sc.next(); } break; case 2: System.out.println("从学校中删除班级! "); System.out.println("请输入班级名称"); try { String className = sc.next(); BanJi bj = banJiList.searchByName(className); if(bj != null) { banJiList.deleteBanji(bj); } else { System.out.println("班级不存在,请重新输入!"); } } catch(InputMismatchException e) { System.out.println("请输入正确的数字"); String s = sc.next(); break; } break; case 3: System.out.println("通过班级名称查询班级信息"); System.out.println("请输入班级名称!"); String className = sc.next(); BanJi bj = banJiList.searchByName(className); if(bj != null) { System.out.println("班级信息:[" + bj.getClassId() + ",班级名称:" + bj.getClassName()); } else { System.out.println("没有查询到相关班级,请重新输入!"); } break; case 4: System.out.println("对各班语文成绩平均分进行由大到小排序"); Set<String> banJiSet1 = banJiList.getBanJiMap().keySet(); if(banJiSet1 != null) { banJiList.sortChineseByAverage(); } else { System.out.println("您还没有输入班级,请先输入班级"); } break; case 5: System.out.println("对各班数学成绩平均分进行由大到小排序 "); Set<String> banJiSet2 = banJiList.getBanJiMap().keySet(); if(banJiSet2 != null) { banJiList.sortMathByAverage(); } else { System.out.println("您还没有输入班级,请先输入班级"); } break; case 6: System.out.println("显示所有班级名称"); banJiList.displayBanJiName(); break; default: System.out.println("请输入指定的数字"); break; } } break; default: System.out.println("请输入指定的数字"); break; } } } public static void main(String[] args) { TestDemo ts = new TestDemo(); //Scanner so = new Scanner(System.in); //ts.testDemo(); //ts.testBanJi(); //ts.testSchool(); ts.test(); } }
请老师指教,像普通班级添加学生时,为什么明明有班级,也会反馈为查询到此班级。还有为什么会直接跳过录入学生的过程
9
收起
正在回答
1回答
同学你好,使用的变量bj1初始化是null,所以导致报空指针异常。则建议同学调用searchByName()方法,查看班级是否存在,存在则进行添加。具体实现思路:
先判断班级是否存在,判断班级存在后,输入要添加的学生数量。然后输入的学生编号,当学生编号在主学生列表中存在时,直接添加到普通班级中。 当输入的学生编号在主学生列表中不存在时,提示不存在要求输入学生姓名。参考代码如下所示:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题
登录后可查看更多问答,登录/注册
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星