学生管理系统中一些异常的解决
在进行学生管理系统中的会出现许多异常比如:空指针异常,ClassNotFoundException异常
例如以下:
ClassNotFoundException异常
Map<Integer, User> map3;
ObjectInputStream ois1 = null;
try {
ois1 = new ObjectInputStream (new FileInputStream ("C:\\Users\\杨骜捷\\IdeaProjects\\ProjectFinal\\src\\com\\Change\\Student"));
map3 = (TreeMap) ois1.readObject ();
map3.put (user.getUserName (), user);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream ("C:\\Users\\杨骜捷\\IdeaProjects\\ProjectFinal\\src\\com\\Change\\Student");
oos = new ObjectOutputStream (fos);
oos.writeObject (map3);
oos.flush ();
} catch (IOException e){
e.printStackTrace ();
} finally {
if (null != fos) {
try {
fos.close ();
}catch (IOException e) {
e.printStackTrace ();
}
}
if (null != oos) {
try {
oos.close ();
}catch (IOException e) {
e.printStackTrace ();
}
}
}
}catch (IOException |ClassNotFoundException e) {
e.printStackTrace ();
}finally {
try {
if (null != ois1) {
ois1.close ();
}
}catch (IOException e) {
e.printStackTrace ();
}
}
sign_in();
如果这样的话会出现ClassNotFoundException针异常,因为如果是第一次注册那么就会反序列化失败会爆出空指针异常
Map<Integer, User> map3;
ObjectInputStream ois1 = null;
try {
ois1 = new ObjectInputStream (new FileInputStream ("C:\\Users\\杨骜捷\\IdeaProjects\\ProjectFinal\\src\\com\\Change\\Student"));
map3 = (TreeMap) ois1.readObject ();
map3.put (user.getUserName (), user);
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream ("C:\\Users\\杨骜捷\\IdeaProjects\\ProjectFinal\\src\\com\\Change\\Student");
oos = new ObjectOutputStream (fos);
oos.writeObject (map3);
oos.flush ();
} catch (IOException e){
e.printStackTrace ();
} finally {
if (null != fos) {
try {
fos.close ();
}catch (IOException e) {
e.printStackTrace ();
}
}
if (null != oos) {
try {
oos.close ();
}catch (IOException e) {
e.printStackTrace ();
}
}
}
}catch (IOException |ClassNotFoundException e) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
Map<Integer, User> map_s = new TreeMap<> ();
try {
fos = new FileOutputStream ("C:\\Users\\杨骜捷\\IdeaProjects\\ProjectFinal\\src\\com\\Change\\Student");
oos = new ObjectOutputStream (fos);
map_s.put (user.getUserName (), user);
oos.writeObject (map_s);
oos.flush ();
} catch (IOException exception){
exception.printStackTrace ();
} finally {
if (null != fos) {
try {
fos.close ();
}catch (IOException ioException) {
ioException.printStackTrace ();
}
}
if (null != oos) {
try {
oos.close ();
}catch (IOException exception1) {
exception1.printStackTrace ();
}
}
}
}finally {
try {
if (null != ois1) {
ois1.close ();
}
}catch (IOException e) {
e.printStackTrace ();
}
}
sign_in();
那么可以通过catch语句来解决问题
因为在捕获异常后一般我们会选择打印异常
那么我们就可以在catch语句中直接写入要注册的账号,因为出现ClassNotFoundException异常那么说明文件中没有存入有用的数据不用担心数据覆盖的问题。
NullPointerException异常
System.out.println ("输入查询班级");
String major = scanner.next ();
ObjectInputStream ois1 = null;
try {
ois1 = new ObjectInputStream (new FileInputStream ("C:\\Users\\杨骜捷\\IdeaProjects\\ProjectFinal\\src\\com\\Change\\Student"));
Map<Integer, User> map1 = (TreeMap) ois1.readObject ();
Map<Object, User> map2 = new TreeMap ();
for (Object obj : map1.keySet ()) {
//首先遍历map1 比较map1.get (obj).getMajor ()和major
if (map1.get (obj).getMajor ().equalsIgnoreCase (major)) {
//将相要查询班级的学生放入同一map-map2
map2.put (obj, map1.get (obj));
}
}
for (Object obj : map2.keySet ()) {
//遍历学生成绩
System.out.println ("学号:" + obj + " " + "学生成绩:" + map2.get (obj).getGrade ());
}
}catch (IOException | ClassNotFoundException|NullPointerException e) {
e.printStackTrace();
}finally {
try {
if (null != ois1) {
ois1.close ();
}
}catch (IOException e) {
e.printStackTrace ();
}
}
出现该异常时可以通过将 e.printStackTrace();替换为 getGrade_Teacher ();
直接递归回方法开头,因为出现该异常一般是输入的学号不存在对应的对象或者是对应的对象不存在该属性,那么直接递归是一个
个人认为较为好的方法。