0

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 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;
    }
}
2
  • Please post the exception that your app is throwing. Commented Mar 19, 2011 at 11:48
  • What does that code returns new FileReader("C:\\Users\\Owner\\workspace\\Book\\src\\hotBooks.txt") ? Commented Mar 19, 2011 at 11:49

1 Answer 1

8

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

private Book[] b_books = new Book[50];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Erik!! Talk about a simple but annoying bug!...I wonder if you could take a look at my parseOne and parseAll functions as well as my this.parseAll() call in the constructor..I am now getting a No line found at java.util.Scanner.nextLine(Unknown Source) at the parseAll() calls/method definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.