Question: I am all ready to get scolded for poor programming practice but I cannot resolve an Null pointer exeption issue here in this code. import

I am all ready to get scolded for poor programming practice but I cannot resolve an Null pointer exeption issue here in this code.

import java.io.*;

import java.util.Scanner;

public class BooksForList {

public static void main(String[] args) throws Exception

{

final int NUM_ITEMS = 3;

String name;

String price;

BookInfo[] product = new BookInfo[NUM_ITEMS];

File file = new File("BookPrices.txt");

Scanner inputFile = new Scanner(file);

inputFile.useDelimiter(",");

for (int i = 0; i < NUM_ITEMS; i++)

{

while (inputFile.hasNext())

{

name = inputFile.nextLine();

product[i].setName(name);

price = inputFile.nextLine();

product[i].setPrice(price);

}

inputFile.close();

}

for (int i = 0; i < NUM_ITEMS;i++)

{

System.out.println(product[i].getName());

System.out.println(product[i].getPrice());

}

}

}

I am reading from a file which is stored as such and am attempting to seperate the names of books from their prices and store them in an object.

I Did It Your Way, 11.95 The History of Scotland, 14.50 Learn Calculus in One Day, 29.95 Feel the Stress, 18.50

This is why I am using a delimiter with a comma, as I am short on varioations of ways to properly and easily read data from a file.

any help solutions and or pointers in the right direction would be greatly appreciated.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!