tags:

views:

370

answers:

4

Hi all,

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()); 
    }
A: 

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 ?

VonC
Just tried it with FileInputStream directly, the returned object is still null
SimonV
+1  A: 

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 ?

Brian Agnew
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
If the XML file specifies an encoding, then the VM encoding shouldn't matter
Brian Agnew
A: 
JRL
I can trigger the ArrayIndexOutOfBoundsException if I call readObject() the second time. So *something* is read the first time.
SimonV
A: 

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.

objects
It's been a while:) Eventually i did just that, and it worked.
SimonV