Can't Figure out Why I am Getting Null Pointer Exception
Hello,
Following is my Code.
Code :
public class ClearTech {
String EmpCode = new String();
String EmpName;
String Address;
String DOB;
double MonthlySalary;
double MontlyTax;
double NetSalary;
byte NoOfDaysinMonth;
byte NoOfLeaves;
double NetPayableSalary;
ClearTech()
{
EmpCode="Init Value";
EmpName = "Init Value";
Address = "Init Value";
DOB = "Init Value";
}
public static void main(String[] args) {
ClearTech CT = new ClearTech();
PerEmployee PE = CT.new PerEmployee();
PE.Accept();
PE.Display();
}
class PerEmployee
{
int[] AnnualSalary = new int[3];
ClearTech[] CT = new ClearTech[3];
public void Accept()
{
CT[0].EmpCode = "E0001";
CT[0].EmpName = "Bob";
CT[0].Address = "E-12 Lajpat Nagar";
CT[0].DOB = "01/Feb/1974";
AnnualSalary[0] = 800000;
CT[1].EmpCode = "E0002";
CT[1].EmpName = "Kevin";
CT[1].Address = "E-15 Mandir Marg";
CT[1].DOB = "01/Apr/1990";
AnnualSalary[1] = 1000000;
CT[2].EmpCode = "E0003";
CT[2].EmpName = "Mohan";
CT[2].Address = "E-15 Mandir Marg";
CT[2].DOB = "31/July/1984";
AnnualSalary[2] = 400000;
System.out.println("Done with Accept");
}
public void Display()
{
System.out.println("Emp Code\tName\tAddress\t\tDate of Birth\tAnnual Basic Salary");
for(int i=1;i<4;i++)
{
System.out.println(CT[i].EmpCode+"\t"+CT[i].EmpName+"\t"+CT[i].Address+"\t"+CT[i].DOB+"\t"+AnnualSalary[i]);
}
}
}
class TempEmployee
{
int RatePerWorkingDay;
}
}
I am not able to figure out Why I am getting a Null pointer exception. I am new to this so please help as soon as possible as the deadline for my submission is 00:00 hours today (11th March 2012). Thanks in advance.
Re: Can't Figure out Why I am Getting Null Pointer Exception
Quote:
Originally Posted by
seizitp
I am not able to figure out Why I am getting a Null pointer exception.
Post the full stack trace of your NullPointerException and mark the place in your code where it's thrown with something like
Code java:
myExistingCode(anObjectIMade); // <-- NPE thrown here
Re: Can't Figure out Why I am Getting Null Pointer Exception
Hello seizitp!
I think that your array (CT) needs to be initialised instead of just being declared(ClearTech[] CT = new ClearTech[3];) before you can assign it values. You can accomplish that by adding a for loop which initialises CT in your Accept() method. And something more; you should check again the indexes here
Code Java:
for(int i=1;i<4;i++)
{
System.out.println(CT[i].EmpCode+"\t"+CT[i].EmpName+"\t"+CT[i].Address+"\t"+CT[i].DOB+"\t"+AnnualSalary[i]);
}
otherwise you 'll face another exeption.
Re: Can't Figure out Why I am Getting Null Pointer Exception
Quote:
Originally Posted by
andreas90
Hello seizitp!
I think that your array (
CT) needs to be initialised instead of just being declared(
ClearTech[] CT = new ClearTech[3];) before you can assign it values. You can accomplish that by adding a
for loop which initialises
CT in your
Accept() method. And something more; you should check again the indexes here
Code Java:
for(int i=1;i<4;i++)
{
System.out.println(CT[i].EmpCode+"\t"+CT[i].EmpName+"\t"+CT[i].Address+"\t"+CT[i].DOB+"\t"+AnnualSalary[i]);
}
otherwise you 'll face another exeption.
Hello Andreas90,
Thanks for the correction in the java code. I got it corrected. But my problem still persists. I declared a Constructor for the Parent class (ClearTech) in this case. But still getting the same error. Array of objects is really confusing me. I would really appreciate if you show me an example as of how to Initialize the Array of objects before they could be used.
Thanks in advance.
Re: Can't Figure out Why I am Getting Null Pointer Exception
There are plenty of initialising arrays examples in the Internet. Google is always your best friend. ;)
Code java:
public void initialiseArray {
Empoloyee[] employee = new Employee[5];
for(int i = 0; i < employee.length; i++) {
employee[i] = new Employee();
}
}
Re: Can't Figure out Why I am Getting Null Pointer Exception
Thanks andreas90 :D
Just before you posted the solution I figured it out..
Yes! Google was a real help :)
I think it's best to quote ' After a Dog, Google is Man's best friend :)'