aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Null Pointer Exception" Watch "Null Pointer Exception" New topic
Author

Null Pointer Exception

Saba Shahrukh
Greenhorn

Joined: Feb 13, 2013
Posts: 19
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: 1531
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: 19

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: 3734
    
  16
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: 19
in the constructor, i am passing the values while creating an object..
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3734
    
  16
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: 19
Oo ya .. actually I did not initialize it that's why its showing the Exception there..

thanks..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Null Pointer Exception