I am creating a program that uses 2 classes, one that creates an author with a name and birthday that is constructed using java.localdate and a second that creates a book that refers to the author class. Using a demo class I am attempting to set the birthday of the author but when I set the month using Month.JULY
I get a null pointer exception and I have no idea why. Here is my code:
public class Book {
private String name;
private Author author;
private String ISBN;
private double price;
public Book(String name, Author author, double price, String ISBN) {
this.name = name;
this.author = author;
this.price = price;
this.ISBN = ISBN;
}
public String getName() {
return name;
}
public Author getAuthor() {
return author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}
public String toString() {
return name + " by " + author + ISBN + price;
}
}
import java.time.LocalDate;
import java.time.Month;
public class Author {
private String firstname;
private String lastname;
private int year;
private Month month;
private int dayOfMonth;
LocalDate birthday = LocalDate.of( year, month, dayOfMonth);
public Author(String firstname, String lastname, int year, Month month, int dayOfMonth) {
this.firstname= firstname;
this.lastname= lastname;
this.year = year;
this.month = month;
this.dayOfMonth=dayOfMonth;
}
public String getFirstName() {
return firstname;
}
/** Returns the gender */
public String getLastName() {
return lastname;
}
/** Returns the email */
public int getYear() {
return year;
}
public Month getMonth() {
return month;
}
public int getdayOfMOnth() {
return dayOfMonth;
}
/** Returns a self-descriptive String */
public String toString() {
return firstname + " " + lastname + "(birthday:" + birthday + ")";
}
}
import java.time.Month;
import java.time.localDate:
//I tried with and without java.time.localDate
public class testBook {
public static void main(String[] args) {
Author jkr = new Author("JK","Rowling",1965,Month.JULY,31);
Book HPSS = new Book("Harry Potter and the Sorcerer's Stone", jkr, 11.99, "B017V4IMVQ" );
System.out.println(HPSS);
System.out.println(jkr);
}
}