-1

Im having a null pointer exception error in below code.

Exception in thread "main" java.lang.NullPointerException
at BookTest.createInstances(BookTest.java:53)
at BookTest.main(BookTest.java:20)

i could really use some help, ive been stuck on this for quite a long time.

import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class BookTest
{


public static void main (String[] args){

ArrayList list = createInstances();
writeFile(list);
 }


public  static ArrayList<Book> createInstances()
{
ArrayList<Book> bookArray = new ArrayList<Book>();
String inputArray[] = new String [10];
int i = 0;
Scanner input;

   // Read the text file and stores into inputArray where each line is stored as String.
try 
{
    input = new Scanner( new File("book.txt"));
    input.useDelimiter("\n");
    while (input.hasNext()){
        inputArray[i]=input.next();
        i++;
    }





    // dataArray defines the two dimensional array that store all the values in the line.
    String dataArray [] [] = new String [10] [11]; 

    System.out.println(inputArray.length);
    for (int k =0; k<inputArray.length; k++){

        String getLine = inputArray[k];
        String[] eachLine =getLine.split(" ");
        int length = eachLine.length;


          for ( int j=0; j<length;j++){
             dataArray [k][j]= eachLine[j];
         }
    }



    for (int l = 0; l < 10; l++)
        {
            if (dataArray[l][0].equals("Fiction"))
            {
                Publisher p = new Publisher(dataArray[l][3], dataArray[l][4]);
                bookArray.add(new Fiction(dataArray[l][1], dataArray[l][2], dataArray[l][5],
                p, Double.parseDouble(dataArray[l][6]), dataArray[l][7], l));
            }

            else 
            {
                Publisher p = new Publisher(dataArray[l][3], dataArray[l][4]);
                bookArray.add(new NonFiction(dataArray[l][1], dataArray[l][2],dataArray[l][5],
                p, Double.parseDouble(dataArray[l][6]), dataArray[l][7], l));
            }
        }

} 

catch (FileNotFoundException e) {

    e.printStackTrace();
}
return bookArray;
}


public static void writeFile(ArrayList arrayOfBook)

{
Formatter output ; 

try 

{
     output = new Formatter("book.txt");
            for(int i = 0; i<11;i++)
            {
                output.format( "%s %s %s %s %s %s %s %s %s %s %s \n", arrayOfBook.get(i));
            }
    }       
 catch (IOException e) 

 {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } 

    }
  }

this is the file im trying to read

Fiction AbrahamLincolnVampireHunter Grahame-Smith Wiley NY 978-0446563079 13.99 222 true 12 1/1/2015

Fiction Frankenstein Shelley Prescott GA 978-0486282114 7.99 321 true 8 5/12/2008

NonFiction LifeofKennedy Jones Pearson MT 758-29304566 12.90 biography 3 11/11/2011

1 Answers1

1

You have declared your inputArray as an array of length 10 (which might not be the best idea if you cannot guarantee that the file contains exactly 10 lines), which means that the remaining elements (for which there was no line in the file) stay at null, and you use null.split(" "), which results in the exception. Either make sure you have exactly 10 lines, or use a dynamic data structure (such as an ArrayList<String>) instead.

Blauelf
  • 261
  • 1
  • 9