【问题标题】:Can't find NullPointerException找不到 NullPointerException
【发布时间】:2013-11-15 01:13:20
【问题描述】:

晚上好(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。非常感谢社区,你们帮了大忙!

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

【问题讨论】:

  • 你能复制过去的异常堆栈跟踪吗
  • 您的 getTitlegetAuthor 方法毫无意义。您只是返回传递给他们的值
  • 事实上你可以看看堆栈跟踪。它会准确地告诉您问题出在哪里,然后修复应该是显而易见的。
  • 很抱歉,不确定您的要求。尽管“book1.getTitle(title);”上出现异常我假设如果我要解决这个问题,那么所有后续的 getTitles 和 getAuthors 都会出现更多异常,直到我弄清楚发生了什么

标签: java string nullpointerexception


【解决方案1】:

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

你的意思可能是检查bookN != null,如果我理解你的用法正确的话。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      我认为在您的 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

      【讨论】:

        【解决方案4】:

        看起来 book1 为 null,而您在 book1.getTitle(title); 处收到 NullPointerException

        【讨论】:

          【解决方案5】:

          我没有看到你在哪里做 new Book()。

          另外我建议你重构你的代码。例如,我会像这样重构 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 类的构造函数中传递“name”作为参数。

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2021-04-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多