Java反射调用不起作用

时间:2015-08-13 16:32:24

标签: java swing reflection

public class RunnableSupport implements Runnable {

private MyClient myClient;
private String frameType;

Method[] methodList;
Constructor<?> constructor;
Class<?> myClass;

private JFrame mainFrame;
private boolean mustClose;
private int width;
private int height;
private int x;
private int y;

public RunnableSupport(MyClient myClient, String frameType, int width,
        int height, int x, int y, boolean mustClose) {
    this.myClient = myClient;
    this.frameType = frameType;
    this.height = height;
    this.width = width;
    this.x = x;
    this.y = y;
    this.mustClose = mustClose;
}

@Override
public void run() {
    try {
        myClass = Class.forName("it.polimi.social.frame." + frameType);
        constructor = myClass
                .getDeclaredConstructor(new Class[] { MyClient.class });
        methodList = myClass.getDeclaredMethods();

        mainFrame = (JFrame) constructor.newInstance(myClient);
        mainFrame.setSize(width, height);
        mainFrame.setLocation(x, y);
        if (mustClose)
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setVisible(true);
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}

public JFrame getFrame() {
    return mainFrame;
}

public void invokeMethod(String method) {
    for (int i = 0; i < methodList.length; i++) {
        if (methodList[i].getName() == method) {
            try {
                methodList[i].invoke(mainFrame);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}

我有反思问题。我将此类用作各种JFrame元素的通用runnable。出于这个原因,我需要通过反射调用方法。当我尝试调用方法时,我收到此错误:

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 it.polimi.social.frame.RunnableSupport.invokeMethod(RunnableSupport.java:83)
    at it.polimi.social.client.MyClient.main(MyClient.java:56)
即使它似乎没事也是如此。问题是当我尝试调用该方法时...我还检查了其他问题,但它们都是不同的问题,即使是类似的问题。谢谢!

编辑:我已经解决了这个问题,谢谢大家。

现在我有另外一个问题,我已经做了什么。拥有一个使用反射而不是为每个JFrame类具有不同runnable的runnable是一个好主意吗?我担心的是,每次调用方法时都使用单个类意味着在方法列表中进行搜索。

1 个答案:

答案 0 :(得分:1)

在这一行:

if (methodList[i].getName() == method)

永远不要将字符串与==进行比较。尝试将其更改为.equals()并查看是否有帮助。