Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago