0

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()));

ArthurV
  • 113
  • 2
  • 8
  • 1
    Add the stack trace and highlight the line where the error occurs – MadProgrammer Mar 27 '15 at 01:41
  • 3
    You need to learn the general concepts of how to debug a NPE (NullPointerException). **You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully**, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Mar 27 '15 at 01:42
  • http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – user2864740 Mar 27 '15 at 01:45
  • @Hovercraft Full Of Eels I've never used debugger unfortunately.. It is definitely something important to get used with. – ArthurV Mar 27 '15 at 01:51
  • Arthur, what I suggested above doesn't require a debugger at all -- just a set of eyeballs. – Hovercraft Full Of Eels Mar 27 '15 at 01:52
  • Hovercraft Full Of Eels, what is exception's stacktrace? The message which is thrown by default? – ArthurV Mar 27 '15 at 01:54
  • The message that tells you that a NPE was thrown -- that's the stacktrace. It tells you the line numbers of the exception and all. You should **always** post this with your question (you didn't). – Hovercraft Full Of Eels Mar 27 '15 at 01:56

1 Answers1

3

You never initialise the contents of the productRange array

Product [] productRange = new Product[range];

Creates an array capable of Product whose elements are all null

You then try to "modify" each element...

for(int i = 0; i < range; i++)
{
    productRange[i].setProductCode(codeDataScan.next());
    productRange[i].setPricePerUnit(Integer.
                                    parseInt(priceDataScan.nextLine()));
}

Which is the probable cause of you NPE

You need to intiailsie the element of the array BEFORE you initialise it

for(int i = 0; i < range; i++)
{
    productRange[i] = new Product();
    productRange[i].setProductCode(codeDataScan.next());
    productRange[i].setPricePerUnit(Integer.
                                    parseInt(priceDataScan.nextLine()));
}

When diagnosing these types of problems, you can use System.out.println (or an appropriate logger) to help you identify problem areas and use the debugger to inspect the state (and flow) of your program.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366