鱼C论坛

 找回密码
 立即注册
查看: 1015|回复: 7

帮忙看看哪里错了

[复制链接]
发表于 2022-7-8 08:27:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. [code]import java.io.Serializable;
  2. import java.util.Objects;

  3. public class Student implements Serializable {

  4.     private static final long serialVersionUID= 1L;
  5.     private int stuNo;
  6.     private String  name;
  7.     private boolean sex;
  8.     private String email;
  9.     private String birthday;

  10.     public Student() {
  11.     }

  12.     public Student(int stuNo, String name, boolean sex, String email, String birthday) {
  13.         this.stuNo = stuNo;
  14.         this.name = name;
  15.         this.sex = sex;
  16.         this.email = email;
  17.         this.birthday = birthday;
  18.     }

  19.     public String getEmail() {
  20.         return email;
  21.     }

  22.     public void setEmail(String email) {
  23.         this.email = email;
  24.     }

  25.     public String getBirthday() {
  26.         return birthday;
  27.     }

  28.     public void setBirthday(String birthday) {
  29.         this.birthday = birthday;
  30.     }

  31.     public Student(int stuNo, String name, boolean sex) {
  32.         this.stuNo = stuNo;
  33.         this.name = name;
  34.         this.sex = sex;
  35.     }

  36.     public int getStuNo() {
  37.         return stuNo;
  38.     }

  39.     public void setStuNo(int stuNo) {
  40.         this.stuNo = stuNo;
  41.     }

  42.     public String getName() {
  43.         return name;
  44.     }

  45.     public void setName(String name) {
  46.         this.name = name;
  47.     }

  48.     public boolean isSex() {
  49.         return sex;
  50.     }

  51.     public void setSex(boolean sex) {
  52.         this.sex = sex;
  53.     }

  54.     @Override
  55.     public boolean equals(Object o) {
  56.         if (this == o) return true;
  57.         if (o == null || getClass() != o.getClass()) return false;
  58.         Student student = (Student) o;
  59.         return stuNo == student.stuNo &&
  60.                 sex == student.sex &&
  61.                 Objects.equals(name, student.name) &&
  62.                 Objects.equals(email, student.email) &&
  63.                 Objects.equals(birthday, student.birthday);
  64.     }

  65.     @Override
  66.     public int hashCode() {
  67.         return Objects.hash(stuNo, name, sex, email, birthday);
  68.     }

  69.     @Override
  70.     public String toString() {
  71.         return "Student{" +
  72.                 "stuNo=" + stuNo +
  73.                 ", name='" + name + '\'' +
  74.                 ", sex=" + sex +
  75.                 ", email='" + email + '\'' +
  76.                 ", birthday='" + birthday + '\'' +
  77.                 '}';
  78.     }

  79. }
复制代码
[/code]

  1. import org.omg.PortableInterceptor.INACTIVE;

  2. import java.io.*;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6. import java.util.Set;

  7. public class ShowStudentPage {
  8.     public void rukou(){
  9.         while (true){
  10.            /* 欢迎使用学生信息管理系统,请认真阅读以下使用说明:
  11.             请输入不同的功能编号来选择不同的功能:
  12.                     [1]查看学生列表
  13.                     [2]保存学生
  14.                     [3]删除学生
  15.                     [4]查看某个学生详细信息*/
  16.             System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:+\n" +
  17.                     "请输入不同的功能编号来选择不同的功能:"+"\n"+"" +
  18.                             " [1]查看学生列表\n" +
  19.                             " [2]保存学生\n" +
  20.                             " [3]删除学生\n" +
  21.                             " [4]查看某个学生详细信息");
  22.             Scanner s = new Scanner(System.in);
  23.             System.out.print("请输入功能编号:");
  24.             int no = s.nextInt();
  25.             if (no == 1){
  26.                 showStuInfo();
  27.             }else if (no == 2){
  28.                 saveStudent();
  29.             }else if(no == 3 ){
  30.                 deleteStu();
  31.             }else if(no == 4){
  32.                 showStuAllInfo();

  33.             }else{
  34.                 System.out.println("输入的编号有误,请重新输入!");
  35.             }

  36.         }
  37.     }



  38.     //保存学生
  39.     public void saveStudent() {
  40.         Scanner s = new Scanner(System.in);
  41.         Student student = new Student();

  42.         System.out.println("请输入学生编号:");
  43.         student.setStuNo(s.nextInt());

  44.         System.out.println("请输入学生姓名:");
  45.         student.setName(s.next());

  46.         System.out.println("请输入学生性别:");
  47.         student.setSex((s.next().equals("男")));

  48.         System.out.println("请输入学生生日:");
  49.         student.setBirthday(s.next());

  50.         System.out.println("请输入学生邮箱:");
  51.         student.setEmail(s.next());

  52.         Map<Integer, Student> studentMap = readMap();
  53.         studentMap.put(student.getStuNo(),student);
  54.         saveMap(studentMap);
  55.     }

  56.     //展示学生列表
  57.     public void showStuInfo() {
  58.         System.out.println("------------------------------");
  59.         System.out.println("学生信息列表展示");
  60.         System.out.println("------------------------------");
  61.         System.out.println("学号\t姓名\t性别");


  62.         Map<Integer,Student> map= readMap();
  63.         Set<Map.Entry<Integer,Student>> set = map.entrySet();
  64.         for (Map.Entry<Integer,Student> s : set) {
  65.             System.out.println(s.getKey()+"\t" + s.getValue().getStuNo() + "\t" + s.getValue().getName() +
  66.                     (s.getValue().isSex()?"男" : "女"));
  67.         }
  68.     }

  69.     //展示学生详细信息
  70.     public void showStuAllInfo(){
  71.         Scanner s = new Scanner(System.in);
  72.         System.out.print("请输入学生编号:");
  73.         int no = s.nextInt();
  74.         Map<Integer,Student> map= readMap();
  75.         Set<Map.Entry<Integer,Student>> set = map.entrySet();
  76.         for (Map.Entry<Integer,Student> s1 : set) {
  77.             if (no==s1.getKey()){
  78.                 System.out.println("学号:" + s1.getKey()+ "\n" +"姓名:" +s1.getValue().getName() + "\n"+"生日:" +
  79.                         s1.getValue().getBirthday() + "\n" + "性别:"+
  80.                                 (s1.getValue().isSex()?"男" : "女") + "\n"+ "邮箱:" + s1.getValue().getEmail());
  81.             }else{
  82.                 System.out.println("您输入的学生编号不存在,请重新输入!");

  83.             }        }
  84.     }
  85.     //删除学生
  86.     public void deleteStu() {
  87.         Scanner s3 = new Scanner(System.in);
  88.         System.out.print("请输入要删除的学生编号:");
  89.         int no = s3.nextInt();
  90.         Map<Integer,Student> map = readMap();
  91.         map.remove(no);
  92.         saveMap(map);
  93.     }
  94.     //序列化学生集合
  95.     public void saveMap(Map<Integer,Student>  map) {
  96.         ObjectOutputStream oos = null;
  97.         try {
  98.             oos = new ObjectOutputStream(new FileOutputStream("StuInfo"));
  99.             oos.writeObject(map);
  100.             oos.flush();


  101.         } catch (IOException e) {
  102.             e.printStackTrace();
  103.         } finally {
  104.             if (oos != null) {
  105.                 try {
  106.                     oos.close();
  107.                 } catch (IOException e) {
  108.                     e.printStackTrace();
  109.                 }
  110.             }
  111.         }
  112.     }

  113.     //反序列化
  114.     public Map<Integer, Student> readMap() {
  115.         ObjectInputStream ois = null;
  116.         try {
  117.             Map<Integer,Student> map = null;
  118.             ois = new ObjectInputStream(new FileInputStream("StuInfo"));
  119.            if (null != ois){
  120.                map = (Map<Integer,Student>) ois.readObject();
  121.            }

  122.             return map;
  123.         } catch (IOException e) {
  124.             e.printStackTrace();

  125.         } catch (ClassNotFoundException e) {
  126.             e.printStackTrace();
  127.         } finally {
  128.             if (ois != null) {

  129.                 try {
  130.                     ois.close();
  131.                 } catch (IOException e) {
  132.                     e.printStackTrace();
  133.                 }
  134.             }
  135.         }
  136.         return null;
  137.     }
  138. }
复制代码
  1. public class Test {
  2.     public static void main(String[] args) {
  3.         ShowStudentPage showStudentPage = new ShowStudentPage();
  4.         showStudentPage.rukou();
  5.     }
  6. }

复制代码



最后运行报错

  1. 欢迎使用学生信息管理系统,请认真阅读以下使用说明:+
  2. 请输入不同的功能编号来选择不同的功能:
  3. [1]查看学生列表
  4. [2]保存学生
  5. [3]删除学生
  6. [4]查看某个学生详细信息
  7. 请输入功能编号:1
  8. ------------------------------
  9. 学生信息列表展示
  10. ------------------------------
  11. 学号        姓名        性别
  12. java.io.EOFException
  13.         at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2335)
  14.         at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2804)
  15.         at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)
  16.         at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
  17.         at ShowStudentPage.readMap(ShowStudentPage.java:139)
  18.         at ShowStudentPage.showStuInfo(ShowStudentPage.java:78)
  19.         at ShowStudentPage.rukou(ShowStudentPage.java:28)
  20.         at Test.main(Test.java:4)
  21. Exception in thread "main" java.lang.NullPointerException
  22.         at ShowStudentPage.showStuInfo(ShowStudentPage.java:79)
  23.         at ShowStudentPage.rukou(ShowStudentPage.java:28)
  24.         at Test.main(Test.java:4)

  25. Process finished with exit code 1
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-8 08:48:50 | 显示全部楼层
看提示语“Exception in thread "main" java.lang.NullPointerException”,指针为空,是不是野指针(或者说悬空指针)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-7-8 09:03:32 | 显示全部楼层
豆嘉木 发表于 2022-7-8 08:48
看提示语“Exception in thread "main" java.lang.NullPointerException”,指针为空,是不是野指针(或者说 ...

找不出原因呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-1 09:43:21 | 显示全部楼层
同上,空指针异常,应该是readMap反序列化的地方出了问题,可以尝试在那边每隔一行输出一个1 2  3 4 5 6 这样排除是哪一行有问题,难道是路径问题
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-1 09:47:01 | 显示全部楼层
风华叶冷 发表于 2022-8-1 09:43
同上,空指针异常,应该是readMap反序列化的地方出了问题,可以尝试在那边每隔一行输出一个1 2  3 4 5 6 这 ...

不确定,可以现在查询学生那里先检查一下是不是readMap出错了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-2 09:12:20 | 显示全部楼层
报错 EOF ,这个流根本没有被生成,而是直接结束了这个流,下面的空指针就是因为这个而出现的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-2 09:23:31 | 显示全部楼层
如果流里的数据是空的,用 ObjectInputStream 就会报 EOF ,不同于字节流,如果是空的就返回 -1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-1-6 15:31:35 | 显示全部楼层
showStuInfo() 方法里   Map<Integer,Student> map= readMap(); 加判空
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 19:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表