1

gui.CataloguePanel.(CataloguePanel.java) 处的线程“主”java.lang.NullPointerException 中的异常

我正在尝试初始化一个BookQuery对象CataloguePanel并调用getBookList()

CataloguePanel.java

package gui;

import java.util.ArrayList;

import backend.Book;
import backend.BookQuery;

// Catalog panel. Shows the library titles' information and allows users to place their orders.
public class CataloguePanel extends JPanel implements GBPanel {

    BookQuery bQuery;
    ArrayList<Book> bookList = bQuery.getBookList(); // Here's where the error occurred

}

编辑: http ://www.reactionface.info/sites/default/files/imagecache/Node_Page/images/1314029819767.png

4

4 回答 4

5
BookQuery bQuery;
ArrayList<Book> bookList = bQuery.getBookList(); 

bQuery没有初始化,所以它类似于null.getBookList()必须产生NullPointerException. 要么你初始化bQuery. 喜欢 -

BookQuery bQuery= new BookQuery();

或者制作getBookList()方法static并调用方法,如 -

ArrayList<Book> bookList = BookQuery.getBookList(); 
于 2013-05-14T06:59:13.013 回答
3

您需要实例化 bookQuery

 BookQuery bQuery= new BookQuery();
于 2013-05-14T06:58:45.830 回答
0

你需要做 BookQuery bQuery = new BookQuery();

于 2013-05-14T06:59:27.313 回答
0

bQuery未初始化,因此是null. 这会产生 NPE。你必须初始化它: BookQuery bQuery= new BookQuery();

(正如@Daniel Robertus 提到的那样)+1

于 2013-05-14T06:59:59.327 回答