-1

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

}
RacerX142
  • 7
  • 2
  • 1
    `LocalDate birthday = LocalDate.of( year, month, dayOfMonth);` throws NPE because `month` is `null`. – matejko219 Jul 27 '20 at 13:15
  • You need to move the assignment to `birthday` inside the constructor, because field initializers run before the constructor code, so `month` hasn't been assigned yet in your current code. – Andreas Jul 27 '20 at 13:19

2 Answers2

2

It looks to me like you want to move the initialization of the variable 'birthday' down into your constructor for Author.

J.D. Luke
  • 66
  • 4
  • I have moved the the varible ```birthday``` into the constructed e.g. ( ```public Author(String firstname, String lastname, int year, Month month, int dayOfMonth, LocalDate birthday) { this.firstname= firstname; this.lastname= lastname; this.year = year; this.month = month; this.dayOfMonth=dayOfMonth; this.birthday = birthday; ```) now I am having trouble setting it in my demo class. I cannot finds the proper format to set the date anywhere.... – RacerX142 Jul 27 '20 at 13:50
  • There is a difference between declaration and assignment. Keep the declaration at the class level but move assignment into the constructor. Try to follow the pattern set by 'month' and the other instance variables. – J.D. Luke Jul 27 '20 at 14:09
1

You have a line

  LocalDate birthday = LocalDate.of( year, month, dayOfMonth);

now year and dayOfMonth are int and are initialized with 0, but month is object of Month calss and is null when the instance of Author class is being created. This is causing a null pointer.

So if you try to move the line in constructor like below:

    private String firstname;
    private String lastname;
    private int year;
    private Month month;
    private int dayOfMonth;
     LocalDate birthday = null;
    
    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;
    this.birthday = LocalDate.of( year, month, dayOfMonth);
    
    }

You will have an output like:

    Harry Potter and the Sorcerer's Stone by JK Rowling(birthday:1965-07-31)B017V4IMVQ11.99
    JK Rowling(birthday:1965-07-31)
Anshuman
  • 831
  • 5
  • 18
  • I have moved the the varible ```birthday``` into the constructed e.g. ( ```public Author(String firstname, String lastname, int year, Month month, int dayOfMonth, LocalDate birthday) { this.firstname= firstname; this.lastname= lastname; this.year = year; this.month = month; this.dayOfMonth=dayOfMonth; this.birthday = birthday; ``` now I am having trouble setting it in my demo class. I cannot finds the proper format to set the date anywhere.... – RacerX142 Jul 27 '20 at 13:52