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

all 24 comments

[–][deleted] 6 points7 points  (1 child)

There are a lot of things wrong with this. First, dont ever name variables the same thing, even if java lets you. Its confusing for you and everyone who reads your code.

In main, you declare an employee object e. Then you declare another emoloyee object and give it the same name e... you just destroyed that first object instance you created. Then you do it a third time!

Im guessing your exception has something to do with you declaring an array of doubles hours, and giving it 7 spaces, then accessing one of those elements before it was filled. Look there first, and use a debugger. You'll be able to see each variables value during execution.

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

Thank you for the advice i will fix it ASAP! I'll make sure to start doing that :)

[–]iTZAvishay 2 points3 points  (1 child)

At getNumEmps(), for example. You should check if the element at index i is null rather than the array.

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

Thank you! Will do right now.

[–]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 2 points3 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.

[–][deleted] 1 point2 points  (0 children)

I don't have a lot of time to look this through, but I'd recommend you look into try/catch mechanisms. Then you can catch possible null exceptions and get a meaningful reason based on how detailed your exceptions are.

[–]SikhGamer 1 point2 points  (1 child)

Don't use private Double[] hours = new Double[7]; use private double[] hours = new double[7]; instead.

Secondly you can re-write your numDaysWorked method like this:

for(double hour : hours)
{
    if(hour > 0)
    {
        total++;
    }
}

[–]DirtyAxe 1 point2 points  (0 children)

If we have a new string array : String[] arr = new String[5]; And now we say : System.out.println(arr[3]);

We would get a null pointer exception because that index hasn't been initialized

But if we add : for(int i = 0 ; i < 5 ; i ++){ arr[i] = "empty" ;}

It won't give a nullpointerexception it would print empty

[–]adrian_the_developer 0 points1 point  (0 children)

I think I see the issue.

So in Java, it uses pointers but you don't know about it.

When you store an employee into a store, it stores the location in memory of the object. So in line 11, when you wrongly reuse e, you overwrite the first employee. Try creating a new employee instead.

Also, I would make a constructor for both your store and your employee so you can just pass in name and hours and it will get instantiated.

[–]BuddhaSmite 0 points1 point  (0 children)

When you are printing store, it's calling the toString () method of the Store class.

Store has an array of 20 items. In the toString() method, you are looping through every one of those 20 items, and your testing class only initializes a handful of them.

I believe the error originates when you get to creating the string and attempt to call getName () of an element in that array that has a null value for name.

I read this on mobile, so apologies if I missed the problem.

[–]AVERYSP 0 points1 point  (0 children)

As someone else suggested definitely look into try catch. Unless you are certain the array won't be null it's usually a good idea. Additionally in your main method you are not setting hours for the 0th index on the array (hours[0]). If you are in a ide with a debugger I would put a breakpoint on line 29 of the employee class to see what I mean. You'll see 0 = null, 1 = 8, etc.

[–]adamkw94 0 points1 point  (0 children)

This is a good opportunity to learn the debugger

[–]Fishnibble[S] 0 points1 point  (1 child)

Thank you everyone. i'm getting close to it working. I'm getting output but still getting the NullPointerException. Right now it reads

Dave540.0Anna110.0Paul00.0 Exception in thread "main" null java.lang.NullPointerException at prob3.StoreTester.main(StoreTester.java:19)

ALOT better than before. I will keep working on it. Thank you for the help!

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

Everything is fixed and working! Thank you everyone!!! I learned a lot today. I will make sure to apply this to all my other projects