更新
然而,在输入作者时,以下错误已经得到解决,并显示了以下消息:
“
Exception in thread "main" java.lang.NullPointerException
at manualLibrary.Library.add(Library.java:21)
at manualLibrary.Menu.main(Menu.java:25)
“有什么原因吗?
我是java新手,被指派完成以下任务:
我一直在处理这些任务,并已接近尾声,但是我认为我的菜单类存在错误,从而阻止了它的运行。下面是我使用的4个不同的类:
手动类:http://pastebin.com/z31QBKf9
控制台类:http://pastebin.com/B4XFZESF
库类:http://pastebin.com/Gx25iDwA
菜单类:
package manualLibrary;
public class Menu
{
public static void main(String[] args)
{
// Initialise container
//Manuals manuals = new Manuals();
Library manuals = new Library();
// declare boolean for while loop
boolean finished = false;
char option;
// while loop
while(! finished)
{
// ask option using menu choices
option = Console.askOption("Select: A)dd, P)rint, Q)uit");
// implement switch option
switch (option)
{
// implement cases
// add
case 'A':
Manual one = new Manual();
one.ask("Enter manual details: ");
manuals.addManual(one);
break;
// find
case 'F':
manual = new Manual();
String manualserialNumber = manual.askserialNumber("\nEnter the serial number for the manual: ");
if ( manuals.find( manualserialNumber) != null)
System.out.println("Manual found is: " +manuals.find(manualserialNumber));
else
System.out.println("Error: Manual not found! \n");
break;
// print
case 'P':
manuals.print("Stored manuals are: ");
break;
// quit menu case
case 'Q':
finished = true;
System.out.println("End of application, goodbye!");
break;
// default case
case '\0':
break;
default:
System.out.println("Invalid entry!");
}
}
}
}
如果有人对我如何修复当前的错误有任何想法,请帮助我,我一直在努力工作,希望我的代码能够正常运行。
编辑
下面是eclipse抛出的错误:
“
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method askOption(String) is undefined for the type Console
The method ask() in the type Manual is not applicable for the arguments (String)
The method addManual(Manual) is undefined for the type Library
manual cannot be resolved to a variable
manual cannot be resolved
at manualLibrary.Menu.main(Menu.java:17)
“
谢谢!
发布于 2014-10-29 20:50:36
这个错误是不言而喻的,但是如果你不能理解它,下面是……
1.)您尝试用一个Console.askOption()
参数调用String
方法,但是不存在这样的方法--您需要定义它。但是,看起来您需要Console.askChar()
。
2.)您尝试使用ask()
实例上的String
参数调用方法Manual
,但是您定义的方法没有参数。使用普通的one.ask();
代替,或者看起来像这里您想要one.print("Enter manual details: ");
。
3.)尝试在case 'F':
下分配一个未声明的变量case 'F':
,这可能是Manual manual = new Manual();
而不是manual = new Manual();
。
4.)您尝试使用addManual()
实例上的Manual
参数调用方法Library
(不存在这种方法)--看起来您想要manuals.add(one);
5.)您尝试使用print
实例上的String
参数调用方法Library
(不存在这种方法)--看起来您想要manuals.print();
我想就是这样了。
https://stackoverflow.com/questions/26639734
复制相似问题