| Author |
Null Pointer Exception
|
Saba Shahrukh
Greenhorn
Joined: Feb 13, 2013
Posts: 4
|
|
I have written a code but its generating a run time Exception which I am not able to debug...
class Employee
{
String [] id;
String [] name;
String [] salary;
static int count = 0;
Employee(String i, String n,String s)
{
id[count] = i;
name[count] = n;
salary[count] = s;
count ++;
}
public void showAll()
{
for(int i = 0 ; i <id.length ; i++)
{
System.out.println(id[i]+" is the ID of "+ name[i] + " and earns " + salary[i]);
}
}
public String[] showById(int i)
{
String[] ans = {id[i],name[i],salary[i]};
return ans;
}
}
class UseEmployee
{
public static void main(String [] args)throws NullPointerException
{
Employee emp1 = new Employee("1","first","15000.25f");
Employee emp2 = new Employee("2","second","25000.25f");
Employee emp3 = new Employee("3","third","35000.25f");
Employee emp4 = new Employee("4","fourth","45000.25f");
emp1.showAll();
}
}
please help...
|
 |
Akhilesh Trivedi
Ranch Hand
Joined: Jun 22, 2005
Posts: 1493
|
|
Where are you getting null pointer exception?
Use codetags.
|
Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
|
 |
Saba Shahrukh
Greenhorn
Joined: Feb 13, 2013
Posts: 4
|
|
Inside the constructor when i am trying to initialize the array and if i catch the exception then I get it at the initialization statement of for loop.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Saba Shahrukh wrote:
Inside the constructor when i am trying to initialize the array and if i catch the exception then I get it at the initialization statement of for loop.
Where do you initialise id, where do you initialise name, where do you initialise salary ?
|
Joanne
|
 |
Saba Shahrukh
Greenhorn
Joined: Feb 13, 2013
Posts: 4
|
|
|
in the constructor, i am passing the values while creating an object..
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Saba Shahrukh wrote:in the constructor, i am passing the values while creating an object..
No. That's where you try and assign values to an element of those arrays. But where do you actually create the arrays ?
|
 |
Saba Shahrukh
Greenhorn
Joined: Feb 13, 2013
Posts: 4
|
|
Oo ya .. actually I did not initialize it that's why its showing the Exception there..
thanks..
|
 |
 |
|
|
subject: Null Pointer Exception
|
|
|