简体   繁体   English

找不到NullPointerException

[英]Can't find NullPointerException

Good evening(ish?) I'm just now creating a program for class that will assign 3 sets of title/author to 3 books. 晚上好(ish?)我正在为课堂创建一个程序,为3本书分配3组标题/作者。 I have a book class, test class, and patron class. 我有一个读书班,一个测试班和一个赞助人班。 So far the patron is properly gathering its name from the tester and returning it. 到目前为止,顾客已经从测试人员那里正确地收集了它的名字并返回了它。 The problem lies in patron class, borrowBook method. 问题出在顾客类,roweBook方法上。 The tester initializes a title and name, creates a patron, and then attempts to print the boolean result of the borrowBook method. 测试人员将初始化标题和名称,创建一个赞助人,然后尝试打印借款书方法的布尔结果。 I send title and author to the borrowBook in patron from the tester, though i keep getting a nullpointerexception when the borrowBook method attempts to set a title, i assume the same is true for all other author & title related methods in the borrowBook. 我将标题和作者从测试人员发送到赞助人的roweBook中,尽管当roweBook方法尝试设置标题时,我不断收到nullpointerexception,但我认为对于roweBook中所有其他与作者和标题相关的方法也是如此。 Any advise is much appreciated! 任何建议深表感谢!

Tester Class: 测试人员类别:

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

Patron Class: 赞助人类别:

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

}

Book Class: 书类:

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

}

As many have suggested, i tried setting the borrowBook's books to != null instead, and it worked to some extent. 正如许多人所建议的那样,我尝试将roweBook的书设置为!= null,并且在某种程度上起作用了。 Each book is set to null in the public Patron(){, so the method will come up false. 在公共Patron(){中,每本书均设置为null,因此该方法将为false。 Makes sense! 说得通! However the idea was that each book would start off null, and when borrowBook runs, it would assign the current values of title and author to the first null book it finds. 但是,这样做的想法是,每本书都将以null开始,并且在运行rowerBook时,会将当前的title和author值分配给找到的第一本null书籍。 I suppose i could set it up so if borrowBook returns false, assign values to Book1, though i dont believe that method could then be used for books 2 and 3, as it would return true every time following. 我想我可以进行设置,以便如果roweBook返回false,则为Book1分配值,尽管我不相信该方法可以用于第二本书和第二本书,因为每次之后它都会返回true。 Great thanks to the community though, you guys are a great help! 非常感谢社区,你们们提供了很大的帮助!

Answered - Using the - this - in book reduced the redundancy and will modify the value as i go, great fix! 已回答-使用-本-书中的内容减少了冗余,并会在我行进时修改值,很好的解决! Creating a new book also makes sense and worked, thanks for all the help. 谢谢您的帮助,创作一本新书也很有意义而且很有效。

You check whether bookN == null but most certainly you cannot call bookN.get[Title/Author] on a null object. 您检查bookN == null是否bookN == null但最肯定不能在null对象上调用bookN.get[Title/Author]

What you probably meant was to check whether bookN != null , if I understand your use correctly. 如果我正确理解了您的用法,您可能要检查的是bookN != null

I think in your borrowBook method your conditions are reversed, you have: 我认为在您的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;
    }   
   } 

And i htink for each book you need != instead of == , otherwise if bookX is null then bookX.<anything> will cause NPE 而且我认为每本书都需要!=而不是== ,否则,如果bookX为null,则bookX.<anything>将导致NPE

貌似book1为空,你在得到NullPointerException异常book1.getTitle(title);

Change your Book constructor to this 将您的Book构造函数更改为此

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

In this method, you should create a Book 用这种方法,您应该创建一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;
    }   
}

I dont see where you are doing new Book(). 我看不到您在哪里做新的Book()。

Also i would suggest you to refactor your code. 我也建议您重构您的代码。 eg I would refactor Book class like this 例如我会像这样重构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; }

}

Same way pass "name" as parameter in the constructor of Person class. 以相同的方式将“ name”作为参数传递给Person类的构造函数。

You are trying to get field value from null object it is better to let your book class constructor supply value to the private field 您尝试从空对象获取字段值,最好让您的书类构造函数为私有字段提供值

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM