使用Javassist的意外NullPointerException

时间:2011-08-09 16:13:37

标签: java nullpointerexception javassist

我运行以下代码:

    CfField f = ...
    CtClass classeEnglobante = f.getDeclaringClass();
    ClassPool pool = classeEnglobante.getClassPool();
    ConstPool constPool = classeEnglobante.getClassFile().getConstPool();

    AnnotationsAttribute attr = new AnnotationsAttribute(constPool , AnnotationsAttribute.visibleTag);
    Annotation a = new Annotation(constPool, pool.get("org.hibernate.annotations.Index"));
    a.addMemberValue("name", new StringMemberValue("idx_" + p.getNomMinuscule(), constPool));
    attr.addAnnotation(a); // Here is the line 245

这个NPE被提出来了:

java.lang.NullPointerException
    at javassist.bytecode.annotation.ArrayMemberValue.write(ArrayMemberValue.java:132)
    at javassist.bytecode.annotation.Annotation.write(Annotation.java:317)
    at javassist.bytecode.AnnotationsAttribute.setAnnotations(AnnotationsAttribute.java:246)
    at com.mycompany.MyClass (MyClass.java:245)

1 个答案:

答案 0 :(得分:1)

This question解决了我的问题。 出于某种原因,javassist 3.1.2.GA中存在一个错误。 所以这是我的错误和解决方案:

错误:容易犯错误

Annotation a = new Annotation(constPool, pool.get("org.hibernate.annotations.Index"));

正确:没有错误

Annotation a = new Annotation("org.hibernate.annotations.Index", constPool);