user2963584
user2963584

Reputation: 3

Reflection is not working

I am new to reflection. I am facing some error. Please help. Below is my code:

EmployeeClass.java:

public class EmployeeClass {

    private String empID;
    private String empName;

    public String getEmpID() {
        return empID;
    }

    public void setEmpID(String empID) {
        this.empID = empID;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public EmployeeClass(String empID, String empName) { 
        this.empID = empID;
        this.empName = empName;
    }

    public String getAllDetails() {
        return empID + " " + empName;
    }

}

ReflectionClass.java:

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionClass {

    public static void main(String[] args) {

        EmployeeClass emp = new EmployeeClass("1", "Emp1");
        Method method = null;
        try {
            method = emp.getClass().getMethod("getAllDetails", null);
            System.out.println(method.invoke(null, null));
        } catch (NoSuchMethodException | SecurityException
                | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException e) {
            System.out.println(e.getMessage());
        }


    }

}

While running the ReflectionClass.java, I'm getting the following error:

Exception in thread "main" java.lang.NullPointerException

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
                at java.lang.reflect.Method.invoke(Unknown Source)
                at myprgs.programs.ReflectionClass.main(ReflectionClass.java:14)

Upvotes: 0

Views: 200

Answers (3)

Nav
Nav

Reputation: 1

Changed the main method - method.invoke needs the employee object.

public static void main(String[] args) {

    Employee emp = new Employee("1", "Emp1");
    Method method = null;
    try {
        method = emp.getClass().getMethod("getAllDetails", null);
        System.out.println(method.invoke(emp, null));
    }
    catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.getMessage());
    }
}

Upvotes: 0

Sage
Sage

Reputation: 15408

 method = emp.getClass().getMethod("getAllDetails", null);
            System.out.println(method.invoke(null, null));

java.lang.reflect.Method.(Object obj, Object... args): The first argument is the object instance on which this particular method is to be invoked. However, the first argument should be null, If the method is static. So, you need to invoke with instance emp of EmployeeClass:

System.out.println(method.invoke(emp, null));

Again the second argument args of invoke(): (I am assuming i might have already know it), If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0or null.

Upvotes: 1

Gokul Nath KP
Gokul Nath KP

Reputation: 15553

You need to pass the object of the class (which contains your method) while calling invoke(), as shown below:

method.invoke(emp, null);

Change:

System.out.println(method.invoke(null, null));

To:

System.out.println(method.invoke(emp, null));

Upvotes: 5

Related Questions