简体   繁体   English

无法实例化类型错误...但是代码是对的吗?

[英]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.当我尝试创建 Robot 类的新实例时,出现“无法实例化类型 Robot”错误。 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:得到错误的代码是 RobotWorld 类:

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: Robot.java 类是:

public class Robot {

    public Robot(){

    }

}

I am getting the error on the line "robot = new Robot();"我在“robot = new Robot();”这一行收到错误消息in RobotWorld.java code.在 RobotWorld.java 代码中。 Basically, anywhere I try to instantiate a new Robot object in RobotWorld, it will give me an error.基本上,无论我尝试在 RobotWorld 中实例化一个新的 Robot 对象,它都会给我一个错误。 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.请注意,我重新启动了 Eclipse 几次以确保它不只是 Eclipse 在起作用。

Thank you!谢谢!

UPDATE:更新:

It appears you may have imported the import com.sun.glass.ui.Robot;看来您可能已经导入了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.您可能还需要考虑重命名自己的 Robot 类,以免混淆。

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

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