This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Getting a lot of Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting a lot of "method cannot be applied to (java.lang.string)"" Watch "Getting a lot of "method cannot be applied to (java.lang.string)"" New topic
Author

Getting a lot of "method cannot be applied to (java.lang.string)"

Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
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?

Thanks a lot,
Brian
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

How can I fix these errors?


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
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?

Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
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".
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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?

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
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.

Why is it telling me this?

Anyways, here is my final code:



Thank you for your patience,
Brian
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

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.

Henry
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
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?
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
the problem isn't that you didn't create the book object it is that you are trying to add it to an array you haven't created yet.

You declared the reference to the array but never created the array object.
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
hmm... Why isn't this piece of code creating the array? :



Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18917
    
  40

Brian Wimpsett wrote:hmm... Why isn't this piece of code creating the array? :


Maybe you can start with the Sun tutorial on arrays...

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

Henry
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
Brian Wimpsett wrote:hmm... Why isn't this piece of code creating the array?


Does the following create a Book object?

Book b;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting a lot of "method cannot be applied to (java.lang.string)"