w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories

Getting java.lang.NullPointerException when calling Method.invoke


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();

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

Here is one way to make a fix:

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());

Demo on ideone.


Categories : Java

Related to : Getting java.lang.NullPointerException when calling Method.invoke
NullPointerException while trying to invoke method on private field using Reflection
The code is correct as written, but who says that request isn't supposed to be null? Show us the default constructor of your resource class. (Note: if you're relying on some injection to happen, it won't. Injection frameworks only work when you don't circumvent them.)

Categories : Java
NullPointerException in CXF REST invoke method when passing a null parameter
The @WebParam annotation is only applicable for JAX-WS. Assuming you are trying to send the data as in an HTML form post, you should annotate the parameter with the JAX-RS @FormParam. public boolean uploadConfig(@FormParam("configContent") String configContent); A null value will result in no parameter being sent with the request.

Categories : Java
I am getting nullpointerexception when calling a method to set a variable using element of array to call the method
int en = employees[i].getEmployeeNumber(); //Exception is at this line Make sure you initialize all the elements in employees array. You just initialized employees array but not its elements. public EmployeeManager() { employees = new Employee[employeeMax]; for(int i = 0; i< employees.length; i++) { // initialize elements employees[i] = // new Employee(); }

Categories : Java
grails unit test - java.lang.NullPointerException: Cannot invoke method finish() on null object
try: grails test-app TransactionController.testTransactionAnalytics you forgot the "test" in front of the method name... and yes... it seems, you don't have to write it in the classname, but in the methodname you have to...

Categories : Unit Testing
NullPointerException issue when calling method inside run method
We need to know what "line 57" is, if you can please provide the class in which swTimerDisplay is called. Without this, from here I can only see two possibilities which you should check if you haven't already. Your reference to StopwatchTimer is null, i.e. has not been instantiated, when you call swTimerDisplay(). In effect you'd be doing null.swTimerDisplay(). In the final line of swTimerDispla

Categories : Java
© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.