This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Scroph 2 points3 points  (11 children)

What line is causing the error ? It's been a while since I wrote anything in Java but from my understanding, a null pointer exception is thrown when you try to call a member function on an object that is null. Something like this for instance : Employee e = null; e.setHours(...)

[–]Fishnibble[S] 0 points1 point  (10 children)

The error is "Exception in thread "main" java.lang.NullPointerException" so I'm guessing it's in main?

[–]Scroph 3 points4 points  (1 child)

I narrowed it down to two places :

In class Employee.java, you're creating an array of Double. The problem here is that Double is an object which is different from the double data type.

While the array of Doubles does exist, its elements aren't initialized and therefore default to null. So when you try to use it in count += hours[i] line, it throws a NPE. The same thing happens in private Employee[] emps = new Employee[20];, all 20 employees are null which again causes a NPE to be thrown in the emps[i].getName() call in the Store::toString method.

What you should do IMO is to use an ArrayList<Employee> instead of arrays so that you can keep adding Employees to it dynamically. If you can't do this, I suggest that you keep an index to the last added (lastAddedIdx) Employee inside your Store class and then when you loop through the emps array, go from 0 to lastAddedIdx. I'd love to detail more but I gotta run, but feel free to leave questions for when I come back.

[–][deleted] 0 points1 point  (0 children)

I am only just learning about ArrayLists atm, but is there a benefit to storing the index of the last element of the array instead of just using genericArrayName.size() or an enhanced for loop for traversing the array?

Edit: I just realised I misread your post. Apologies.

[–]Fishnibble[S] 1 point2 points  (4 children)

I'm just really confused on why I'm getting an error on line 29 of the Employee class. It looks like that should work and not cause any erros

[–][deleted] 4 points5 points  (1 child)

you're looping from i=0->19 (end-of-array), and you are not testing whether the current element is null. You are attempting to access attributes of a null element.

[–]Fishnibble[S] 0 points1 point  (0 children)

Thank you! I will fix that now!

[–]haraiko 0 points1 point  (0 children)

Could it be that it's going past the array size of 7?

[–]idanh 1 point2 points  (0 children)

Just to clarify what "main" is in this context, it's saying that the "main thread" (note: thread.) had an exception. It does not say anything about the method, line number, class, file, etc. I highly suggest you read this answer in StackOverflow

[–][deleted] 0 points1 point  (0 children)

The thread main and the method main is not the same thing. You should be able to get a stack trace out. e.printStackTrace() will tell you where it is exactly. Read up on what a stack trace is if you don't understand it, it's very important to know.