Stephen Biz Bizimana Stephen Biz Bizimana - 3 years ago 167
Java Question

Java: Why is there a NullPointerException when there is no null?

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);
}

}
}


}

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download