Java 为什么代码是不可解密的!无法读取可插入文件?

Java 为什么代码是不可解密的!无法读取可插入文件?,java,swing,nullpointerexception,Java,Swing,Nullpointerexception,为什么代码是不可解密的!无法读取可插入文件! 它在文件中正确写入,但无法读取 public ArrayList read_file(){ ArrayList文件=新的ArrayList(); 试一试{ 文件f=新文件(“File.bin”); ObjectInputStream is=新ObjectInputStream(新文件输入流(“file.bin”); int x=(int)f.length(); while(is.available()!=0){ file.add((customer

为什么代码是不可解密的!无法读取可插入文件! 它在文件中正确写入,但无法读取

public ArrayList read_file(){
ArrayList文件=新的ArrayList();
试一试{
文件f=新文件(“File.bin”);
ObjectInputStream is=新ObjectInputStream(新文件输入流(“file.bin”);
int x=(int)f.length();
while(is.available()!=0){
file.add((customer)是.readObject());
}
}捕获(FileNotFoundException ex){
Logger.getLogger(Login.class.getName()).log(Level.SEVERE,null,ex);
}捕获(IOException | ClassNotFoundException ex){
Logger.getLogger(Login.class.getName()).log(Level.SEVERE,null,ex);
}
返回文件;}

您的错误是空指针异常。您已将客户数组初始化为null。我建议改为使用ArrayList。

在此处粘贴代码。不要提供一些外部链接。请参阅&I建议,不要使用序列化,而是使用JAXB或其他方法,序列化仅用于对象的短期存储,通常用于通过有线传输。。。它有问题,很容易被破坏,我尝试了,但没有工作
public ArrayList<customer> read_file(){
ArrayList <customer>file = new ArrayList<customer>();
try {
    File f= new File("file.bin");
    ObjectInputStream is= new ObjectInputStream (new FileInputStream("file.bin"));
    int x=(int) f.length();


    while(is.available()!=0){
    file.add((customer)is.readObject()) ;

    }
} catch (FileNotFoundException ex) {
    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException | ClassNotFoundException ex) {
    Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
}

return file ;}