• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Null Pointer Exception

 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you getting null pointer exception?
Use codetags.
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Saba Shahrukh
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the constructor, i am passing the values while creating an object..
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oo ya .. actually I did not initialize it that's why its showing the Exception there..

thanks..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic