• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

need help with classes oop

 
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?
 
Saloon Keeper
Posts: 2665
130
Google Web Toolkit Eclipse IDE 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


 
Marshal
Posts: 3181
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • 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
Marshal
Posts: 3181
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • 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.
 
Marshal
Posts: 15881
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • 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.
 
No, tomorrow we rule the world! With this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic