2

I'm trying to read an object from a XML file using XMLDecoder. The construction seems to be OK, but when I call the readObject() method i get null instead of the object I expected.

The file exists and the created BufferedInputStream reads the file correctly. This code works OK on the original Vista laptop it was written on, but fails on my Win Xp machine.

     try {
        XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
            new FileInputStream("Params.xml")));

        Params = (Parameters)decoder.readObject();
        decoder.close();

    } catch (FileNotFoundException e) {
        System.out.println(e.toString()); 
    }

4 Answers 4

1

Is the same version of Java being used on the Vista and XP boxes? Also check the version of Java used to create the xml originally.

Another test would be to encode an object on the XP box and try and read it back.

1
  • It's been a while:) Eventually i did just that, and it worked.
    – SimonV
    Jun 7, 2009 at 23:22
1

If it fails on your XP machine, but works on your Vista machine, then that sounds like some environment problem.

As it involves XML, I wonder if there's a character encoding issue and your Vista/XP environments have different encoding properties set. If this is the case then the XMLDecoder may not be able to parse the XML properly.

Check your system property file.encoding on both installations. It would be interesting to see if they're different. Does your .xml file specify the character encoding ?

2
  • the XML file specifies UTF-8 encoding. You were right about file.encoding not matching, I can't check the Vista machine right now but I don't think it matches. I've set file.encoding to UTF-8 on my machine and tried converting it to UTF-8 (with and without BOM), the decoded object is still null.
    – SimonV
    May 3, 2009 at 11:11
  • If the XML file specifies an encoding, then the VM encoding shouldn't matter May 3, 2009 at 11:15
0

That means that somehow your InputStream is considered 'empty' by the XmlDecoder.

Could you try:

XMLDecoder decoder = new XMLDecoder(new FileInputStream("Params.xml"));

using directly FileInputStream instead of BufferedInputStream ?

Or try to reset() your BufferedInputStream before using it in your XmlEncoder initialization ?

1
  • Just tried it with FileInputStream directly, the returned object is still null
    – SimonV
    May 3, 2009 at 10:21
0

You could try constructing your XMLDecoder with a custom ExceptionListener and put a println in it.

public XMLDecoder(InputStream in,
                  Object owner,
                  ExceptionListener exceptionListener)

Also, readObject throws ArrayIndexOutOfBoundsException if the stream contains no objects (or no more objects), are you catching that exception ?

1
  • I can trigger the ArrayIndexOutOfBoundsException if I call readObject() the second time. So something is read the first time.
    – SimonV
    May 3, 2009 at 11:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.