为什么我的ArrayList中的对象的属性返回null



我正在用Java为(书籍(库编写代码。我有一个Book类,它有三个属性:Title、Author和Genre。流派存储为int,因为没有那么多:

public class Book {
public  String title;

public  String author;
public  int genre;

public Book() {
this.title = title;
this.author = author;
this.genre = genre;
}
public void setBookTitle(String newBookTitle) {
this.title = newBookTitle;
}
public void setBookAuthor(String newBookAuthor) {
this.author = newBookAuthor;
}
public void setBookGenre(int newBookGenre) {
this.genre = newBookGenre;
}
public String getBookTitle() {
return  title;
}
public String getBookAuthor() {
return author;
}
public int getBookGenre() {
return genre;
}
public String toString() {
LibraryDatabase libraryDatabase = new LibraryDatabase();

return ("Title: "+this.getBookTitle() +
"Author: " +this.getBookAuthor()+"Genre: " + this.getBookGenre() //edit this part to refer the integer to a String
);
}

}

我有一个图书馆数据库类,用于存储书籍的ArrayList:


import java.util.*;

public class LibraryDatabase extends Book {
ArrayList<Book> bookDatabase;
public LibraryDatabase() {

}
public void books() {
Book book1 = new Book();

book1.setBookTitle("Harry Potter");
book1.setBookAuthor("J.K. Rowling");
book1.setBookGenre(1);
}

public ArrayList<Book> getArrayList() {
return bookDatabase;
}

}

最后,我有一个客户类,它基本上只是将以上所有内容组织成一个用户友好的界面:

import java.io.*;
import java.util.*;
public class Customer {

public Customer() {
}
public void run() {
System.out.print("Welcome to Generic Library Service. Please enter your name.");
Scanner kbEater = new Scanner(System.in);
String customerName = kbEater.nextLine();
System.out.println("Would you like to borrow, return, or donate a book?");
System.out.println("    1. Borrow");
System.out.println("    2. Return");
System.out.println("    3. Donate");
Scanner kbNeeder = new Scanner(System.in);
int customerChoice = kbEater.nextInt();
switch (customerChoice) {
case 1: borrow();
break;
//case 2: returnBook();
//case 3: donate();
}
}
public void printGenres() {

System.out.println("    1. Fantasy");
System.out.println("    2. Science Fiction");
System.out.println("    3. Dystopian");
System.out.println("    4. Action & Adventure");
System.out.println("    5. Mystery");
System.out.println("    6. Horror");
System.out.println("    7. Thriller & Suspense");
System.out.println("    8. Historical Fiction");
System.out.println("    9. Romance");
System.out.println("    10. Graphic Novel");
System.out.println("    11. Young Adult");
System.out.println("    12. Children's");
System.out.println("    13. Memoir & Biography");
System.out.println("    14. Food");
System.out.println("    15. Art & Photography");
System.out.println("    16. Self-improvement");
System.out.println("    17. History");
System.out.println("    18. Travel");
System.out.println("    19. Humor");
System.out.println("    20. How-to");
System.out.println("    21. Science & Tech");
}

public void borrow() {
System.out.println("What is the genre of the book you're looking for?");
printGenres();
Scanner kbMeter = new Scanner(System.in);
int genre = kbMeter.nextInt();
System.out.println("Do you want to see titles, or are you looking for a particular author?");
System.out.println("    1. Titles now");
System.out.println("    2. Search by author");
Scanner kbLeader = new Scanner(System.in);
int titleOrSearch = kbLeader.nextInt();
switch(titleOrSearch) {
case 1:
titlesNow();
break;
case 2:
//method for searching by author
}
}
public void titlesNow() {
LibraryDatabase libraryDatabase = new LibraryDatabase("1", "1", 2);
System.out.println(toString());
}

}

这就是输出:

Welcome to Generic Library Service. Please enter your name. **Username**
Here are our books:
Title: nullAuthor: nullGenre: 0

为什么会发生这种情况?为什么属性返回null?我想从图书排列列表中列出所有书籍的标题、作者和流派。因为目前只有一本书,所以应该把那本书(J.K.罗琳的《哈利波特》(打印出来。

您的问题就在这里,在构造函数中:

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

您正在将title, author, genre分配给它们自己,它们都是null。要解决这个问题,只需将一些参数传递给构造函数:

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

一切都应该正常,而不必使用setter方法。

打印错误是因为您没有将book1保存在任何位置。在LibraryDatabase的构造函数中,添加bookDatabase = new ArrayList<>();,并在books()函数的末尾添加bookDatabase.add(book1);

另外,请注意,LibraryDatabasetoString()方法中不执行任何操作。

将您的LibraryDatabase更改为以下

import java.util.*;

public class LibraryDatabase extends Book {
ArrayList<Book> bookDatabase;
public LibraryDatabase() {

}
public void books() {
Book book1 = new Book();

book1.setBookTitle("Harry Potter");
book1.setBookAuthor("J.K. Rowling");
book1.setBookGenre(1);
bookDatabase.add(book1);
}

public ArrayList<Book> getArrayList() {
return bookDatabase;
}

}

将该book1添加到您的数组列表中。同时添加@null_awe代码。你也错过了。

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

最新更新