Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm... Why isn't this piece of code creating the array? :



 
Henry Wong
author
Marshal
Pie
Posts: 21127
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian Wimpsett wrote:hmm... Why isn't this piece of code creating the array?


Does the following create a Book object?

Book b;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic