英文:
Cant figure out why I get a null pointer exception while setting month using java.localdate
问题
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;
}
public String getLastName() {
return lastname;
}
public int getYear() {
return year;
}
public Month getMonth() {
return month;
}
public int getdayOfMOnth() {
return dayOfMonth;
}
public String toString() {
return firstname + " " + lastname + "(birthday:" + birthday + ")";
}
}
import java.time.Month;
import 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);
}
}
英文:
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);
}
}
答案1
得分: 2
看起来你想把变量 'birthday' 的初始化移到 Author 的构造函数中。
英文:
It looks to me like you want to move the initialization of the variable 'birthday' down into your constructor for Author.
答案2
得分: 1
你有一行代码
LocalDate birthday = LocalDate.of(year, month, dayOfMonth);
现在year
和dayOfMonth
是整数,初始化为0,但month
是Month
类的对象,在创建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) B017V4IMVQ 11.99
JK Rowling (birthday: 1965-07-31)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论