NullPointerException Error
Author |
Message |
javanoob
|
Posted: Fri May 30, 2008 3:04 pm Post subject: NullPointerException Error |
|
|
As you can see from my name, I am a Java noob, so bear with me. I made a program for my grade 12 comp sci class using classes. I have a "worker" class, "employee" class, and my driver. The driver is the problem. The program allows the user to create a "worker", or an "employee", and once those are created, it allows them to look up created people. So, here is the problem.
code: |
System.out.println("Would you like to display a worker or employee?");
System.out.println("Type 'w' for worker, 'e' for employee, or 'n'to skip.");
String input = myInput.readLine();
answer = input;
while (!answer.equalsIgnoreCase("n"))
{
if (answer.equalsIgnoreCase("w"))
{
System.out.println("Which worker would you like to display?");
input = myInput.readLine();
String query = input;
for (int j = 0 ; j<numWorkers ; j++)
{
if (workers[j].getWname().equalsIgnoreCase(query))
{
System.out.println(workers[j]);
}
}
System.out.println("Would you like to display a worker or employee?");
System.out.println("Type 'w' for worker, 'e' for employee, or 'n'to skip.");
input = myInput.readLine();
answer = input;
}
else if (answer.equalsIgnoreCase("e"))
{
System.out.println("Which employee would you like to display?");
input = myInput.readLine();
String query = input;
for (int k = 0 ; k<numEmployees ; k++)
{
if (employees[k].getName().equalsIgnoreCase(query))
{
System.out.println(employees[k]);
}
}
System.out.println("Would you like to display a worker or employee?");
System.out.println("Type 'w' for worker, 'e' for employee, or 'n'to skip.");
input = myInput.readLine();
answer = input;
}
}
|
If I create a worker, and display it, it works...if I create 10 workers, I can display all of them, but if I try to create a worker, then an employee, I get the NullPointerExcpeption error when i try to display either of them. Any ideas??? I thought I fixed it once, but the next day it wasn't working again. Thanks.
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Fri May 30, 2008 4:22 pm Post subject: RE:NullPointerException Error |
|
|
it will help a lot if you tell us at what line did this error occured, as well as showing us the whole code with your worker & employee class (if too long, there is the attachment option) because that snip doesn't help at all
|
|
|
|
|
![](images/spacer.gif) |
javanoob
|
Posted: Fri May 30, 2008 5:34 pm Post subject: Re: NullPointerException Error |
|
|
OK sorry about that.... here they are, and the error occurs on lines 68 or 89 depending on which I created second. If I create a worker first and then an employee, when I look up the employee it gives me the error on line 89, and visa versa.
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
Worker.java |
Filesize: |
1.51 KB |
Downloaded: |
62 Time(s) |
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
employeeDriver.java |
Filesize: |
19.58 KB |
Downloaded: |
64 Time(s) |
Description: |
|
![](http://compsci.ca/v3/pafiledb/images/icons/clip.gif) Download |
Filename: |
Employee.java |
Filesize: |
2.71 KB |
Downloaded: |
78 Time(s) |
|
|
|
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Fri May 30, 2008 8:55 pm Post subject: RE:NullPointerException Error |
|
|
what you did wrong was, you used 1 loop trying to create two seperate arrays, which is fine. but you are using the variable i, instead of the numberOfWorkers, or numberOfEmployee, which resulting you to skip some index of either employee or worker depending on which you make,
example, if i make a worker, index i goes from 0 to 1, and worker at index 0 of its array, after that i make a employee, index i goes from 1 to 0, and employee is made at index 1 of its array, and now if i want to look up my employee, at index 0, it would be null (cuz you didn't make anything there), and so resulting your null exception
now just some side things to improve your code, first thing, why would you extends Employee if you dont even make use of it? and another problem is your main "driver" class, be sure to make good use of the methods. and i noticed some logic error, eg, your loop where you had ignore case "exit" or i < 100, and some other minor things.
that is all
i hope you understood what i said
|
|
|
|
|
![](images/spacer.gif) |
Reality Check
|
Posted: Fri May 30, 2008 11:53 pm Post subject: Re: NullPointerException Error |
|
|
You're using one loop to assign an index of the array to its corresponding object. This means that after creating, say a worker, you're then assigning an employee (if you create an employee after worker) to an index higher then what it should be. Therefore, you might have an employee at index 1 and null at 0 so when you try and reference index 0, which of course points at nothing and returns that error.
On a side note, for something like this, look up ArrayList class as it will greatly help your organization and overall functionality of your program.
Edit: Oh wow, HeavenAgain already answered. I should start reading the replies
Still take my ArrayList suggestion.
|
|
|
|
|
![](images/spacer.gif) |
javanoob
|
Posted: Sat May 31, 2008 7:57 am Post subject: Re: NullPointerException Error |
|
|
OK, I think I understand what you guys are saying is wrong, but what do I need to do to fix it? Thanks for the help guys!
|
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Sat May 31, 2008 12:05 pm Post subject: RE:NullPointerException Error |
|
|
Make sure you understand array indexing.
|
|
|
|
|
![](images/spacer.gif) |
|
|