晚上好(是吗?)我现在正在为课堂创建一个程序,将3组标题/作者分配给3本书。我有一本书课,测试课和守护课。到目前为止,顾客正在从测试仪中正确地收集其名称并将其返回。问题在于赞助人类,借书方法。测试人员初始化标题和名称,创建一个顾客,然后尝试打印borrowBook方法的布尔结果。我从测试人员那里将标题和作者发送给借书人,但是当借阅方法尝试设置标题时我仍然得到一个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,而且它在某种程度上起作用。在公共Patron(){中,每本书都设置为null,因此该方法将出现错误。说得通!然而,这个想法是每本书都会从null开始,当借阅书运行时,它会将标题和作者的当前值分配给它找到的第一个空书。我想我可以设置它,所以如果borrowBook返回false,为Book1分配值,虽然我不相信该方法可以用于第2和第3本书,因为它会在每次跟随时返回true。非常感谢社区,你们是一个很好的帮助!
已回答 - 使用 - this - in book减少了冗余,并将随时修改值,很好的修复!感谢所有帮助,创建一本新书也很有意义并且有效。
答案 0 :(得分:1)
您检查bookN == null
是否,但肯定无法在bookN.get[Title/Author]
对象上拨打null
。
如果我理解你的用法正确,你可能想要检查bookN != null
是否正确。
答案 1 :(得分: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
答案 2 :(得分:0)
看起来book1
为空,您在book1.getTitle(title);
答案 3 :(得分: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;
}
}
答案 4 :(得分: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类的构造函数中将“name”作为参数传递。
答案 5 :(得分:0)
您正尝试从null对象获取字段值 最好让你的book类构造函数为私有字段提供值
public Book(String title,String author){
this.title = title;
this.author = author;
}