简体   繁体   中英

Why is XMLDecoders readObject method throwing MalformedURLException when trying to read object?

I am trying to instantiate a Settings class. Also, I am using the Singleton pattern. Here is my code:

package com.op.OccupancyPrediction.BusinessLogic.utility;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Settings {


    private int preHeatTimeIntervalInMinutes;
    private double PIROccupiedValue;
    private int hoursPerDay;
    private int minutesPerHour;

    private static Settings instance = null;

    private Settings()
    {

    }

    public static Settings getInstance() {
          if(instance == null) {

              instance = new Settings();

             try {

                XMLDecoder d = new XMLDecoder(
                         null, new BufferedInputStream(
                             new FileInputStream("Settings.xml")));
                instance = (Settings) d.readObject();
                d.close();  
            } catch (FileNotFoundException e) {
                e.printStackTrace();        
            }

            return instance;

          }
          return instance;
    }

    public int getPreHeatTimeIntervalInMinutes() {
        return preHeatTimeIntervalInMinutes;
    }

    public void setPreHeatTimeIntervalInMinutes(int preHeatTimeIntervalInMinutes) {
        this.preHeatTimeIntervalInMinutes = preHeatTimeIntervalInMinutes;
    }

    public double getPIROccupiedValue() {
        return PIROccupiedValue;
    }

    public void setPIROccupiedValue(double pIROccupiedValue) {
        this.PIROccupiedValue = pIROccupiedValue;
    }

    public int getHoursPerDay() {
        return hoursPerDay;
    }

    public void setHoursPerDay(int hoursPerDay) {
        this.hoursPerDay = hoursPerDay;
    }

    public int getMinutesPerHour() {
        return minutesPerHour;
    }

    public void setMinutesPerHour(int minutesPerHour) {
        this.minutesPerHour = minutesPerHour;
    }
}

The XML document is placed in the root folder and looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.8.0" class="java.beans.XMLDecoder">
    <object class="com.op.OccupancyPrediction.BusinessLogic.utility.Settings">
        <void property="preHeatTimeIntervalInMinutes">
            <int>15</int>
        </void>
        <void property="PIROccupiedValue">
            <double>30</double>
        </void>
        <void property="hoursPerDay">
            <int>24</int>
        </void>
        <void property="minutesPerHour">
            <int>60</int>
        </void>
    </object>
</java>

It worked before I created the Settings class as a singleton.

Any suggestions?

This is the stacktrace:

java.net.MalformedURLException
Continuing ...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at java.beans.XMLDecoder.readObject(Unknown Source)
    at com.op.OccupancyPrediction.BusinessLogic.utility.Settings.getInstance(Settings.java:32)
    at com.op.OccupancyPrediction.BusinessLogic.engine.PreHeat.GenerateOccupancyVectorFromPIR(PreHeat.java:83)
    at com.op.OccupancyPrediction.BusinessLogic.engine.PreHeat.GenerateDaysFromPIR(PreHeat.java:43)
    at com.op.OccupancyPrediction.BusinessLogic.parser.PIRBereklyParser.Parse(PIRBereklyParser.java:49)
    at com.op.OccupancyPrediction.App.ParserTest(App.java:22)
    at com.op.OccupancyPrediction.App.main(App.java:14)

I found a solution so i will make an answer to my own question. It might be helpful to others. Because the constructor of the Settings class is not public, it will throw an exception when trying to call readObject. My solution:

package com.op.OccupancyPrediction.BusinessLogic.utility;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class SettingsWrapper {

    private static SettingsWrapper instance = null;
    private static Settings settings = null;

    private SettingsWrapper() {
        // Exists only to defeat instantiation.
    }

    public static SettingsWrapper getInstance() {
        if (instance == null) {
            instance = new SettingsWrapper();
        }
        return instance;
    }

    public Settings GetSettings() {
        if(settings == null)
        {
            settings = new Settings();
            try {

                XMLDecoder d = new XMLDecoder(new BufferedInputStream(
                        new FileInputStream("Settings.xml")));
                settings = (Settings) d.readObject();
                d.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();

            }
        }
        return settings;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM