开发者

Can't Locate Source of NullPointerException in this Java code

开发者 https://www.devze.com 2023-02-18 15:40 出处:网络
I\'m getting Null Pointer Exceptions when I try to run the following block of code which is supposed to print out a list of books stored in a txt file. (The \"roundabout\" implementation of first havi

I'm getting Null Pointer Exceptions when I try to run the following block of code which is supposed to print out a list of books stored in a txt file. (The "roundabout" implementation of first having a book object before parsing is on purpose)

The occurrences of Null Pointer exceptions are indicated in the code (essentially anywhere BookPrint is referenced)

Any thoughts?

     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开发者_StackOverflow中文版 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;
    }
}


You're assigning to b_books[i] in parseAll() without creating the array.

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

精彩评论

暂无评论...
验证码 换一张
取 消