![](/img/trans.png)
[英]NullPointerException in JAXBContext.newInstance()
[英]JAXBContext.newInstance with generic class as input
我有几个这样的JavaBean(省略了getter和setter):
@Entity
@XmlRootElement(name = "talent")
public class Talent extends BaseObject implements Serializable {
String gruppe;
String art;
}
和它们的xml文件,都喜欢
<body>
<item>
<gruppe>a</gruppe>
<art>b</art>
</item>
<item>
<gruppe>a</gruppe>
<art>b</art>
</item>
</body>
现在我想将这些xml文件读入我的对象。 对于清单,我编写了一个通用包装器类,如下所示:
@XmlRootElement(name = "body")
public class GenericWrapper<E extends BaseObject> {
private ArrayList<E> talentListe;
public ArrayList<E> getTalentListe() {
return talentListe;
}
@XmlElement(name = "item")
public void setTalentListe(ArrayList<E> talentListo) {
this.talentListe = talentListo;
}
}
当我要实例化JAXBContext时,会发生问题:
JAXBContext context = JAXBContext.newInstance(GenericWrapper.class);
Unmarshaller um = context.createUnmarshaller();
GenericWrapper<Talent> gw = (GenericWrapper<Talent>) um.unmarshal(new FileReader(STORE_FILENAME));
List<Elementar> list = gw.getTalentListe();
然后出现错误消息:
Exception in thread "main" javax.xml.bind.UnmarshalException: Unable to create an instance of struktur.BaseObject
如果我不使我的wrapperclass通用,一切正常。 我想问题是,当我想从我的通用包装器类中获取JAXBContext.newInstance时,未设置通用部分,因此它尝试使用BaseObject中最基本的构造函数,这是抽象的。生成wrapper.class作为GenericWrapper实例的方法? 他使用Talent的构造函数,而不是我的抽象BaseObject。
PS:我确定我忘记了很多必要的信息。 请写下,我将提供所需的一切。 如果说其他方法似乎更合理,我也愿意听。 但总的来说,这个问题的答案会很好。
编辑:BaseObject只是一个MappedSuperclass和struktur包:
@MappedSuperclass
@PrimaryKeyJoinColumn(name = "colConfig")
public abstract class BaseObject {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
int id;
private String name;
@Column(columnDefinition = "TEXT")
private String beschreibung;
我们不知道什么是BaseObject或struktur ... https://stackoverflow.com/help/mcve
话虽如此,按照JAXB和抽象类 ,您可以尝试仅将impl类注释为@XmlRootElement,将抽象类注释为@XmlTransient
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.