1

I'm writing a small program for an assignment and part of it involves reading from a file using ObjectInputStream. I've run into a brick wall because I keep getting errors when trying to close the file in the finally block as well as a NullPointerException but I cannot understand why. Any help is much appreciated! I have checked already and the file path is correct, so it is able to find the file.

Example file: hello || apples, acai berry, bananas || shopping || 0.0005 || yes

 public Disease[] readInCancers() {
    Disease[] cancerList = null;
    try {
        FileInputStream fis = new FileInputStream(myData);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ois = new ObjectInputStream(bis);
        while(true) {
            Disease disease = null;
            try {
                disease = (Disease)ois.readObject();
            } catch (EOFException eofx) {
                break;
            }
            if (cancerList == null || cancerList.length == 0) {
                    cancerList = new Disease[1];
                    cancerList[0] = disease;
                } else {
                    Disease[] newList = new Disease[cancerList.length + 1];
                    System.arraycopy(cancerList, 0, newList, 0, cancerList.length);
                    newList[cancerList.length] = disease;
                    cancerList = newList;
                }
        }
    } catch (FileNotFoundException fnfx) {
        JOptionPane.showMessageDialog(null, "File could not be found");
    } catch (IOException iox) {
        JOptionPane.showMessageDialog(null, "Problem with reading from file");
    } catch (ClassNotFoundException cnfx) {
        JOptionPane.showMessageDialog(null, "Class could not be found");
    } catch (NullPointerException npx) {
        System.out.println("blah");
     } finally {
        try {
            ois.close();
        } catch (IOException iox) {
            JOptionPane.showMessageDialog(null, "Problem with closing file");
        }
    }
    return cancerList;
}

When I run the program, it gives a NullPointerException at ois.close() as well as an IOException that produces the pop-up "Problem with reading from file".

I have also tried changing how the file itself is structured, replaced the || (delimiters) with a word or even blank space but nothing changes.

3
  • Does the file you're reading just contain those strings, or does it contain actual Disease objects that you've written? Commented Apr 12, 2013 at 18:43
  • The file that is being read contains just those strings and I want to write it as an object. Commented Apr 12, 2013 at 19:03
  • That's your problem. You're trying to read Objects but the file contains Strings. Commented Apr 12, 2013 at 19:19

2 Answers 2

3

Your FileInputStream is throwing an exception (I'm guessing from incorrect file permissions, but you'll have to look into this further); this occurs before you initialize your ObjectInputStream, so ois is still null when you reach your finally block which is resulting in a null pointer exception. It's usually a good idea to precede close statements in final blocks by null pointer checks for this reason.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but i think it's a better idea not to enter the try block until you know the resource is non-null.
1

When using an ObjectInputStream the input data is required to be in a byte format that can be read into a serialized object, Disease in this case. If the format is not in the expected format a StreamCorruptedException will be thrown. If you are changing the text file manually, chances are that this exception is being thrown but the exact message is not displayed as you are displaying a generic Problem with reading from file message.

Displaying the stack trace will help

iox.printStackTrace();

Ensure that you are writing the objects correctly to file. Alternatively you could use a text based file, and use Printwriter to write, Scanner to read. You can use || for as a Scanner delimiter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.