Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't find NullPointerException
    text
    copied!<p>Good evening(ish?) I'm just now creating a program for class that will assign 3 sets of title/author to 3 books. I have a book class, test class, and patron class. So far the patron is properly gathering its name from the tester and returning it. The problem lies in patron class, borrowBook method. The tester initializes a title and name, creates a patron, and then attempts to print the boolean result of the borrowBook method. I send title and author to the borrowBook in patron from the tester, though i keep getting a nullpointerexception when the borrowBook method attempts to set a title, i assume the same is true for all other author &amp; title related methods in the borrowBook. Any advise is much appreciated!</p> <p>Tester Class:</p> <pre><code>public class ProjectFiveSix { public static void main(String[] args) { String title = "On the Origin of Species"; String name = "Hugo"; String author = "Charles Darwin"; Patron patronOne = new Patron(); System.out.print("The Patron's Name is: " + patronOne.getName(name)); System.out.print("Test " + patronOne.borrowBook(author, title)); </code></pre> <p>Patron Class:</p> <pre><code>public class Patron { private String name; private Book book1; private Book book2; private Book book3; public Patron(){ name = ""; book1 = null; book2 = null; book3 = null; } public String getName(String name){ return name; } public boolean borrowBook(String title, String author){ if (book1 == null){ book1.getTitle(title); book1.getAuthor(author); return true; }else if (book2 == null){ book2.getTitle(title); book2.getAuthor(author); return true; }else if (book3 == null){ book3.getTitle(title); book3.getAuthor(author); return true; }else{ return false; } } public String toString(String str){ str = name + "\n" + book1; return str; } } </code></pre> <p>Book Class:</p> <pre><code>public class Book { private String title; private String author; public Book(){ title = ""; author = ""; } public String getTitle(String title){ title = title; return title; } public String getAuthor(String author){ author = author; return author; } } </code></pre> <p>As many have suggested, i tried setting the borrowBook's books to != null instead, and it worked to some extent. Each book is set to null in the public Patron(){, so the method will come up false. Makes sense! However the idea was that each book would start off null, and when borrowBook runs, it would assign the current values of title and author to the first null book it finds. I suppose i could set it up so if borrowBook returns false, assign values to Book1, though i dont believe that method could then be used for books 2 and 3, as it would return true every time following. Great thanks to the community though, you guys are a great help!</p> <p>Answered - Using the - this - in book reduced the redundancy and will modify the value as i go, great fix! Creating a new book also makes sense and worked, thanks for all the help. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload