Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java注释不工作方法错误_Java_Oop_Annotations - Fatal编程技术网

Java注释不工作方法错误

Java注释不工作方法错误,java,oop,annotations,Java,Oop,Annotations,我正在做一个注释,其目的是使类必须是不可变的。以下是处理器的代码: 最终输出显示错误 /*Exception in thread "main" java.lang.NullPointerException at Annotation.Meta.myMeth(Meta.java:27) at Annotation.Meta.main(Meta.java:34)8*/ import java.lang.annotation.Retention; import java.lang.reflect.M

我正在做一个注释,其目的是使类必须是不可变的。以下是处理器的代码:

最终输出显示错误

/*Exception in thread "main" java.lang.NullPointerException
at Annotation.Meta.myMeth(Meta.java:27)
at Annotation.Meta.main(Meta.java:34)8*/

import java.lang.annotation.Retention;
import java.lang.reflect.Method;

// An annotation type declaration.  
@Retention(RetentionPolicy.RUNTIME)


@interface MyAnno{

    String str();

int val();

}

class Meta {

    // Annotate a method.
    @MyAnno(str = "Annotation Example", val = 100)

    public static void myMeth() {
        Meta ob = new Meta();
        // Obtain the annotation for this method
        try {
            // First, get a Class object that represents
            Class< ? > c = ob.getClass();

            // Now, get a Method object that represents
            Method m = c.getMethod("myMeth");

            // Next, get the annotation for this class.
            MyAnno anno = m.getAnnotation(MyAnno.class);

            // Finally, display the values.
            System.out.println(anno.str() + " " + anno.val());

        }
        catch (NoSuchMethodException exc) {
            System.out.println("Method Not Found.");
        }

    }

    public static void main(String args[]) {
        myMeth();
    }
}
/*线程“main”java.lang.NullPointerException中的异常
位于Annotation.Meta.myMeth(Meta.java:27)
位于Annotation.Meta.main(Meta.java:34)8*/
导入java.lang.annotation.Retention;
导入java.lang.reflect.Method;
//注释类型声明。
@保留(RetentionPolicy.RUNTIME)
@接口MyAnno{
字符串str();
int val();
}
类元{
//注释一个方法。
@MyAnno(str=“注释示例”,val=100)
公共静态无效myMeth(){
Meta ob=新的Meta();
//获取此方法的注释
试一试{
//首先,获取一个表示
Class<?>c=ob.getClass();
//现在,获取一个表示
方法m=c.getMethod(“myMeth”);
//接下来,获取该类的注释。
MyAnno-anno=m.getAnnotation(MyAnno.class);
//最后,显示值。
System.out.println(anno.str()+“”+anno.val());
}
捕获(NoSuchMethodException){
System.out.println(“未找到方法”);
}
}
公共静态void main(字符串参数[]){
myMeth();
}
}

对我来说效果很好:您的代码正在工作……我在Annotation.Meta.myMeth(Meta.java:37)Annotation.Meta.main(Meta.java:46)的线程“main”java.lang.NullPointerException中使用Eclipse mars 2异常时出错。如果问题不可重现,我们将无法帮助您。您需要找出您在此处发布的代码中的不同之处(这可能已经导致了问题)。