1

I have a problem right now in my program where a Student class is allowed 1 book and it must be stored in a variable _book however I can not seem to find a way to check if an object has already been instantiated or not without getting a run time error.

I have tried

  1. Comparing variable to null
  2. Accessing a function inside the variable that checks if the variable is null
  3. Accessing a function inside the variable that checks if variable is 0

Simplified Code:

Student Class

public class Student {
    private String _name;
    private Library _collegeLibrary;
    private LibraryCard _card;
    private TextBook _book;

    public Student(String name, Library library) {
        _name = name;
        _collegeLibrary = library;
        System.out.println("[Student] Name: " + _name);
    }

    public void describe() {
        String message;
        message = "[Student] " + _name;
        if (_book.returnTitle() == null) // returns java.lang.NullPointerException
            message += " does not have a book";
        else {
            message += " is borrowing the book \"" + _book.returnTitle() + "\"";
        }
        System.out.println(message);
    }
}

TextBook Class

public class TextBook {
    String _title;

    public TextBook(String title) {
        _title = title;
    }

    public String returnTitle() {
       return _title;
    }
}

The above code will give me a java.lang.NullPointerException. I looked into catching the error however it doesn't seem like that is recommended.

Enayet Hussain
  • 908
  • 4
  • 17
  • 33

1 Answers1

3

You are checking if _book.returnTitle() is null, however, this doesn't take in account for _book being null. You can check if _book is null instead. That should fix your nullpointer exception.

Also, you should always wrap your if-else clauses in curly brackets. That way it's easier to read.

Change this section of your code:

if (_book.returnTitle() == null) // returns java.lang.NullPointerException
        message += " does not have a book";
    else {
        message += " is borrowing the book \"" + _book.returnTitle() + "\"";
    }

To this:

if (_book == null) { // returns java.lang.NullPointerException
        message += " does not have a book";
    } else {
        message += " is borrowing the book \"" + _book.returnTitle() + "\"";
    }

Also, as a tip, you can override the toString function to do exactly what your describe function does:

   @Override
   public String toString() {
        String message;
        message = "[Student] " + _name;
        if (_book == null) { // returns java.lang.NullPointerException
            message += " does not have a book";
        } else {
            message += " is borrowing the book \"" + _book.returnTitle() + "\"";
        }
        return message;
    }

Usage:

public class SomeClass {
    public static void main(String[] args) {
        Student student = new Student("Student", new Library());
        System.out.println(student); //Because you override #toString() you can just println the Student object.
    }
}
nbokmans
  • 5,492
  • 4
  • 35
  • 59