0

I am reading data from a file and passing the data (three Strings, one float) into objects that I created via setters.

I keep on getting a NullPointerException and cannot figure out what to do, even after researching the topic online.

I have attached pictures of both my main function as well as the code for the class that I am trying to use the setters with.

my main function

the class

Bishan
  • 15,211
  • 52
  • 164
  • 258
  • 2
    Paste your code here. Do not post screenshots of code. – xrisk Sep 26 '18 at 03:06
  • `popCustArr()` does nothing. You create an `Array` that will pass out of scope when you leave the method, and you never return anything – GBlodgett Sep 26 '18 at 03:07
  • Because you have not initazed the array you have created in popCUstArr method. In that method after line "int =0;" add line " regular[current] = new Customer (); ". That will work – Nawnit Sen Sep 26 '18 at 03:25

2 Answers2

0

You are not create any new customers, only an array of Customers[],

you might need

regular[current] = new Customer() (you will need to create an empty public constructor for you Customer Class)

Or

regular[current] = new Customer(id, f, l, money)

Brandon
  • 1,158
  • 3
  • 12
  • 22
0

Usually, a nullPointerException happens when you pass an argument into a method and try to use that argument, but you don't check for whether or not the thing passed in to the method was actually a null value.

For instance, if I have a String as an argument in my method, and I do String operations on that item in my method, but one day someone gives me a null String instead. I can't do String operations on a null object, so the program will throw the nullPointerException.

My guess is that when you're putting the lines of that text file through your scanner, some of them are actually empty, thus causing the exception. Or, it could be happening in a function where an argument value is being entered as a null value.

If this is for a class, I know that my professors like to make sure we're on our toes for this sort of thing. I've had to deal with the same error many times!

SethBarton
  • 133
  • 3
  • 10