1

晚上好(ish?)我现在正在为课程创建一个程序,它将为 3 本书分配 3 组标题/作者。我有读书班、考试班和赞助班。到目前为止,顾客正在正确地从测试人员那里收集它的名字并将其归还。问题在于赞助人类,borrowBook 方法。测试人员初始化一个标题和姓名,创建一个赞助人,然后尝试打印borrowBook 方法的布尔结果。我从测试人员将标题和作者发送到赞助人的借书,尽管当借书方法尝试设置标题时我不断收到空指针异常,但我假设借书中的所有其他作者和与标题相关的方法也是如此。任何建议都非常感谢!

测试员类:

public class ProjectFiveSix {

public static void main(String[] args) {


    String title = "On the Origin of Species";
    String name = "Hugo";
    String author = "Charles Darwin";

    Patron patronOne = new Patron();

    System.out.print("The Patron's Name is: " + patronOne.getName(name));
    System.out.print("Test " + patronOne.borrowBook(author, title));

赞助人等级:

public class Patron {

private String name;
private Book book1;
private Book book2;
private Book book3;

public Patron(){
    name = "";
    book1 = null;
    book2 = null;
    book3 = null;
}
public String getName(String name){
    return name;
}
public boolean borrowBook(String title, String author){     
    if (book1 == null){
        book1.getTitle(title);
        book1.getAuthor(author);
        return true;    

    }else if (book2 == null){
        book2.getTitle(title);
        book2.getAuthor(author);
        return true;

    }else if (book3 == null){
        book3.getTitle(title);
        book3.getAuthor(author);
        return true;

    }else{
        return false;
    }   
   }    




public String toString(String str){
    str = name + "\n" + book1;
    return str;
}

}

书类:

public class Book {

private String title;
private String author;

public Book(){
    title = "";
    author = "";
}

public String getTitle(String title){
    title = title;
    return title;
}
public String getAuthor(String author){
    author = author;
    return author;
}

}

正如许多人所建议的那样,我尝试将borrowBook的书籍设置为!= null,并且它在某种程度上起作用。每本书在 public Patron(){ 中都设置为 null,因此该方法将返回 false。说得通!然而,想法是每本书都会从 null 开始,并且当 borrowBook 运行时,它会将 title 和 author 的当前值分配给它找到的第一本 null 书。我想我可以设置它,所以如果 borrowBook 返回 false,则将值分配给 Book1,尽管我不相信该方法可以用于书籍 2 和 3,因为它每次都会返回 true。非常感谢社区,你们是一个很大的帮助!

已回答 - 使用 - 这 - 在书中减少了冗余,并将随时修改值,很好的修复!创作一本新书也很有意义并且很有效,感谢所有的帮助。

4

6 回答 6

1

您检查是否bookN == null但最肯定的是您不能调用bookN.get[Title/Author]对象null

bookN != null如果我正确理解您的使用,您可能的意思是检查是否。

于 2013-11-15T01:17:27.227 回答
0

我认为在你的borrowBook方法中你的条件是相反的,你有:

public boolean borrowBook(String title, String author){     
    if (book1 == null){
        book1.getTitle(title);
        book1.getAuthor(author);
        return true;    

    }else if (book2 == null){
        book2.getTitle(title);
        book2.getAuthor(author);
        return true;

    }else if (book3 == null){
        book3.getTitle(title);
        book3.getAuthor(author);
        return true;

    }else{
        return false;
    }   
   } 

而且我会为您需要的每本书!=而不是==,否则如果bookX为空,bookX.<anything>则会导致NPE

于 2013-11-15T01:17:48.947 回答
0

看起来book1 是 null 并且你得到 NullPointerExceptionbook1.getTitle(title);

于 2013-11-15T01:20:13.353 回答
0

将您的Book构造函数更改为此

public Book(String title, String author){
    this.title = title;
    this.author = author;
}

在这种方法中,您应该创建一个Book

public boolean borrowBook(String title, String author){     
    if (book1 == null){
        book1 = new Book(title, author);
        book1.getTitle(title);   // I don't know what you need these for?
        book1.getAuthor(author); // ???
        return true;    

    }else if (book2 == null){
        book2 = new Book(title, author);
        book2.getTitle(title);
        book2.getAuthor(author);
        return true;

    }else if (book3 == null){
        book3 = new Book(title, author);
        book3.getTitle(title);
        book3.getAuthor(author);
        return true;

    }else{
        return false;
    }   
}
于 2013-11-15T01:22:58.413 回答
0

我看不到你在哪里做新书()。

另外我建议你重构你的代码。例如,我会像这样重构 Book 类

public class Book {
  private final String title;
  private final String author;

  public Book(String title, String author) {
     this.title = title;
     this.author = author;
  }

  public String getTitle() { return title; }
  public String getAuthor() { return author; }

}

同样的方式将“名称”作为参数传递给 Person 类的构造函数。

于 2013-11-15T01:24:41.083 回答
0

您正在尝试从 null 对象获取字段值,最好让您的书类构造函数为私有字段提供值

public Book(String title,String author){
    this.title = title;
    this.author = author;
} 
于 2013-11-15T01:39:14.057 回答