2013-11-08 3 views
0

Я начинаю писать свои собственные классы, и до сих пор у меня есть авторский класс, который имеет две строки (имя и фамилию) и два целых числа (год рождения и смерти). Теперь я написал еще один класс, книги, и у него есть авторский объект с именем bookAuthor. Итак, это мой код, вы можете помочь исправить мое исключение 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 
    } 

метод .getAuthor() является методом класса Book, который возвращает автор этой книги мой вопрос заключается в том, чтобы рассказать свою программу «если что-то было установлено, то сделать это», например, в мой код. Я пытаюсь передать свою программу, если заголовок был установлен для книги, а затем вернуть заголовок ... кроме того, если год публикации был установлен, верните заголовок, как описано выше.

+0

Можете ли вы опубликовать трассировку стека ошибки? – jagmohan

+0

, если вы имели в виду, где произошла ошибка, я сделал в качестве комментария к этой строке – user2888031

+0

либо ваш bookAuthor имеет значение null, либо getAuthor() возвращает нуль –

ответ

0
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; 
} 
Смежные вопросы