I can't figure out why I get the NullPointerException on Line 223. Would appreciate any help whatsoever.
Sorry about the spacing below. Some of the code appears messy. Toggle Plain Text and it should look better.
/* Section 1: Import Statements. Imports the Scanner class and the IO Package. */
import java.util.Scanner;
import java.io.*;
/* Section 2: Class ReadFiles, contains the methods to open, read and close all the files to be read by the program. */
class ReadFiles {
String [] codes = new String[99];
String [] description = new String[99];
int [] pricesAndTax = new int[99];
int [] weight = new int[99];
int j = 0;
private Scanner readCodes;
private Scanner readDesc;
private Scanner readPAndT;
private Scanner readWeight;
/* Section : Methods to open, read and close the 'codes.txt' file. */
public void openCodesFile() {
try {
readCodes = new Scanner(new File("C:/Users/Carlo/Desktop/Files/codes.txt"));
} catch (Exception e) {
System.out.println("Could not locate the data file!");
}
}
public void readCodesFile() {
while(readCodes.hasNext()) {
codes[j] = readCodes.nextLine();
//System.out.println(codes[i]);
j++;
}
}
public void closeCodesFile() {
readCodes.close();
}
/* Section : Methods to open, read and close the 'description.txt' file. */
public void openDescFile() {
try {
readDesc = new Scanner(new File("C:/Users/Carlo/Desktop/Files/description.txt"));
} catch (Exception e) {
System.out.println("Could not locate the data file!");
}
}
public void readDescFile() {
while(readDesc.hasNext()) {
description[j] = readDesc.nextLine();
//System.out.println(description[i]);
j++;
}
}
public void closeDescFile() {
readDesc.close();
}
/* Section : Methods to open, read and close the 'priceAndTax.txt' file. */
public void openPAndTFile() {
try {
readPAndT = new Scanner(new File("C:/Users/Carlo/Desktop/Files/priceAndTax.txt"));
} catch (Exception e) {
System.out.println("Could not locate the data file!");
}
}
public void readPAndTFile() {
while(readPAndT.hasNext()) {
pricesAndTax[j] = readPAndT.nextInt();
//System.out.println(pricesAndTax[i]);
j++;
}
}
public void closePAndTFile() {
readPAndT.close();
}
/* Section : Methods to open, read and close the 'weight.txt' file. */
public void openWeightFile() {
try {
readWeight = new Scanner(new File("C:/Users/Carlo/Desktop/Files/weight.txt"));
} catch (Exception e) {
System.out.println("Could not locate the data file!");
}
}
public void readWeightFile() {
while(readWeight.hasNext()) {
weight[j] = readWeight.nextInt();
//System.out.println(weight[i]);
j++;
}
}
public void closeWeightFile() {
readWeight.close();
}
}
/* Section : Class NewHardware. Contains the main method. */
public class NewHardware {
public static void main(String[] args) {
/* Instances of the ReadFiles class. */
ReadFiles codesRead = new ReadFiles();
ReadFiles descRead = new ReadFiles();
ReadFiles pAndTRead = new ReadFiles();
ReadFiles weightRead = new ReadFiles();
/* Section : Scanner object for user input. */
Scanner in = new Scanner(System.in);
String userName;
String address1;
String address2;
String city;
String country;
String postalCode;
String buyerNew;
int i = 1000;
String output = "";
String [] userCode = new String[i];
char dolSymb = '$';
float weightTotal = 0f;
float weightCalc = 0f;
int [] userQuantity = new int[i];
int [] userPrice = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int [] orderLimit = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int userPreWithTax = 0;
int userTotal = 0;
int userPreTotal = 0;
int userShipping = 0;
int inputCount = 0;
/* Section : Methods from the ReadFiles class being called here to be executed. */
/* Section : All the 'open...' methods. */
codesRead.openCodesFile();
descRead.openDescFile();
pAndTRead.openPAndTFile();
weightRead.openWeightFile();
/* Section : All the 'read...' methods. */
codesRead.readCodesFile();
descRead.readDescFile();
pAndTRead.readPAndTFile();
weightRead.readWeightFile();
/* Section : Printing the welcome statement and the catalog. */
System.out.printf("\n\n%55s", "**WELCOME TO THE HARDWARE STORE**\n");
System.out.printf("%56s", "=================================\n\n");
System.out.printf("%-10s%-40s%-15s%-10s\n", "CODE", "DESCRIPTION", "WEIGHT(grams)", "PRICE\n");
/* Section : A for-loop to print out the catalog. Makes use of the welCount variable to go through all the product information. */
for (int welCount = 0; welCount < 9; welCount++) {
System.out.printf("%-10s%-40s%-15d%-1s%d.%02d\n", codesRead.codes[welCount], descRead.description[welCount], weightRead.weight[welCount], dolSymb, pAndTRead.pricesAndTax[welCount]/100, pAndTRead.pricesAndTax[welCount]%100);
}
/* Section : Loops to take customer input. */
i = 0;
do {
System.out.println("\n\nPLEASE ENTER YOUR ORDER:");
System.out.print("\nNAME: ");
userName = in.nextLine();
System.out.print("\nADDRESS Line 1: ");
address1 = in.nextLine();
System.out.print("\nADDRESS Line 2: ");
address2 = in.nextLine();
System.out.print("\nCITY: ");
city = in.nextLine();
System.out.print("\nCOUNTRY: ");
country = in.nextLine();
System.out.print("\nPOSTAL CODE: ");
postalCode = in.nextLine();
System.out.println();
while (true) {
System.out.print("CODE (X to QUIT):");
userCode[i] = in.nextLine();
if (userCode[i].equalsIgnoreCase("x")) {
break;
}
System.out.print("QUANTITY: ");
userQuantity[i] = in.nextInt();
in.nextLine();
if (userCode[i].equalsIgnoreCase(codesRead.codes[0]) && userQuantity[i] >= 1) {
orderLimit[0] += userQuantity[i];
if (orderLimit[0] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[0] = 0;
} else {
userPrice[0] += userQuantity[i]*pAndTRead.pricesAndTax[0];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[0], "\n", descRead.description[0], " @ ", dolSymb, pAndTRead.pricesAndTax[0]/100, pAndTRead.pricesAndTax[0]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[0]*userQuantity[i])/100, (pAndTRead.pricesAndTax[0]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[0];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[1]) && userQuantity[i] >= 1) {
orderLimit[1] += userQuantity[i];
if (orderLimit[1] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[1] = 0;
} else {
userPrice[1] += userQuantity[i]*pAndTRead.pricesAndTax[1];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[1], "\n", descRead.description[1], " @ ", dolSymb, pAndTRead.pricesAndTax[1]/100, pAndTRead.pricesAndTax[1]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[1]*userQuantity[i])/100, (pAndTRead.pricesAndTax[1]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[1];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[2]) && userQuantity[i] >= 1) {
orderLimit[2] += userQuantity[i];
if (orderLimit[2] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[2] = 0;
} else {
userPrice[2] += userQuantity[i]*pAndTRead.pricesAndTax[2];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[2], "\n", descRead.description[2], " @ ", dolSymb, pAndTRead.pricesAndTax[2]/100, pAndTRead.pricesAndTax[2]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[2]*userQuantity[i])/100, (pAndTRead.pricesAndTax[2]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[2];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[3]) && userQuantity[i] >= 1) {
orderLimit[3] += userQuantity[i];
if (orderLimit[3] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[3] = 0;
} else {
userPrice[3] += userQuantity[i]*pAndTRead.pricesAndTax[3];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[3], "\n", descRead.description[3], " @ ", dolSymb, pAndTRead.pricesAndTax[3]/100, pAndTRead.pricesAndTax[3]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[3]*userQuantity[i])/100, (pAndTRead.pricesAndTax[3]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[3];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[4]) && userQuantity[i] >= 1) {
orderLimit[4] += userQuantity[i];
if (orderLimit[4] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[4] = 0;
} else {
userPrice[4] += userQuantity[i]*pAndTRead.pricesAndTax[4];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[4], "\n", descRead.description[4], " @ ", dolSymb, pAndTRead.pricesAndTax[4]/100, pAndTRead.pricesAndTax[4]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[4]*userQuantity[i])/100, (pAndTRead.pricesAndTax[4]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[4];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[5]) && userQuantity[i] >= 1) {
orderLimit[5] += userQuantity[i];
if (orderLimit[5] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[5] = 0;
} else {
userPrice[5] += userQuantity[i]*pAndTRead.pricesAndTax[5];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[5], "\n", descRead.description[5], " @ ", dolSymb, pAndTRead.pricesAndTax[5]/100, pAndTRead.pricesAndTax[5]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[5]*userQuantity[i])/100, (pAndTRead.pricesAndTax[5]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[5];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[6]) && userQuantity[i] >= 1) {
orderLimit[6] += userQuantity[i];
if (orderLimit[6] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[6] = 0;
} else {
userPrice[6] += userQuantity[i]*pAndTRead.pricesAndTax[6];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[6], "\n", descRead.description[6], " @ ", dolSymb, pAndTRead.pricesAndTax[6]/100, pAndTRead.pricesAndTax[6]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[6]*userQuantity[i])/100, (pAndTRead.pricesAndTax[6]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[6];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[7]) && userQuantity[i] >= 1) {
orderLimit[7] += userQuantity[i];
if (orderLimit[7] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[7] = 0;
} else {
userPrice[7] += userQuantity[i]*pAndTRead.pricesAndTax[7];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[7], "\n", descRead.description[7], " @ ", dolSymb, pAndTRead.pricesAndTax[7]/100, pAndTRead.pricesAndTax[7]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[7]*userQuantity[i])/100, (pAndTRead.pricesAndTax[7]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[7];
i++;
}
}
if (userCode[i].equalsIgnoreCase(codesRead.codes[8]) && userQuantity[i] >= 1) {
orderLimit[8] += userQuantity[i];
if (orderLimit[8] > 100) {
System.out.println("Sorry, you can only order 100 of a particular item.");
orderLimit[8] = 0;
} else {
userPrice[8] += userQuantity[i]*pAndTRead.pricesAndTax[8];
output += String.format("%d%s%s%s%s%s%s%d.%02d%s%s%d.%02d\n", userQuantity[i], " x ", codesRead.codes[8], "\n", descRead.description[8], " @ ", dolSymb, pAndTRead.pricesAndTax[8]/100, pAndTRead.pricesAndTax[8]%100, ": ", dolSymb, (pAndTRead.pricesAndTax[8]*userQuantity[i])/100, (pAndTRead.pricesAndTax[8]*userQuantity[i])%100);
weightTotal += userQuantity[i]*weightRead.weight[8];
i++;
}
}
weightCalc = weightTotal/250;
userShipping = (int)Math.ceil(weightCalc)*100;
userPreTotal = (userPrice[0] + userPrice[1] + userPrice[2] + userPrice[3] + userPrice[4] + userPrice[5] + userPrice[6] + userPrice[7] + userPrice[8] + userShipping);
userPreWithTax = (userPreTotal * pAndTRead.pricesAndTax[9])/100;
userTotal = userPreTotal + userPreWithTax;
}
System.out.printf("\n%s\n", "INVOICE FOR ORDER:");
System.out.printf("%s\n\n", "------------------");
System.out.printf("%s\n", userName);
System.out.printf("%s\n", address1);
System.out.printf("%s\n", address2);
System.out.printf("%s\n", city);
System.out.printf("%s\n", country);
System.out.printf("%s\n", postalCode);
System.out.printf("\n\n\n%s\n", output);
System.out.printf("%s%.2f%s\n", "TOTAL WEIGHT: ", weightTotal, " grams.");
System.out.printf("\n%s%.2f%s%s%d.%02d\n", "SHIPPING COST for ", weightTotal, " grams: ", dolSymb, userShipping/100, userShipping%100);
System.out.printf("\n%s%s%d.%02d\n", "TOTAL WITH SHIPPING: ", dolSymb, userPreTotal/100, userPreTotal%100);
System.out.printf("\n%s%s%d.%02d\n", "VAT @ 20%: ", dolSymb, userPreWithTax/100, userPreWithTax%100);
System.out.printf("\n%s%s%d.%02d\n\n\n", "TOTAL: ", dolSymb, userTotal/100, userTotal%100);
System.out.printf("%55s\n", "THANK YOU FOR SHOPPING AT HARDWARE STORE!");
System.out.printf("%55s\n", "=========================================");
i = 0;
userTotal *= i;
userPrice[0] *= i; userPrice[1] *= i; userPrice[2] *= i; userPrice[3] *= i; userPrice[4] *= i; userPrice[5] *= i; userPrice[6] *= i; userPrice[7] *= i; userPrice[8] *= i;
output = "";
orderLimit[0] *= i; orderLimit[1] *= i; orderLimit[2] *= i; orderLimit[3] *= i; orderLimit[4] *= i; orderLimit[5] *= i; orderLimit[6] *= i; orderLimit[7] *= i; orderLimit[8] *= i;
weightTotal *= i;
System.out.printf("\n\n%s", "ANOTHER CUSTOMER? (Y or N): ");
buyerNew = in.nextLine();
} while (!buyerNew.equalsIgnoreCase("n"));
/* Section : All the 'close...' methods. */
codesRead.closeCodesFile();
descRead.closeDescFile();
pAndTRead.closePAndTFile();
weightRead.closeWeightFile();
}
}