I am trying to write a program that simulates a simple libraray. I am having some trouble applying some of the methods to the main method.
Here is my code so far:
Here are the main errors that I am getting:
borrowBook(Book) in Library cannot be applied to (java.lang.String)
firstLibrary.returnBook("The Lord of The Rings")
printAvailbleBooks(Book) in Library cannot be applied to ()
System.out.println(secondLibrary.printAvailableBook())
array required but Book found
System.out.println(b[numBooks])
a non-static method printOpeningHours() cannot be referenced from a static context
printOpeningHours()
Also the borrow and return book methods reference another method in my book class.
How can I fix these errors?
and
How could I write this code so that it will mark a book as borrowed and print out "Sorry that book is checked out" when ever someone tries to borrow it again?
Simply, you are not calling your methods with the correct type of parameters. For example, your returnBook() method takes a Book object, yet you pass it a String object. To fix it, pass a Book object. Or modify your method to take a String object.
Henry
This message was edited 2 times. Last update was at by Henry Wong
Thanks for replying. Ok, so I tried to insert book into my methods so that: firstLibrary.returnBook(Book("The Lord of the Rings")); . Now it is telling me that the symbol for Book isn't found. I though this was the proper return type?
I have also tried to change it to firstLibrary.borrowBook(books[1]); and it is telling me a "non-static varible cannot be referenced from a static context".
Brian Wimpsett wrote:Thanks for replying. Ok, so I tried to insert book into my methods so that: firstLibrary.returnBook(Book("The Lord of the Rings")); . Now it is telling me that the symbol for Book isn't found. I though this was the proper return type?
Book is a class -- not a method. What is it that you are trying to do?
Brian Wimpsett wrote:I have also tried to change it to firstLibrary.borrowBook(books[1]); and it is telling me a "non-static varible cannot be referenced from a static context".
That error message pretty much explains itself. You cannot access an instance variable from a static method -- as a static method doesn't have access to an instance (this reference).
Henry
This message was edited 2 times. Last update was at by Henry Wong
Thanks, Henry. I fixed my code and it compiled. Excuse me for keeping this thread open, I am pretty new at Java. When I ran it it throws the run time error of : java.lang.NullPointer. at Library.addBook(Library.java:10) ; at Library.main(Library.java:60). I looked online and this exception was caused with an illegal use of null. Like if you try to divide a null variable with a number.
I looked online and this exception was caused with an illegal use of null. Like if you try to divide a null variable with a number.
Whatever site that you "looked online", please don't use that site again -- as the explanation is a bunch of gibberish.
When I ran it it throws the run time error of : java.lang.NullPointer. at Library.addBook(Library.java:10) ; at Library.main(Library.java:60).
Follow the trace -- look at the addBook() method at line 10 of Library.java. Basically, null pointer exceptions are caused when you try to use something that doesn't exist. Which is this case, tried to use an array that you never instantiated. Just look at the line, and you should easily see which variable you are trying to use.
Ok. I am kind of lost. I am not sure how my variable for addBook is wrong. I am creating a new instance of the book class like my method addBook is supposed to do. I tried to remove static on my books varible and I also removed void and it is still giving me the nullPointer exception. How do I change my constructor or variable so that it builds an array?