有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

nullpointerexception无法理解为什么在使用java设置月份时出现null指针异常。本地日期

我正在创建一个使用2个类的程序,其中一个类创建一个使用java构造的具有姓名和生日的作者。localdate和创建引用author类的书籍的第二个。使用demo类,我试图设置作者的生日,但是当我使用Month.JULY设置月份时,我得到了一个空指针异常,我不知道为什么。这是我的密码:

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

}

共 (2) 个答案

  1. # 1 楼答案

    你有台词吗

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

    现在year和dayOfMonth是int,并用0初始化,但month是month Cals的对象,在创建Author类的实例时为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);
        
        }
    

    您将获得如下输出:

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

    在我看来,您希望将变量'birth'的初始化向下移动到Author的构造函数中