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

huangapple go评论153阅读模式
英文:

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

现在yeardayOfMonth是整数,初始化为0,但monthMonth类的对象,在创建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)

huangapple
  • 本文由 发表于 2020年7月27日 21:11:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63116075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定