简体   繁体   中英

Cannot instantiate the type error... but code is right?

So I have a big issue. I am getting the "cannot instantiate the type Robot" error when I try to make a new instance of the Robot class. I fully understand what this error means and when it occurs, but I do not understand why I am getting it. I swear, my code is correct. Maybe there is one small error in the code that I just do not see? Any help?

The code getting the error is the RobotWorld class:

public class RobotWorld extends JPanel{

Robot robot;

private static final long serialVersionUID = 1L;

    public RobotWorld(){
        robot = new Robot();
        this.setBackground(Color.GREEN);
    }
}

And the Robot.java class is:

public class Robot {

    public Robot(){

    }

}

I am getting the error on the line "robot = new Robot();"in RobotWorld.java code. Basically, anywhere I try to instantiate a new Robot object in RobotWorld, it will give me an error. I even tried doing it when I created the instance variable robot, still didn't work.

Both of these classes are in the same package, so I don't see how it can be some kind of class scope error. Can anyone tell me why I am getting this error?

Note, I restarted Eclipse a few times to make sure it wasn't just Eclipse acting up.

Thank you!

UPDATE:

It appears you may have imported the import com.sun.glass.ui.Robot; which is an abstract class and cannot be instantiated.

Be sure to remove any import statements for the wrong Robot class.

You may want to consider renaming your own Robot class as well to not get them confused.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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