Reputation: 21
the below errors have been solved however upon entering the author I am displayed with this message:
"
Exception in thread "main" java.lang.NullPointerException
at manualLibrary.Library.add(Library.java:21)
at manualLibrary.Menu.main(Menu.java:25)
" Is there any reason why this happens?
I am new to java and have been assigned some tasks to complete as follows:
I have been working through these tasks and have almost reached the end however I think my Menu class has errors present which prevents it from running. Here are the 4 different classes I am using:
Manual class: http://pastebin.com/z31QBKf9
Console class: http://pastebin.com/B4XFZESF
Library class: http://pastebin.com/Gx25iDwA
Menu class:
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!");
}
}
}
}
If anyone has any ideas as to how I can fix the errors present please help, I have been working really hard on this and would love to get my code functioning.
Here are the errors eclipse throws up:
"
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)
"
Thank you!
Upvotes: 0
Views: 300
Reputation: 2907
The error is pretty self-explanatory, but if you can't understand it, here goes...
1.) You try to call the method Console.askOption()
with a String
parameter, however no such method exists - you need to define it. However, it looks like you want Console.askChar()
.
2.) You try to call the method ask()
with a String
parameter on your Manual
instance, but the method you defined has no parameters. Use plain one.ask();
instead, or it looks like here you want one.print("Enter manual details: ");
.
3.) You try to assign an undeclared variable manual
under the case 'F':
, this should probably be Manual manual = new Manual();
instead of manual = new Manual();
.
4.) You try to call the method addManual()
with a Manual
parameter on your Library
instance (no such method exists) - it looks like you wanted manuals.add(one);
5.) You try to call the method print
with a String
parameter on your Library
instance (no such method exists) - it looks like you wanted manuals.print();
I think that's it.
Upvotes: 1