[英]Why are the attributes of the objects from my ArrayList returning null?
我正在為 Java 中的圖書館(書籍)編寫代碼。 我有一本書 class 具有三個屬性:標題、作者和流派。 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
);
}
}
我有一個圖書館數據庫 class 用於存儲 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;
}
}
最后,我有一個客戶 class 基本上只是將以上所有內容組織成一個用戶友好的界面:
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());
}
}
這是 output:
Welcome to Generic Library Service. Please enter your name. **Username**
Here are our books:
Title: nullAuthor: nullGenre: 0
為什么會這樣? 為什么返回 null 的屬性? 我想列出 ArrayList 書中所有書籍的標題、作者和類型。 因為目前只有一本書,所以應該打印出那本書(JK羅琳的哈利波特)。
你的問題在這里,在構造函數中:
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 的構造函數中,添加LibraryDatabase
bookDatabase = new ArrayList<>();
在books()
function 的末尾,添加bookDatabase.add(book1);
.
另外,請注意您的LibraryDatabase
在您的toString()
方法中沒有做任何事情。
如下更改您的 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
添加到您的 arraylist 中。 還要添加@null_awe 代碼。 你也錯過了。
public Book(String title,String author,int genre) {
this.title = title;
this.author = author;
this.genre = genre;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.