ObjectInputStream没有读入

问题描述 投票:-1回答:1

一个类将“StudentRecord”数组的元素写入它自己的序列化文件中,如果工作正常,我会检查它。但是,在另一个方法(printstats)中,我使用ObjectInputStream的readin反序列化文件并转换返回的对象,但没有填充StudentRecord对象。

package adapter;
import java.io.*;
import model.*;
import util.FileIO;
public abstract class Gradebook implements Gradeable{
    private StudentRecord [] sturec;
    public Gradebook(String fname) {
        FileIO f = new FileIO("C:\\Users\\maya4\\eclipse-workspace\\Lab 5\\StudentInfo.txt");
        Student [] stuarr = f.readData();
        Statistics statobj = new Statistics(stuarr,f);
        //System.out.printf(" main determines that countr holds %d students\n", f.getCountr());

        statobj.calculate();
        if(DEBUG)
        {
            statobj.print();
        }

        for(int i =0 ; i<f.getCountr();i++)
        {
            sturec = new StudentRecord[40];
            sturec[i] = new StudentRecord(statobj, stuarr[i]);
            if(DEBUG)
            {
                sturec[i].printStudentRecord();
            }
        }

        for(int i=0;i<f.getCountr()-1;i++)
        {
            String tempstring= Integer.toString(stuarr[i].getId());
            String filename = tempstring +".dat";
            try {
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
                out.writeObject(sturec[i]);

                if (DEBUG)
                {
                    System.out.printf("filename is %s ", filename);
                }
            }
            catch(FileNotFoundException fne) 
            {
                System.out.println("Error--" +fne.toString());
            }
            catch(IOException ioe) 
            {
                System.out.println("Error" +ioe.toString());
            }
        }
        System.out.println("\n");           
    }

    public void printstats(int stuid) 
    {
        //deserialize any file and print stats. 

        String Sstuid = Integer.toString(stuid)+".dat";
        try 
        {
            if(DEBUG)
            {
                System.out.printf("filename: %s", Sstuid);
            }
            //problem area!

            ObjectInputStream in = new ObjectInputStream(new FileInputStream(Sstuid));
            StudentRecord tempsr= (StudentRecord)in.readObject();


            System.out.printf("%d", tempsr.getSt().getId());
            //tempsr.printStudentRecord();
        }
        catch(FileNotFoundException fne) 
        {
            System.out.println("Error--" + fne.toString());
        }
        catch(IOException ioe)
        {
            System.out.println("Error --" + ioe.toString());
        } 
        catch (ClassNotFoundException cnf)
        {
            System.out.println("Error--" + cnf.toString());
        }
    }
}
java serialization objectinputstream
1个回答
0
投票

如果,您仍然没有澄清这一点,程序会在此行与NPE崩溃:

System.out.printf("%d", tempsr.getSt().getId());

之一:

  1. tempsr是空的。
  2. tempsr.getSt()返回null。

(1)如果你用writeObject()写了一个null,你可以在写作网站查看。

(2)如果基础属性是(a)null,(b)transient,或(c)static:或者,例如,如果没有底层属性,如果该方法以某种方式计算出null。

© www.soinside.com 2019 - 2024. All rights reserved.