主类中有一个初始化块:
private ScoreSheet ss;
{
File file = new File("table.bin");
if(!file.exists()) {
try {
file.createNewFile();
ss = new ScoreSheet();
try(FileOutputStream fos = new FileOutputStream("table.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos)){
oos.writeObject(ss);
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
else {
try(FileInputStream fis = new FileInputStream("table.bin");
ObjectInputStream ois = new ObjectInputStream(fis)){
ss = (ScoreSheet) ois.readObject();
}
catch (IOException | ClassNotFoundException ex) {
ex.printStackTrace();
}
}
}
在行ss = (ScoreSheet) ois.readObject(); 在线程“AWT-EventQueue-0”java.lang.ExceptionInInitializerError 中出现异常崩溃
在堆栈跟踪的更下方是由以下原因引起的:java.util.Base64$Decoder.decode(Base64.java:549) 处的 java.lang.NullPointerException
调用对象类代码:
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.List;
public class ScoreSheet implements Externalizable {
private List<PlayerStats> players = new ArrayList<>();
public List<PlayerStats> getList() {
return players;
}
public ScoreSheet() {}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
for(PlayerStats current: players) {
out.writeObject(current);
}
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
for (int i = 0; i < players.size(); i++) {
PlayerStats current = (PlayerStats) in.readObject();
players.add(current);
}
}
}
工作表中的对象类代码:
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.Date;
public class PlayerStats implements Externalizable, Comparable<PlayerStats> {
private String name;
private String pass;
private Integer score = 0;
private Date date = new Date();
public PlayerStats(String name, String pass) {
this.name = name;
this.pass = pass;
}
public PlayerStats() {}
public void setScore(Integer score) {
this.score = score;
}
public String getName() {
return name;
}
public String getPass() {
return pass;
}
public Integer getScore() {
return score;
}
public String getDate() {
String pattern = "dd/MM/yyyy";
DateFormat df = new SimpleDateFormat(pattern);
Date today = Calendar.getInstance().getTime();
String reportDate = df.format(today);
return reportDate;
}
@Override
public int compareTo(PlayerStats another) {
return score.compareTo(another.score);
}
@Override
public String toString() {
return "Player [name=" + name + ", date=" + date + ", score=" + score + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((pass == null) ? 0 : pass.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PlayerStats other = (PlayerStats) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (pass == null) {
if (other.pass != null)
return false;
} else if (!pass.equals(other.pass))
return false;
return true;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.getName());
out.writeObject(this.encryptString(this.getPass()));
out.writeObject(this.getScore());
out.writeObject(this.getDate());
}
private Object encryptString(String pass2) {
return Base64.getEncoder().encodeToString(pass.getBytes());
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
name = (String) in.readObject();
pass = this.decryptString((String) in.readObject());
score = (Integer) in.readObject();
date = (Date) in.readObject();
}
private String decryptString(String readObject) {
return new String(Base64.getDecoder().decode(pass));
}
}
当到处都是Serializable而不是Externalizable时,一切正常。
问题在于从二进制文件中读取 java.util.Data 类对象。我只是将其替换为字符串表示形式。