I changed lines 19-35 to:
String productID = "";
String productName = reader.readLine();
String productPrice = reader.readLine();
String productQuantity = reader.readLine();
String product = "";
productID = getID(fileName);
productName = fullName(productName);
productPrice = fullPrice(productPrice);
productQuantity = fullQuantity(productQuantity);
product = productID + productName + productPrice + productQuantity;
writer.write("\n" + product);
in order to test the code and used the following input:
C:\Users\User\Products.txt
swimming trunks
150.00
5
and the program threw a NullPointerExceptionpackage com.codegym.task.task18.task1827;
/*
Prices
*/
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
FileOutputStream fileOutputStream = new FileOutputStream(fileName, true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
String productID = "";
String productName = "";
String productPrice = "";
String productQuantity = "";
String product = "";
if (args[0].equals("-c")){
productID = getID(fileName);
productName = fullName(args[1]);
productPrice = fullPrice(args[2]);
productQuantity = fullQuantity(args[3]);
product = productID + productName + productPrice + productQuantity;
writer.write("\n" + product);
}
reader.close();
fileOutputStream.close();
writer.close();
}
//get ID
public static String getID(String fileName) throws Exception {
ArrayList<String> products = allProducts(fileName);
ArrayList<Integer> productIDs = productIDs(products);
int id = productIDs.get(productIDs.size()-1) +1;
return fullID(id);
}
//return sorted ArrayList of product IDs
public static ArrayList<Integer> productIDs(ArrayList<String> list) {
ArrayList<Integer> productIDs = new ArrayList<>();
for (String string : list){
productIDs.add(getProductID(string));
}
Collections.sort(productIDs);
return productIDs;
}
//get the ID of a product
public static int getProductID(String product){
String productIDString = product.substring(0, 7);
int productID = Integer.parseInt(productIDString.replaceAll(" ", ""));
return productID;
}
// return ArrayList of all products in the file
public static ArrayList<String> allProducts(String fileName) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
ArrayList<String> products = new ArrayList<>();
String s;
while (!(s = reader.readLine()).equals(null)){
products.add(s);
}
reader.close();
return products;
}
//make sure ID is 8 characters long
public static String fullID(int id){
StringBuilder idAsString = new StringBuilder(String.valueOf(id));
while (idAsString.length()!=8){
idAsString.append(" ");
}
return idAsString.toString();
}
//make sure product name is 30 characters long
public static String fullName(String name){
StringBuilder fullName = new StringBuilder(name);
while (fullName.length()!=30){
fullName.append(" ");
}
return fullName.toString();
}
//make sure price is 8 characters long
public static String fullPrice(String s){
StringBuilder price = new StringBuilder(s);
while (price.length()!=8){
price.append(" ");
}
return price.toString();
}
//make sure quantity is 4 characters long
public static String fullQuantity(String s){
StringBuilder quantity = new StringBuilder(s);
while (quantity.length()!=4){
quantity.append(" ");
}
return quantity.toString();
}
}