0

This is my first time using this site so hopefully this makes sense. I have my code below and when I execute it its giving me a NullPointerException on line 38 in my Library class,

(the line says if (items[i].getTitle() != null && items[i].getTitle().equals(title)) {)

and another NullPointerException on like 29 in my MainMethod class,

(line is if (loaned == true) {).

To check the contents of items[i], after I added an item I had printed in the console to see if it was null and it successfully printed so I am confused as to why the NullPointerExpcetion is present. Any and all help will be greatly appreciated.

public class MediaItem {

private String title;
private String format;
public boolean onLoan;
public String loanedTo;
public String dateLoaned;

MediaItem() {
    onLoan = false;
    loanedTo = null;
    dateLoaned = null;
    title = null;
    format = null;
}

MediaItem(String title, String format) {
    this.title = title;
    this.format = format;
}

public String getTitle() {
    return this.title;
}

public String setFormat(String format) {
    this.format = format;
    return this.format;
}

public String getFormat() {
    return format;
}

void markOnLoan(String name, String date) {
    if (onLoan == true) {
        System.out.println(title + " is already on loan to " + loanedTo);
    } else {
        onLoan = true;
        loanedTo = name;
        dateLoaned = date;
    }
}

void markReturned() {

}

}

import java.util.Scanner;

public class Library {

int numberOfItems = 0;
MediaItem[] items = new MediaItem[100];

int displayMenu() {
    Scanner s = new Scanner(System.in);
    System.out.println(
            "1. Add an item \n2. Mark an item as on loan \n3. List all items \n4. Mark an item as returned \n5. Quit \n\nWhat would you like to do?");
    int choice = s.nextInt();
    return choice;
}

void addNewItem(String title, String format) {
    items[numberOfItems] = new MediaItem(title, format);

    numberOfItems++;

}

void markItemOnLoan(String title, String name, String date) {
    for (int i = 0; i <= 100; i++) {
        if (items[i].getTitle() == title) {
            items[i].onLoan = true;
            items[i].loanedTo = name;
            items[i].dateLoaned = date;
        }
    }

}

boolean checkIfLoaned(String title) {
    char loaned = 'N';
    System.out.println(items[0].getTitle());
    for (int i = 0; i < 100; i++) {
        if (items[i].getTitle() != null && items[i].getTitle().equals(title)) {
            if (items[i].onLoan) {
                String personName = items[i].loanedTo;
                System.out.println(title + " is already on loan to " + personName);
                loaned = 'Y';
            }
        }

    }
    if (loaned == 'Y') {
        return true;
    } else {
        return false;
    }
}

}

import java.util.Scanner;

public class MainMethod {

public static void main(String[] args) {
    int choice = 1;
    String personName;
    String mediaName;
    String format;
    String loanDate;
    Scanner s = new Scanner(System.in);
    Library l = new Library();

    while (choice != 5) {
        choice = l.displayMenu();
        switch (choice) {
        case 1:
            System.out.println("What is the title?");
            mediaName = s.nextLine();
            System.out.println("What is the format?");
            format = s.nextLine();
            l.addNewItem(mediaName, format);
            break;
        case 2:
            System.out.println("Which item (enter the title)?");
            mediaName = s.nextLine();
            System.out.println("Who are you loaning it to?");
            personName = s.nextLine();
            boolean loaned = l.checkIfLoaned(mediaName);

            if (loaned == true) {
                break;
            }
            System.out.println("When did you loan the item?");
            loanDate = s.nextLine();
            l.markItemOnLoan(mediaName, personName, loanDate);
        }

    }
}

}

biz
  • 53
  • 6

3 Answers3

0

it would be better if you post the callstack too. 3 potential errors I can see are :

  1. items[i] might be null. Although you are checking for null on items[X].getTitle()... You don't perform a null check on items[i]

  2. Your for loop goes from 0 to 100. Is it gauranteed that all elements in array from 0-99 index are populated ? Shouldn't you go from 0 to items.length ?

  3. Your array is defined for 100 elements. You are trying to access 101st elements in the for loop (i<=100) should be i < 100.

Siddharth Garg
  • 1,560
  • 14
  • 19
0

I am assuming this is homework, so I will steer you in the right direction, as opposed to posting code.

  1. You are iterating through a hard-coded value of 100 items in both markItemOnLoan and checkIfLoaned. Just because you declare an array of 100 items, does not mean they are all initialized. Consider altering your loops to iterate through numberOfItems instead of 100. You should also do a check in addNewItem to ensure you won't surpass 100 items.
  2. Related to #1, you do not check if the actual item you are extracting from the array is null, so you're attempt to call a method getTitle on a null Object, naturally throws a NPE.

Side note, in markItemOnLoan, you're incorrectly comparing Strings. The way you compare titles in checkIfLoaned is correct.

ryanlutgen
  • 2,951
  • 1
  • 21
  • 31
0

Why is there a NullPointerException when there is no null?

It cannot happen.

From this we deduce that there is a null. That is simple logic.

If you are going to succeed in debugging a program MUST NOT deny what the evidence tells you.

So where is the null?

 if (items[i].getTitle() != null && items[i].getTitle().equals(title)) { 

Based on this line alone there are four possible explanations:

  1. items is null
  2. items[i] is null
  3. another thread is changing something ... and you got unlucky.
  4. the getTitle() method is returning something that is not a String or that is not the same String each time.

We can eliminate the first and fourth explanations by reading the code, and the third one is extremely unlikely (even supposing that it is possible).

That leaves the second explanation as the "working hypothesis".

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216