Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';找不到NullPointerException_Java_String_Nullpointerexception - Fatal编程技术网

Java Can';找不到NullPointerException

Java Can';找不到NullPointerException,java,string,nullpointerexception,Java,String,Nullpointerexception,晚上好(ish?)我正在为课堂创建一个程序,为3本书指定3套标题/作者。我有一个图书班、测试班和赞助人班。到目前为止,用户正从测试人员处收集其名称并返回。问题在于用户类、借书方法。测试人员初始化标题和名称,创建用户,然后尝试打印借阅簿方法的布尔结果。我将title和author从测试人员发送到用户中的借阅簿,尽管当借阅簿方法尝试设置标题时,我一直得到nullpointerexception,但我假设借阅簿中所有其他与作者和标题相关的方法也是如此。任何建议都将不胜感激 测试仪等级: public

晚上好(ish?)我正在为课堂创建一个程序,为3本书指定3套标题/作者。我有一个图书班、测试班和赞助人班。到目前为止,用户正从测试人员处收集其名称并返回。问题在于用户类、借书方法。测试人员初始化标题和名称,创建用户,然后尝试打印借阅簿方法的布尔结果。我将title和author从测试人员发送到用户中的借阅簿,尽管当借阅簿方法尝试设置标题时,我一直得到nullpointerexception,但我假设借阅簿中所有其他与作者和标题相关的方法也是如此。任何建议都将不胜感激

测试仪等级:

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

}
正如许多人建议的那样,我尝试将借书设置为!=相反,它为null,并且在某种程度上起作用。在公共用户()中,每本书都设置为空{,所以这个方法会出现错误。这是有道理的!不过,我们的想法是,每本书的开头都是null,当借阅书运行时,它会将title和author的当前值分配给它找到的第一本null的书。我想我可以设置它,这样,如果借阅书返回false,就将值分配给Book1,尽管我不相信这个方法可以n用于第二册和第三册,因为它将在以后的每一次都返回真的。非常感谢社区,你们是一个伟大的帮助


回答-使用书中的-this-in减少了冗余,并且会在我进行时修改值,很好的修复!创建一本新书也很有意义并且有效,感谢所有的帮助。

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


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

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


如果我正确理解您的用法,您可能是想检查
bookN!=null

我认为在您的
借书
方法中,您的条件是相反的,您有:

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。
将导致
NPE

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

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.
将导致
NPE

看起来像
book1
为空,并且你在
book1.getTitle(标题)处获得空点异常

看起来
book1
为空,您在
book1.getTitle(title);

构造函数更改为此

public Book(String title, String author){
    this.title = title;
    this.author = author;
}
在这种方法中,您应该创建一本

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

将您的
书籍
构造函数更改为此

public Book(String title, String author){
    this.title = title;
    this.author = author;
}
在这种方法中,您应该创建一本

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

我看不出你在哪里写新书

我还建议您重构代码

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

}

用同样的方法将“name”作为Person类构造函数中的参数传递。

我看不出你在哪里做newbook()

我还建议您重构代码

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”作为参数。

您试图从null对象获取字段值 最好让book类构造函数向私有字段提供值

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

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

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

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