简体   繁体   中英

Unexpected NullPointerException using Javassist

I run the following code :

    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

And this NPE is raised :

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)

This question solves my problem. For some reason there's a bug in javassist 3.1.2.GA. So here is my error and the solution to it :

WRONG : Prone to bug

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

CORRECT : No bug

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM