Class 如何比较类中的两个对象

Class 如何比较类中的两个对象,class,object,methods,compare,Class,Object,Methods,Compare,我开始编写自己的类,到目前为止,我有一个author类,它有两个字符串(名字和姓氏)和两个整数(出生年份和死亡年份)。现在我已经编写了另一个类books,它有一个名为bookAuthor的author对象。这是我的代码,你能帮我修复NullPointerException吗 public class Author{ String authorFirstName; String authorLastName; int authorBirth; int

我开始编写自己的类,到目前为止,我有一个author类,它有两个字符串(名字和姓氏)和两个整数(出生年份和死亡年份)。现在我已经编写了另一个类books,它有一个名为bookAuthor的author对象。这是我的代码,你能帮我修复NullPointerException吗

public class Author{
      String authorFirstName;
      String authorLastName;
      int authorBirth;
      int authorDeath;

      //creates a new author object with nothing set
      public Author(){
      }

      //String l=last name, String f=frist name
      //creates new author object by setting the last and first name
      public Author(String l, String f){
        authorLastName=l;
        authroFirstName=f;
      }

      //returns a string that is the first name of the author
      public String getFirstName(){
        return authorFirstName;
      }

      //return a string that is the last name of the author
      public String getLastName(){
        return authorLastName;
      }

      //int b=year of birth for author
      //sets the birth year of author and returns nothing
      public void setDates(int b){
        if (b<-2000){
          authorBirth=-40000;
        }else{
          authorBirth=b;
        }
      }

      //int b,d represent the authors year of birth and death respectively
      //returns nothing but sets the authors year of death and birth
      public void setDates(int b, int d){
        if(d>=b||birth<-2000){
          authorBirth=-4000;
          authorDeath==-4000;
        }else{
          authorBirth=b;
          authorDeath=d;
        }
      }

      //returns an int that represents the birth year of the author
      public int getBirth(){
        return authorBirth;
      }

      //returns an int that represents the death year of the author
      public int getDeath(){
        return authorDeath;
      }

      //compares two authors and if they have the same first and last name then it returns 0.
      //it also returns 0 if one of the authors appreviates his or her name by the first letter of their first name.
      public int compareTo(Author a1){
        int compareValue=1;
        if(a1.getFirstName().equals(authorFirstName)&&a1.getLastName().equals(authorLastName)){
          compareValue=0;
        }
        if(a1.charAt(0)==authorFirstName.charAt(0)&&a1.getLastName().equals(authorLastName)){
          compareValue=0;
        }
        return compareValue;
      }

      //returns a String in the form of the authors name by last name, first name
      public String toString(){
        String name=authorLastName+", "+authorFirstName;
        return name;
      }

      //returns a string of the last name, first name of the author
      //if the birth year is known it returns last name, first name of the author but input birth by (b.____)
      //if birth and death are known then returns last name, first name of the author with (birth-death)
      public String printAuthorInfo(){
        String name=authorLastName+", "+authorFirstName;
        if(authorBirth=this.getBirth()){
          name=name+" (b. "+authorBirth+")";
        }
        if(authorDeath=this.getDeath()){
          name=authorLastName+", "+authorFirstName+"("+authorBirth+"-"+authorDeath+")";
        }
        return name;
      }

      //end of Author class
    }
    public class Book{
      int bookYearPublish;
      Author bookAuthor;
      String bookNumberISBN;
      String bookTitle;

      public Book(){
      }

      //String t is the title of the book
      //creats a Book object with a title
      public Book(String t){
        bookTitle=t;
      }

      //String t is the title of the book
      //Author a is the author of the book
      //creats a Book object with a title and author
      public Book(String t, Author a){
        bookTitle=t;
        bookAuthor=a;
      }

      //String t is the title of the book
      //creates a title for a Book object
      //returns nothing but sets the title of the Book object
      //if no title was set then the title is "TITLE NOT SET"
      public void setTitle(String t){
        if (t.length()==0){
          bookTitle="TITLE NOT SET";
        }else{
          bookTitle=t;
        }
      }

      //returns a String that represents the title of the Book
      public String getTitle(){
        return bookTitle;
      }

      //Author a is the author that will be set for the Book
      //sets the author of the book
      public void setAuthor(Author a){
        bookAuthor=a;
      }

      //returns the Author of the Book
      public Author getAuthor(){
        return bookAuthor;
      }

      //int p is the year of publication of the Book
      //returns nothing
      //sets the year of publication for the Book
      public void setYear(int p){
        if (p<-2000||p>2018){
          bookYearPublish=-4000;
        }else{
          bookYearPublish=p;
        }
      }

      //returns an int that represents the year it was published
      public int getYear(){
        return bookYearPublish;
      }

      //String i is the ISBN number
      //returns nothing but sets the ISBN for the book
      //if the ISBN number is not length 13 or 10 the ISBN is "ISBN NOT SET"
      public void setISBN(String i){
        if(i.length()==13||i.length()==10){
          bookNumberISBN=i;
        }else{
          bookNumberISBN="ISBN NOT SET";
        }
      }

      //returns a String that represents the ISBN number of the book
      public String getISBN(){
        return bookNumberISBN;
      }

      //Book b is the book that is being checked if it has the same author
      //returns a boolean true if a book has two authors who are the same
      public boolean sameAuthor(Book b){
        boolean compareValue=false;
        if(b.getAuthor().compareTo(bookAuthor)==0){
          compareValue=true;
        }
      return compareValue;
      }

      //Book b is the book that is being compared to by the ISBN number
      //compares two books by relating thier ISBN numbers and returns 0 is they are the same
      public int compareTo(Book b){
        int compareValue=1;
        if(bookNumberISBN.equals(b.getISBN())){
          compareValue=0;
        }
        return compareValue;
      }

      //returns the title of Book
      //if the author is known returns a String in the form of title. last name, first name of author
      //if the year is known returns a String in the form title (year). last name, first name of author
      public String toString(){
        String title=bookTitle;
        if(bookAuthor.equals(this.getAuthor())){
          title=title+". "+bookAuthor;
        }
        if(bookYearPublish.equals(this.getYear())){
          title=bookTitle+" ("+bookYearPublish+"). "+bookAuthor;
        }
        return title;
      }

      //ends class Book
    }
公共类作者{
字符串authorFirstName;
字符串authorLastName;
intauthorbirth;
作者死亡;
//创建未设置任何内容的新作者对象
公共作者(){
}
//字符串l=姓,字符串f=首名
//通过设置姓和名创建新的作者对象
公共作者(字符串l、字符串f){
authorLastName=l;
authroFirstName=f;
}
//返回作为作者名字的字符串
公共字符串getFirstName(){
返回authorFirstName;
}
//返回作为作者姓氏的字符串
公共字符串getLastName(){
返回authorLastName;
}
//int b=作者的出生年份
//设置作者的出生年份,不返回任何内容
公共无效设置日期(int b){

如果(b=b | | | birth),您可以发布错误的堆栈跟踪吗?如果您是指错误发生的位置,我在该行上做了注释,您的bookAuthor为null或getAuthor()返回null。发布您的完整代码。可能代码与您的逻辑不符
public String toString(){

      String title= (bookTitle==null) ? "" : bookTitle;

      if (getYear() != null && this.getYear().equals(bookYearPublish)) {
           title +=" ("+bookYearPublish+")";
      }

      title += ".";

      if (getAuthor() != null && this.getAuthor().equals(bookAuthor)) {
          title +=" "+bookAuthor;
      }

      return title;
}