EOFexception in Java when reading objectinputstream

前端 未结 1 1224
庸人自扰
庸人自扰 2021-01-15 00:27

I want to read multiple objects (my own class Term) that I have output to a .dat file, but I always get a nullPointException or EOFException.

ObjectInputStre         


        
相关标签:
1条回答
  • 2021-01-15 00:54

    See the Javadoc. readObject() doesn't return null at EOF. It throws EOFException. The only way it can return a null is if you wrote a null at the other end, and that's not necessarily a good reason for terminating the read loop.

    In short your code is wrong.

    NB the initialization of 'o' is redundant.

    NB (2) The code you posted cannot throw NullPointerException, unless masterFile is null. Is that a serious report or just a guess?

    0 讨论(0)
提交回复
热议问题