对象的二维数组YAML到Java

时间:2014-04-25 17:23:23

标签: java arrays yaml

我正在尝试在Java中反序列化我在YAML中放在一起的2D数组。我正在使用Yamlbeans库。

这是我的YAML:

states:
 - 0:
  - name: SPAWNINGSTATE
    duration: 4

这是我的班级:

public class State {
    public String name;
    public long duration;

    public State(){}

    public State(String name, long duration){
        this.name = name;
        this.duration = duration;

    }
}

数组在另一个只有一行的类中声明:

public State[][] states;

这是我得到的错误:

com.esotericsoftware.yamlbeans.YamlReader$YamlReaderException: Line 57, column 5: Error creating object.
    at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:266)
    at com.esotericsoftware.yamlbeans.YamlReader.readValue(YamlReader.java:152)
    at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:335)
    at com.esotericsoftware.yamlbeans.YamlReader.readValue(YamlReader.java:152)
    at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:295)
    at com.esotericsoftware.yamlbeans.YamlReader.readValue(YamlReader.java:152)
    at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:103)
    at com.esotericsoftware.yamlbeans.YamlReader.read(YamlReader.java:90)
    at com.sbg.wickedtemplate.layers.ForegroundLightning.<init>(ForegroundLightning.java:22)
    at com.sbg.wickedtemplate.World.<init>(World.java:25)
    at com.sbg.wickedtemplate.LWP_Engine.create(LWP_Engine.java:39)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: java.lang.reflect.InvocationTargetException: Unable to find a no-arg constructor for class: [Lcom.sbg.wickedtemplate.State;
    at com.esotericsoftware.yamlbeans.Beans.createObject(Beans.java:111)
    at com.esotericsoftware.yamlbeans.YamlReader.createObject(YamlReader.java:363)
    at com.esotericsoftware.yamlbeans.YamlReader.readValueInternal(YamlReader.java:264)

我没有问题制作常规数组。为什么它告诉我它找不到一个无参数的构造函数,当它显然在那里?我做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以尝试修复其中一个节点吗?

    states:
       0:
         name: SPAWNINGSTATE
         duration: 4
&#34; - &#34;使它成为一个阵列,它可能会把它扔掉。