简体   繁体   English

Java反射调用不起作用

[英]Java reflection invoke doesn't work

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;
        }
    }
}

I have a problem with reflection. 我对反射有疑问。 I use this class as an universal runnable for various JFrame elements. 我将此类用作各种JFrame元素的通用可运行对象。 For this reason I need to invoke methods via reflection. 因此,我需要通过反射来调用方法。 When I try to invoke a method I get this 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 it.polimi.social.frame.RunnableSupport.invokeMethod(RunnableSupport.java:83)
    at it.polimi.social.client.MyClient.main(MyClient.java:56)

even if it seems to be all right. 即使看起来还可以。 The problem is when I try to invoke the method... I've also checked other questions but they were all different problems, even if similiar. 问题是当我尝试调用该方法时...我也检查了其他问题,但它们都是不同的问题,即使是类似的问题。 Thank you! 谢谢!

EDIT: I've fixed the problem, thank to everyone. 编辑:我已经解决了问题,谢谢大家。

Now I have another question regarding what I've done. 现在,我对已完成的工作还有另一个疑问。 Is it a good idea to have a single runnable that uses reflection instead of having a different runnable for each JFrame class? 最好有一个使用反射的可运行对象,而不是每个JFrame类都使用不同的可运行对象? What concerns me is that using a single class each time that I invoke a method implies searching in the method list. 我担心的是,每次调用一个方法都使用一个类意味着在方法列表中进行搜索。

On this line: 在这行上:

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

You should never compare strings with ==. 您永远不要将字符串与==进行比较。 Try changing this to .equals() and see if that helps. 尝试将其更改为.equals(),看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM