Results 1 to 9 of 9
- 03-11-2012, 08:50 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Please help me with the code. I get Null Pointer Exception.Can't understand reason
Following is my code:
Java Code:public class ClearTech { String EmpCode; String EmpName; String Address; String DOB; double MonthlySalary; double MontlyTax; double NetSalary; byte NoOfDaysinMonth; byte NoOfLeaves; double NetPayableSalary; 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[3].EmpCode = "E0001"; CT[3].EmpName = "Bob"; CT[3].Address = "E-12 Lajpat Nagar"; CT[3].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; } }
What I don't understand is where am I going wrong. When I run the code it says Null Pointer Exception. Please help.
- 03-11-2012, 08:58 AM #2
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso
Please include a correction at line 35 to 39.Java Code:CT[0].EmpCode = "E0001"; CT[0].EmpName = "Bob"; CT[0].Address = "E-12 Lajpat Nagar"; CT[0].DOB = "01/Feb/1974"; AnnualSalary[0] = 800000;
- 03-11-2012, 09:41 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso
When you create an array of objects of some type (here: a ClearTech array), each element *can* refer to an object of that type but it doesn't do that yet, i.e. each element is null; you have to make each element refer to such an object. You are assuming that those elements do refer to objects and you're trying to alter the members of those (none existent) objects (see CT[0].EmpName for example).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-11-2012, 09:49 AM #4
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso
- 03-11-2012, 02:11 PM #5
Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso
An array is like an egg tray. Once you have one, you still have to put eggs in before you can boil or do anything else with them.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-11-2012, 04:08 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
-
Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso
To be as concrete as possible, this:
creates an array of Foo, but the array holds no objects, just empty placeholders. To be able to use Foo objects held in the array, you must first fill the array with objects:Java Code:Foo[] myFooArray = new Foo[5];
Java Code:Foo[] myFooArray = new Foo[5]; for (int i = 0; i < myFooArray.length; i++) { myFooArray[i] = new Foo(); }
- 03-11-2012, 04:25 PM #8
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
-
Similar Threads
-
Null pointer exception
By mathidioticz in forum New To JavaReplies: 12Last Post: 02-04-2012, 06:38 PM -
Null pointer exception
By DBaskov in forum New To JavaReplies: 14Last Post: 07-10-2011, 11:16 PM -
null pointer exception
By bequick01 in forum New To JavaReplies: 3Last Post: 04-28-2011, 08:31 PM -
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks