调用 Method.invoke 时获取 java.lang.NullPointerException [英] Getting java.lang.NullPointerException when calling Method.invoke

查看:54
本文介绍了调用 Method.invoke 时获取 java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个关于 Java annotaitons 的教程 并实现了 Test 注释,如图所示.但是当运行代码时,我得到以下输出.

I'm following this tutorial on Java annotaitons and implemented the Test annotation as shown there. But when running the code I get the following output.

java.lang.NullPointerException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at TestAnnotationParser.parse(Demo.java:24)
    at Demo.main(Demo.java:51)
Passed:0   Fail:1

以下是我的代码.有人能指出我做错了什么吗?

Following is my code. Can someone point out what I have got wrong?

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface Test {
    Class expected();
}

class TestAnnotationParser {
    public void parse(Class<?> clazz) throws Exception {
        Method[] methods = clazz.getMethods();
        int pass = 0;
        int fail = 0;

        for (Method method : methods) {
            if (method.isAnnotationPresent(Test.class)) {
                Test test = method.getAnnotation(Test.class);
                Class expected = test.expected();
                try {
                    method.invoke(null);
                    pass++;
                } catch (Exception e) {
                    if (Exception.class != expected) {
                        e.printStackTrace();
                        fail++;
                    } else {
                        pass++;
                    }
                }
            }
        }
        System.out.println("Passed:" + pass + "   Fail:" + fail);
    }
}

class MyTest {

    @Test(expected = RuntimeException.class)
    public void testBlah() {
    }
}

public class Demo {
    public static void main(String[] args) {
        TestAnnotationParser parser = new TestAnnotationParser();
        try {
            parser.parse(MyTest.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

推荐答案

您传递给 invoke 的参数必须是调用该方法的对象,除非该方法是 static.你通过反射所做的相当于:

The parameter that you pass to invoke must be an object on which the method is invoked, unless the method is static. What you did through reflection is equivalent to this:

MyTest obj = null;
obj.testBlah();

自然有一个 NPE.要解决此问题,请传递一个对象以在其上调用该方法,或将该方法设为 static.

Naturally, there's an NPE. To fix this problem, pass an object on which to invoke the method, or make the method static.

这是一种修复方法:

public <T> void parse(Class<T> clazz, T obj) throws Exception {
    Method[] methods = clazz.getMethods();
    int pass = 0;
    int fail = 0;

    for (Method method : methods) {
        if (method.isAnnotationPresent(Test.class)) {
            Test test = method.getAnnotation(Test.class);
            Class expected = test.expected();
            try {
                method.invoke(obj);
                pass++;
            } catch (Exception e) {
                if (Exception.class != expected) {
                    e.printStackTrace();
                    fail++;
                } else {
                    pass++;
                }
            }
        }
    }
    System.out.println("Passed:" + pass + "   Fail:" + fail);
}

...

parser.parse(MyTest.class, new MyTest());

ideone 上的演示.

这篇关于调用 Method.invoke 时获取 java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆