繁体   English   中英

无法弄清楚为什么在使用 java.localdate 设置月份时出现 null 指针异常

[英]Cant figure out why I get a null pointer exception while setting month using java.localdate

我正在创建一个使用 2 个类的程序,一个创建一个使用 java.localdate 构建的名称和生日的作者,另一个创建一个引用作者 class 的书。 使用演示 class 我试图设置作者的生日,但是当我使用Month.JULY设置月份时,我得到一个 null 指针异常,我不知道为什么。 这是我的代码:

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

}

在我看来,您想将变量“生日”的初始化下移到 Author 的构造函数中。

你有一条线

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

现在 year 和 dayOfMonth 是 int 并且初始化为 0,但是当创建 Author class 的实例时,月份是 Month calss 的 object 并且是 null。 这导致了 null 指针。

因此,如果您尝试在构造函数中移动该行,如下所示:

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

您将拥有 output ,例如:

    Harry Potter and the Sorcerer's Stone by JK Rowling(birthday:1965-07-31)B017V4IMVQ11.99
    JK Rowling(birthday:1965-07-31)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM