• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with classes oop  RSS feed

 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a book class and a library class. the library class stores an array of books.
to simplify I am just providing the lines of code on which the error was produced.
if you would like the whole code please specify.


the error is on the if iteration.
am I using Objects.equals() incorrectly?
 
salvin francis
Bartender
Posts: 1653
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please provide the complete code. Also format the code to avoid confusion for others to read.
I have never seen a variation of equals where there are two input parameters. I doubt thats what you want.

 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:I have never seen a variation of equals where there are two input parameters. I doubt thats what you want.

It's from the Objects class.  Why not just test for bookname.equals(books[x].title)
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to compare bookname with books[x].title. if they are the same then it will mark the book as borrowed.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried bookname.equals(books[x].title)
I still get the same error
Exception in thread "main" java.lang.NullPointerException
at Library.borrowBook(Library.java:24)
at Library.main(Library.java:68)
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is looping through array elements 0 through maxBooks.  Since you have added 4 books, it is checking books[0], books[1], books[2], books[3], and books[4]. books[4] is going to be null.


Take a look at your other methods and see why you don't have the same problem with those.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the problem is that your loop in borrowBook() is not idiomatic for 0-based indexed arrays.  You wrote:

With 0-based indices, your termination condition should be x < maxBooks rather than x <= maxBooks. That's because if maxBooks is 1, the index for that book will be 0, if maxBooks is 2, the index for the last book will be 1, and so on.  Since you don't terminate the for-loop immediately after finding the book you're looking for, you continue down to the end of the list and when you get to x = maxBooks, you've gone past the last book you have in your array and try to deference a null.
 
Jordan King
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow! thanks Junilu. you made that make a lot of sense. problem fixed.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!