2

I'm trying to use a toString method in LibraryCard to to access the name and email. However, when I use owner.getName() it gives me an null pointer exception error. I also am trying to make the cards a certain persons.

public class Student{ public String name; public String email; public Student(){ name = "UNASSIGNED"; email = "UNASSIGNED"; } public String getEmail(){ return email; } public String getName(){ return name; } public void setEmail(String newEmail){ email = newEmail; } public void setName(String newName){ name = newName; } } 

and

public class LibraryCard{ public Student owner; public int borrowCount; public LibraryCard(){ owner = null; borrowCount = 0; } public void checkOut(int bookNum){ borrowCount += bookNum; } public int getNumberOfooks(){ return borrowCount; } public Student getOwnerName(){ return owner; } public void setOwner(Student object){ owner = object; } public String toString(){ return "\nCard1 Info:\n\t Owner Name: " + owner.getName() + "\n\t Email: " + owner.getEmail() + "\n\t Books borrowed: " + borrowCount ; } } 

and the tester

public class LibraryCardTester{ public static void main(String args[]){ Student student1 = new Student(); student1.setName("James Johnson"); student1.setEmail(student1.getName()+"@gmail.edu"); System.out.println("Name: " + student1.getName()); System.out.println("Email: " +student1.getEmail()); Student student2 = new Student(); student2.setName("Barack Obama"); student2.setEmail(student1.Name()+"@gmail.edu"); System.out.println("Name: " + student2.getName()); System.out.println("Email: " +student2.getEmail()); LibraryCard card1 = new LibraryCard(); card1.checkOut(3); LibraryCard card2 = new LibraryCard(); card2.checkOut(2); LibraryCard card3 = new LibraryCard(); card3.checkOut(5); System.out.println(card1.toString()); System.out.println(card2.toString()); System.out.println(card3.toString()); } } 
1
  • 1
    Why not initialize the student's name and email, as well as the card's owner, in their respective constuctors? This could help you avoid forgetting to set them later on. Commented Feb 18, 2016 at 18:00

6 Answers 6

1

It looks to me you haven't set an owner on the library cards:

card1.setOwner(student1); 

Your constructor sets the owner to null, so unless you set an owner using the setOwner() method, the owner will always be null, meaning you are calling the getName() method on a null object causing a NullPointerException:

 public LibraryCard(){ owner = null; borrowCount = 0; } 
Sign up to request clarification or add additional context in comments.

Comments

0

Hi you never used the setOwner so the owner in the Library is always null

Comments

0

You write

 public LibraryCard(){ owner = null; // NULL! borrowCount = 0; } 

And never use setOwner(Student object) of course you have NullPointerException.

Use something like:

LibraryCard card1 = new LibraryCard(); card1.setOwner(student1); card1.checkOut(3); LibraryCard card2 = new LibraryCard(); card1.setOwner(student2); card2.checkOut(2); LibraryCard card3 = new LibraryCard(); card1.setOwner(student2); card3.checkOut(5); 

Comments

0

add owner = new Student(); in your LibraryCard class

Comments

0

It looks to me like you havent set a name for the cards, and you initialize then to null, giving you the null pointer exception..

 public LibraryCard(){ owner = null; //<==== here you set it to null, but never change it borrowCount = 0; } 

You need to set the owner

 card1.setOwner(student1); 

I might suggest you initialize the variable in the constructor when you create the object. it seems you would be creating a card if you don't know who will own it?

example....

 public LibraryCard(String _owner){ owner = _owner; borrowCount = 0; } 

Comments

0

Set the student object to the card by setting as owner and try once

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.