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

OMGitzMidgar picture OMGitzMidgar · Mar 24, 2015 · Viewed 8.6k times · Source

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!

Answer

chrissukhram picture chrissukhram · Mar 24, 2015

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.