0

I'm trying to instantiate an object Robot knowing that the Robot constructor take a JPanel parameter using reflection.

Class<?> classRobot = null;
Constructor<?> constructorRobot;
classRobot = Class.forName(fullClassRobots.get(i));
constructorRobot = classRobot.getConstructor(JPanel.class);
robot.add((Robot)constructorRobot.newInstance(panelBattlefield));
public Robot(JPanel panelBattlefield) { 
...
}

My problem is that I keep getting a java.lang.NoSuchMethodException and I can't figure out what am I doing wrong.

zhrfrd
  • 11
  • 2
  • 1
    Is this robot you are talking about is `java.awt.Robot`? If so, `Robot` has no constructor that takes a `JPanel`, see for yourself https://docs.oracle.com/javase/7/docs/api/java/awt/Robot.html hence the `java.lang.NoSuchMethodException`, if `java.awt.Robot` is not what you were talking about, please provide more details as to which `Robot` you are talking about. – Doga Oruc Aug 16 '22 at 21:33
  • Also, this question looks horribly like an XY problem, here's a link that explains what an XY problem is and how to avoid asking bad questions. https://xyproblem.info/ – Doga Oruc Aug 16 '22 at 21:35
  • Looks like you want to dynamically instantiate subclasses of `Robot`. Are you sure those subclasses have a constructor that take a single `JPanel` as argument? – Johannes Kuhn Aug 16 '22 at 23:13

0 Answers0