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