Can';在此Java代码中找不到NullPointerException的源
当我尝试运行下面的代码块时,会出现空指针异常,该代码块应该打印出存储在txt文件中的书籍列表。(解析之前首先拥有book对象的“迂回”实现是有意的) 空指针异常的出现在代码中指示(基本上是指引用BookPrint的任何位置) 有什么想法吗Can';在此Java代码中找不到NullPointerException的源,java,nullpointerexception,Java,Nullpointerexception,当我尝试运行下面的代码块时,会出现空指针异常,该代码块应该打印出存储在txt文件中的书籍列表。(解析之前首先拥有book对象的“迂回”实现是有意的) 空指针异常的出现在代码中指示(基本上是指引用BookPrint的任何位置) 有什么想法吗 import java.io.*; import java.util.Scanner; /** there is a basic Book.java file that stores a title and a category that are
import java.io.*;
import java.util.Scanner;
/** there is a basic Book.java file that stores a title and a category that are set by a *constructor and has get methods for each and a "toString" Method for a Book object that *prints out the book's title and category
**/
/**
** BookPrint prints out all the books in the txt file as stored in an array of book objects
**derived by parsing the text file
**/
public class BookPrint
{
private Book[] b_books;
private Scanner defaultFR;
private Book bookPerLine;
/**
* main takes a txt file with book titles on separate lines in title category format like "The Lion King, Children"/n "Yellow Sun, Fiction" /n etc
*/
public static void main(String[] argv)
throws FileNotFoundException
{
BookPrint bk;
/**
The following declaration gives a NullPointer exception
**/
bk = new BookPrint(new FileReader("C:\\Users\\Owner\\workspace\\Book\\src\\hotBooks.txt"));
Book[] books;
books = bk.getBooks();
//
//take each book in and print out
for(int i =0; i<50; i++){
books[i].toString();
}
}
/** constructor populates BookPrint
*
*/
public BookPrint(FileReader fr)
throws ParseError
{
defaultFR = new Scanner(fr);
//Null pointer exception when the parseAll method is called
this.parseAll(defaultFR);
}
/** Return array of books
*/
public Book[] getBooks()
{
return b_books;
}
/** Parse all.
*Null Pointer Exception here as well
*
*/
private void parseAll(Scanner scn)
throws ParseError
{
//open scanner object that reads file
//for all books in array, if there is next line parseOne, and store in book array
try{
while(scn.hasNext()){
for(int i=0; i< 50; i++){
b_books[i]= parseOne(scn.nextLine());
}
}
}
finally{
scn.close();
}
}
/** Parse one
*
*
*/
private Book parseOne(String line)
throws ParseError
{
Scanner scn = new Scanner(line);
//parse line by "," , store each value as book.title and book.category and return book
scn.useDelimiter(",");
if (scn.hasNext() ){
String title = scn.next();
String category = scn.next();
bookPerLine = new Book(title, category);
}
else {
System.out.println("Empty or invalid line. Unable to process.");
}
scn.close();
return bookPerLine;
}
}
import java.io.*;
导入java.util.Scanner;
/**有一个基本的Book.java文件,它存储由*构造函数设置的标题和类别,并且每个文件都有get方法,还有一个用于书籍对象的“toString”方法*打印出书籍的标题和类别
**/
/**
**BookPrint打印txt文件中存储在书籍对象数组中的所有书籍
**通过解析文本文件派生
**/
公共类印刷品
{
私人书籍[]bu书籍;
私人扫描仪;
私书书店;
/**
*main获取一个txt文件,在标题类别格式的单独行中包含书籍标题,如“狮子王,儿童”/n“黄太阳,小说”/n等
*/
公共静态void main(字符串[]argv)
抛出FileNotFoundException
{
书籍印刷;
/**
下面的声明给出了一个null指针异常
**/
bk=newbookprint(新文件阅读器(“C:\\Users\\Owner\\workspace\\Book\\src\\hotBooks.txt”);
书籍[]书籍;
books=bk.getBooks();
//
//把每本书都拿进去打印出来
对于(int i=0;i您正在parseAll()
中分配给b_图书[i]
,而不创建数组
private Book[] b_books = new Book[50];
请发布你的应用程序抛出的异常。该代码返回什么新文件阅读器(“C:\\Users\\Owner\\workspace\\Book\\src\\hotBooks.txt”)
?谢谢Erik!!谈谈一个简单但恼人的错误!…我想知道你是否可以看看我的parseOne和parseAll函数以及我的this.parseAll()调用构造函数..我现在在parseAll()调用/方法定义的java.util.Scanner.nextLine(未知源代码)中找不到行。