0

我从 XStream 库中观察到非常奇怪的行为,同时读取它生成的文件并在结果上使用 Patter.compile 运行时它会给出空指针异常,但如果我在调试模式下逐步运行它,它工作正常
我把它缩小到 Pattern.complie 如果我只是将值读取到一个字符串变量,它工作正常,如果我使用 Pattern.complie(".*") 和 unmarshal 方法它也工作正常
我使用 Java 7 和 XStream 1.4 .4
我认为这是由于 reader.getAttribute("key") 返回 null 但在调试时不返回任何想法为什么会发生这种情况?
这是我所拥有的:
我的班级

import java.util.regex.Pattern;
public class A {
private Pattern key;
ArrayList<A> subTree;
public A(HierarchicalStreamReader reader, UnmarshallingContext context){
    this.key=Pattern.compile(reader.getAttribute("key"));
            // String a = reader.getAttribute("key") - Works fine
            // this.key=Pattern.compile(a) - Breaks   
    subTree=new ArrayList<A>();
    reader.moveDown();
    while(reader.hasMoreChildren()) {
        subTree.add(new A(reader,context));
    }
    reader.moveUp();
}
}

XStream 转换器类

public class AConverter implements Converter {
    public boolean canConvert(Class clazz) {
        return A.class.isAssignableFrom(clazz);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {

    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        reader.moveDown();
        A n = new A(reader,context);
        reader.moveUp();
        return n;
    }

}

测试用例

public void testA() throws FileNotFoundException, IOException, ClassNotFoundException{
    XStream xstream = new XStream();
    xstream.alias("MessageFieldMappings", A.class);
    xstream.registerConverter(new AConverter());
    ObjectInputStream in = xstream.createObjectInputStream(new FileInputStream("test1.xml"));
    A rootNode = (A) in.readObject();
    in.close();

}

测试文件(test1.xml)

<object-stream>
<MessageFieldMappings>
<RootMapping>
  <FieldMap key=".*">
    <FieldMap key="A.*">
      <FieldMap key="AB.*"/>
      <FieldMap key="AC.*"/>
    </FieldMap>
    <FieldMap key="D.*"/>
  </FieldMap>
</RootMapping>
</MessageFieldMappings>
</object-stream>

我得到的错误

com.thoughtworks.xstream.converters.ConversionException: null : null
---- Debugging information ----
cause-exception     : java.lang.NullPointerException
cause-message       : null
class               : nz.orion.monkPraser.praser.fieldConverter.A
required-type       : nz.orion.monkPraser.praser.fieldConverter.A
converter-type      : nz.orion.monkPraser.praser.fieldConverter.AConverter
path                : /MessageFieldMappings/RootMapping
line number         : 3
version             : null
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
...
4

1 回答 1

1

线索在

path                : /MessageFieldMappings/RootMapping
line number         : 3

您已MessageFieldMappings为 class 起别名A,因此当它看到 a 时MessageFieldMappings,它将调用转换器,该转换器将对moveDown元素RootMapping并尝试从中提取key属性。它没有,因此是 NPE。

您需要在转换器中进行额外的 moveDown/Up 级别:

reader.moveDown(); // RootMapping
reader.moveDown(); // First FieldMap
A n = new A(reader,context);
reader.moveUp();
reader.moveUp();
return n;
于 2013-08-28T07:46:39.587 回答