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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started