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;
}
}
}
Hi to everyone! I have a problem with reflection. I use this class as an universal runnable for various JFrame elements. 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!
Aucun commentaire:
Enregistrer un commentaire