RunTime错误解决方案

时间:2014-05-21 16:35:08

标签: java runtime-error

我已经编译了所有类,并且我创建了一个Items数组(所有的超类),并且我放了不同类型的产品:

aItems[1] = new Motherboard("970a",2014,200.0,"GIGABYTE","INTEL",64,5);
aItems[2] = new Motherboard("gb4",2012,150.0,"ASROCK","AMD",32,4); 
aItems[3] = new Proccesor("I5",2010,180.0,"INTEL",3.3,4);
aItems[4] = new Proccesor("I7",2014,900.0,"INTEL",4.0,4); 
aItems[5] = new Gcard("RADEON",2012,300.0,"GIGABYTE","AMD",4) ;
aItems[6] = new Gcard("RADEON",2010,200.0,"SAPPHIRE","NVIDIA",2); 
aItems[7] = new Ram("IO",2010,100.0,"ASUS","DDR",4,1600);
aItems[8] = new Ram("RADIOACTIVE X",2012,160.0,"CRUCIAL","DDR3",8,2000); 
aItems[9] = new HardDrive("E2",2013,100.0,"WD","SSD",2.5,750);
aItems[10] = new HardDrive("LM",2012,150.0,"HP","HDD",3.5,1000) ; 
aItems[11] = new Monitor("CFA90",2014,180.0,"SAMSUNG","LCD","23,5","1080","HDMI");
aItems[12] = new Monitor("27EA",2013,280.0,"ASUS","LED","28","1080","HDMI"); 
aItems[13] = new Mouse("Taipan",2013, 80.0, "Razer","Optical", "wired");
aItems[14] = new Mouse("M9Q",2013, 80.0, "Microsoft","laser", "wireless");
aItems[15] = new Keyboard("Blackwidow", 2014, 130.0, "Razer", "wired");
aItems[16] = new Keyboard("GK10", 2013, 100.0, "CM Storm", "wireless");
aItems[17] = new Printer("PS3H", 2012, 90.0, "HP", "laser" , "colored");
aItems[18] = new Printer("SPP5", 2013, 190.0, "SAMSUNG", "inkjet" , "black and white");

然后我想获取数组的每个对象并通过调用方法得到我想要创建一个包含这些项的详细信息的txt文件。但它给了我这个运行时错误:

Exception in thread "main" java.lang.ClassCastException: Keyboard cannot be cast
 to Mouse
    at mainApp3.run(mainApp3.java:930)
    at acm.program.Program.runHook(Program.java:1592)
    at acm.program.Program.startRun(Program.java:1581)
    at acm.program.Program.start(Program.java:838)
    at acm.program.Program.start(Program.java:1250)
    at acm.program.Program.main(Program.java:1340)

1 个答案:

答案 0 :(得分:1)

您正在尝试将Keyboard类强制转换为Mouse。像

这样的东西
Keyboard kybrd = new Keyboard();
Mouse mse = (Mouse) kybrd;

这样的事情,我不确定你在那里有什么。

这导致了这次考验。

  

键盘无法转换为鼠标

您应该检查编译器在第1006行

显示的错误是什么
at mainApp3.run(mainApp3.java:1006)

它是对错误位置的引用。试着徘徊到它。