0

I've created a class with a few objects.

class SalesPerson {
    String number;
    String name;
    double salesAmount;
}


So now I need to copy some data from a text file to an array.

"sales.txt"

S0001
Alice
2000
S0002
Bob
3400
S0003
Cindy
1200
S0004
Dave
2600


Below is the shortened version of my code, assuming that the getName(s), setName(s) and constructors are created and the text file can be successfully read:

class ArrayImport {
    public static void main(String args[]) throws FileNotFoundException {
        String fileName = "sales.txt";
        SalesPerson sp = new SalesPerson[4]; //Manually counted

        //Read the file
        Scanner sc = new Scanner(new FileReader(fileName));

        //Copy data to array
        int i = 0;
        while (sc.hasNext()) {
            sp[i].name = sc.nextLine(); //Error starts here
            sp[i].number = sc.nextLine();
            sp[i].salesAmount = Double.parseDouble(sc.nextLine());
            i++;
        }
    }
}

I get the error message "Exception in thread "main" java.lang.NullPointerException..." pointing to the line which I commented "Error starts here".


So I am guessing that this is not the way to assign a value to an object array, and if my guess is correct, what are the correct syntax?

GlyphCat
  • 144
  • 1
  • 3
  • 15
  • 1
    You haven't created any instances of `SalesPerson`. All you're missing is `staff[i] = new SalesPerson();` before you start assigning to individual fields. (I'd personally write a `SalesPerson` constructor accepting three parameters, but that's a different matter. I'd also use `BigDecimal` for currency values rather than `double`.) – Jon Skeet Oct 13 '17 at 08:12

2 Answers2

0

The instance of object is null so you have to create the instance first. so create instance like this 'staff[i] = new SalesPerson();' I added instance creation to your code.

  class ArrayImport {
    public static void main(String args[]) throws FileNotFoundException {
        String fileName = "sales.txt";
        SalesPerson sp = new SalesPerson[4]; //Manually counted

        //Read the file
        Scanner sc = new Scanner(new FileReader(fileName));

        //Copy data to array
        int i = 0;
        while (sc.hasNext()) {
            staff[i] = new SalesPerson();
            staff[i].name = sc.nextLine(); //Error starts here
            staff[i].number = sc.nextLine();
            staff[i].salesAmount = Double.parseDouble(sc.nextLine());
            i++;
        }
    }
}
janith1024
  • 1,042
  • 3
  • 12
  • 25
  • I just realized that I forgot to change "staff" to "sp". I wanted it to be shorter so it's easier to read in the thread, I thought. Anyway, I tried your way to create an instance of the object but got a "java.lang.ArrayIndexOutOfBoundsException" error instead, it points to the line where the instance of object is created. – GlyphCat Oct 14 '17 at 05:34
  • Indexed outof boub except is your array length is not enough so change the array size to larger amount. Current is 4 – janith1024 Oct 15 '17 at 07:15
  • Well, it turns out to be that I've forgotten my actual sales.txt file has more than just those records. I kinda want to make it short here but ended up confusing myself. Your method actually worked. Thank you so much. – GlyphCat Oct 19 '17 at 16:10
0

Working example:

public class ArrayImport {

        public static void main(String[] args) throws FileNotFoundException {
            List<SalesPerson> persons = new ArrayList<>();
            SalesPerson salesPerson = null; //Manually counted

            //Read the file
            Scanner scanner = new Scanner(new FileReader("sales.txt"));

            int count=0;
            while(scanner.hasNext()) {
                if( count==0 || count%3 == 0) {
                    salesPerson = new SalesPerson();
                    salesPerson.setNumber(scanner.nextLine());
                    salesPerson.setName(scanner.nextLine());
                    salesPerson.setSalesAmount(Double.parseDouble(scanner.nextLine()));
                    persons.add(salesPerson);
                    count+=3;
                }
            }
            persons.forEach(person->System.out.println(person.toString()));
        }
    }
Yogi
  • 1,805
  • 13
  • 24
  • This code is runnable and but the printed lines are quite weird. For each information of the 'persons' printed, the output are like this "SalesPerson@1b28cdfa". I tried System.out.println(salesPerson.name) and actually got something, it is confirmed that the data are being copied, just that they're not displaying when printed using forEach. – GlyphCat Oct 14 '17 at 05:45