It seems to me that I've done everything right, but the NullPointerException is thrown. I assume it is not about file, cause there would be FileNotFoundExcepion. The method is as follows:
import java.util.Scanner;
import java.io.*;
public class InputFileData
{
public static Product [] readProductDataFile(File inputFile)
throws IOException
{
try
{
Scanner fileScan = new Scanner(inputFile);
int range = Integer.parseInt(fileScan.nextLine());
String codeData = fileScan.nextLine();
String priceData = fileScan.nextLine();
Scanner codeDataScan = new Scanner(codeData);
codeDataScan.useDelimiter("#");
Scanner priceDataScan = new Scanner(priceData);
priceDataScan.useDelimiter("#");
Product [] productRange = new Product[range];
for(int i = 0; i < range; i++)
{
productRange[i].setProductCode(codeDataScan.next());
productRange[i].setPricePerUnit(Integer.
parseInt(priceDataScan.nextLine()));
}
return productRange;
}
catch(NumberFormatException e)
{
System.out.println(e);
return null;
}
}
}
In the main method I use:
try
{
File productData = new File("productData.txt");
Product [] consideredRange = InputFileData.readProductDataFile(productData);
for(int i = 0; i < consideredRange.length; i++)
System.out.println(i + "./n" + consideredRange[i]);
}
catch(Exception e)
{
System.out.println(e);
}
And the data in a file looks like this:
10
PA/1234#PV/5732#Au/9271#DT/9489#HY/7195#ZR/7413#bT/4674#LR/4992#Xk/8536#kD/9767#
153#25#172#95#235#159#725#629#112#559#
Line 1 - number of items
Line 2 - Product code
Line 3 - price
What is wrong then?
Thank you!
Edit:
at Assignment2.InputFileData.readProductDataFile(InputFileData.java:35) at Assignment2.InputFileData.readProductDataFile(InputFileData.java:35)
at Assignment2.MainTest.main(MainTest.java:211)
The line 35 is productRange[i].setProductCode(codeDataScan.next());
Solution:
productRange[i] = new Product(codeDataScan.next(), Integer.parseInt(priceDataScan.next()));