为什么XMLDecoders readObject方法在尝试读取对象时抛出MalformedURLException?

时间:2014-11-06 17:14:25

标签: java

我正在尝试实例化一个Settings类。另外,我使用的是Singleton模式。这是我的代码:

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;
    }
}

XML文档放在根文件夹中,如下所示:

<?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>

在我将Settings类创建为单例之前,它有效。

有什么建议吗?

这是堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,所以我会回答我自己的问题。它可能对其他人有帮助。因为Settings类的构造函数不是公共的,所以在尝试调用readObject时会抛出异常。我的解决方案:

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;
    }

}