Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NullPointerException序列化_Java - Fatal编程技术网

java.lang.NullPointerException序列化

java.lang.NullPointerException序列化,java,Java,我无法找出导致此异常的原因。它发生在reRead方法中 我已经测试了它,没有重读的方法,其他一切都很好 public class SerFiles { private ObjectInputStream in; private ObjectOutputStream out; private FileReader fr; private FileWriter fw; private BufferedReader br; private Buff

我无法找出导致此异常的原因。它发生在reRead方法中

我已经测试了它,没有重读的方法,其他一切都很好

public class SerFiles {

    private ObjectInputStream in; 
    private ObjectOutputStream out;
    private FileReader fr; 
    private FileWriter fw;
    private BufferedReader br;
    private BufferedWriter bw;
    private StringTokenizer token;
    private ArrayList<Product> prod = new ArrayList<Product>();
    private String line = "c";
    private Product proc;

    private int a,b,d,e,f;
    private String c;

    public SerFiles(){ }

    public void openFiles()
    {
        try
        {
            out = new ObjectOutputStream( new FileOutputStream( "prod.ser" ) ); 
            fr = new FileReader("SalesDelim.txt");  
            br = new BufferedReader(fr);

            System.out.println("OPEN SUCCESS");
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }

    public void readAndWrite()
    {
        try
        {
            line = br.readLine();

            while(line != null)
            {
                if(line != null)
                {
                    token = new StringTokenizer(line, "**");

                    a = Integer.parseInt(token.nextToken());
                    b = Integer.parseInt(token.nextToken());
                    c = token.nextToken();
                    d = Integer.parseInt(token.nextToken());
                    e = Integer.parseInt(token.nextToken());
                    f = Integer.parseInt(token.nextToken());

                    prod.add(new Product(a,c,e,f));
                    line = br.readLine();
                }
            }

            for(int i = 0; i<prod.size(); i++)
            {
                out.writeObject(prod.get(i));
            }

            System.out.println("WRITE SUCCESS");
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }

    public void reRead()
    {
        try
        {
            in = new ObjectInputStream(new FileInputStream("prod.ser")); //////ERROR HAPPENS HERE

            while(true)
            {
                proc = (Product)in.readObject();
                System.out.println(proc.toString());
            }
        }
        catch(EOFException ioe){
            return;
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }

    public void closeFiles()
    {

        try
        {
            fr.close();
            br.close();
            out.close();
            in.close();

            System.out.println("CLOSE SUCCESS");
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }
    }
}
我已经测试了它,没有重读的方法,它工作得很好

谢谢

试试:

in = new ObjectInputStream(new FileInputStream("prod.ser"));

while (true)
{
    proc = (Product)in.readObject();
    System.out.println(proc); // change!!

    if (proc == null) break; // quit the loop
}
所以,问题是您在一个空指针上调用toString。

我不相信会发生NullPointerException。张贴stacktrace。格式化你的代码。仅发布相关部分。不要执行System.out.printlnex.toString;。执行例如printStackTrace;它提供了更多有用的信息。