簡體   English   中英

為什么在嘗試讀取對象時XMLDecoders readObject方法拋出MalformedURLException?

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

我正在嘗試實例化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)

我找到了解決方案,所以我將回答我自己的問題。 這可能對其他人有幫助。 因為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;
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM