帮忙看看哪里错了
import java.io.Serializable;import java.util.Objects;
public class Student implements Serializable {
private static final long serialVersionUID= 1L;
private int stuNo;
private Stringname;
private boolean sex;
private String email;
private String birthday;
public Student() {
}
public Student(int stuNo, String name, boolean sex, String email, String birthday) {
this.stuNo = stuNo;
this.name = name;
this.sex = sex;
this.email = email;
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public Student(int stuNo, String name, boolean sex) {
this.stuNo = stuNo;
this.name = name;
this.sex = sex;
}
public int getStuNo() {
return stuNo;
}
public void setStuNo(int stuNo) {
this.stuNo = stuNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return stuNo == student.stuNo &&
sex == student.sex &&
Objects.equals(name, student.name) &&
Objects.equals(email, student.email) &&
Objects.equals(birthday, student.birthday);
}
@Override
public int hashCode() {
return Objects.hash(stuNo, name, sex, email, birthday);
}
@Override
public String toString() {
return "Student{" +
"stuNo=" + stuNo +
", name='" + name + '\'' +
", sex=" + sex +
", email='" + email + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
}
import org.omg.PortableInterceptor.INACTIVE;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class ShowStudentPage {
public void rukou(){
while (true){
/* 欢迎使用学生信息管理系统,请认真阅读以下使用说明:
请输入不同的功能编号来选择不同的功能:
查看学生列表
保存学生
删除学生
查看某个学生详细信息*/
System.out.println("欢迎使用学生信息管理系统,请认真阅读以下使用说明:+\n" +
"请输入不同的功能编号来选择不同的功能:"+"\n"+"" +
" 查看学生列表\n" +
" 保存学生\n" +
" 删除学生\n" +
" 查看某个学生详细信息");
Scanner s = new Scanner(System.in);
System.out.print("请输入功能编号:");
int no = s.nextInt();
if (no == 1){
showStuInfo();
}else if (no == 2){
saveStudent();
}else if(no == 3 ){
deleteStu();
}else if(no == 4){
showStuAllInfo();
}else{
System.out.println("输入的编号有误,请重新输入!");
}
}
}
//保存学生
public void saveStudent() {
Scanner s = new Scanner(System.in);
Student student = new Student();
System.out.println("请输入学生编号:");
student.setStuNo(s.nextInt());
System.out.println("请输入学生姓名:");
student.setName(s.next());
System.out.println("请输入学生性别:");
student.setSex((s.next().equals("男")));
System.out.println("请输入学生生日:");
student.setBirthday(s.next());
System.out.println("请输入学生邮箱:");
student.setEmail(s.next());
Map<Integer, Student> studentMap = readMap();
studentMap.put(student.getStuNo(),student);
saveMap(studentMap);
}
//展示学生列表
public void showStuInfo() {
System.out.println("------------------------------");
System.out.println("学生信息列表展示");
System.out.println("------------------------------");
System.out.println("学号\t姓名\t性别");
Map<Integer,Student> map= readMap();
Set<Map.Entry<Integer,Student>> set = map.entrySet();
for (Map.Entry<Integer,Student> s : set) {
System.out.println(s.getKey()+"\t" + s.getValue().getStuNo() + "\t" + s.getValue().getName() +
(s.getValue().isSex()?"男" : "女"));
}
}
//展示学生详细信息
public void showStuAllInfo(){
Scanner s = new Scanner(System.in);
System.out.print("请输入学生编号:");
int no = s.nextInt();
Map<Integer,Student> map= readMap();
Set<Map.Entry<Integer,Student>> set = map.entrySet();
for (Map.Entry<Integer,Student> s1 : set) {
if (no==s1.getKey()){
System.out.println("学号:" + s1.getKey()+ "\n" +"姓名:" +s1.getValue().getName() + "\n"+"生日:" +
s1.getValue().getBirthday() + "\n" + "性别:"+
(s1.getValue().isSex()?"男" : "女") + "\n"+ "邮箱:" + s1.getValue().getEmail());
}else{
System.out.println("您输入的学生编号不存在,请重新输入!");
} }
}
//删除学生
public void deleteStu() {
Scanner s3 = new Scanner(System.in);
System.out.print("请输入要删除的学生编号:");
int no = s3.nextInt();
Map<Integer,Student> map = readMap();
map.remove(no);
saveMap(map);
}
//序列化学生集合
public void saveMap(Map<Integer,Student>map) {
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("StuInfo"));
oos.writeObject(map);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//反序列化
public Map<Integer, Student> readMap() {
ObjectInputStream ois = null;
try {
Map<Integer,Student> map = null;
ois = new ObjectInputStream(new FileInputStream("StuInfo"));
if (null != ois){
map = (Map<Integer,Student>) ois.readObject();
}
return map;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
public class Test {
public static void main(String[] args) {
ShowStudentPage showStudentPage = new ShowStudentPage();
showStudentPage.rukou();
}
}
最后运行报错
欢迎使用学生信息管理系统,请认真阅读以下使用说明:+
请输入不同的功能编号来选择不同的功能:
查看学生列表
保存学生
删除学生
查看某个学生详细信息
请输入功能编号:1
------------------------------
学生信息列表展示
------------------------------
学号 姓名 性别
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2335)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2804)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at ShowStudentPage.readMap(ShowStudentPage.java:139)
at ShowStudentPage.showStuInfo(ShowStudentPage.java:78)
at ShowStudentPage.rukou(ShowStudentPage.java:28)
at Test.main(Test.java:4)
Exception in thread "main" java.lang.NullPointerException
at ShowStudentPage.showStuInfo(ShowStudentPage.java:79)
at ShowStudentPage.rukou(ShowStudentPage.java:28)
at Test.main(Test.java:4)
Process finished with exit code 1
看提示语“Exception in thread "main" java.lang.NullPointerException”,指针为空,是不是野指针(或者说悬空指针) 豆嘉木 发表于 2022-7-8 08:48
看提示语“Exception in thread "main" java.lang.NullPointerException”,指针为空,是不是野指针(或者说 ...
找不出原因呀 同上,空指针异常,应该是readMap反序列化的地方出了问题,可以尝试在那边每隔一行输出一个1 23 4 5 6 这样排除是哪一行有问题,难道是路径问题 风华叶冷 发表于 2022-8-1 09:43
同上,空指针异常,应该是readMap反序列化的地方出了问题,可以尝试在那边每隔一行输出一个1 23 4 5 6 这 ...
不确定,可以现在查询学生那里先检查一下是不是readMap出错了 报错 EOF ,这个流根本没有被生成,而是直接结束了这个流,下面的空指针就是因为这个而出现的 如果流里的数据是空的,用 ObjectInputStream 就会报 EOF ,不同于字节流,如果是空的就返回 -1 showStuInfo() 方法里 Map<Integer,Student> map= readMap(); 加判空
页:
[1]