2011-03-19 64 views
0

當我嘗試運行以下應該打印出存儲在txt文件中的書籍列表的代碼塊時,出現空指針異常。 (解析前的「迂迴」實現首先有一本書對象是有意的)在此Java代碼中找不到NullPointerException的源

空指針異常的出現在代碼(任意地點BookPrint引用)

任何想法都表示?

 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; 
    } 
} 
+0

發佈堆棧跟蹤。 – Nix 2011-03-19 11:47:59

+0

請發佈您的應用程序正在拋出的異常。 – 2011-03-19 11:48:43

+0

該代碼返回什麼'新的FileReader(「C:\\ Users \\ Owner \\ workspace \\ Book \\ src \\ hotBooks.txt」)'? – MByD 2011-03-19 11:49:13

回答

8

你在parseAll()分配給b_books[i]而無需創建陣列。

private Book[] b_books = new Book[50]; 
+0

謝謝Erik!談論一個簡單但惱人的錯誤!...我不知道你是否可以看看我的parseOne和parseAll函數以及我的this.parseAll()調用在構造函數中..我現在得到一個No行在Java .util.Scanner.nextLine(未知源)在parseAll()調用/方法定義。 – Coderamoter 2011-03-19 13:19:58

相關問題