Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Please help me with the code. I get Null Pointer Exception.Can't understand reason

  1. #1
    seizitp is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Unhappy 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.

  2. #2
    seizitp is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso

    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;
    Please include a correction at line 35 to 39.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    seizitp is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    T

    Hi Jos, than for help..
    But the thing is I m new to this and I did not understand what you are trying to convey here. I would really appriciate if you explain with the help of an example..

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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.

    db
    JosAH likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    seizitp is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso

    Quote Originally Posted by DarrylBurke View Post
    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.

    db
    I don't understand. Please be more specific. I am new to Java and really need to get this done.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso

    To be as concrete as possible, this:

    Java Code:
    Foo[] myFooArray = new Foo[5];
    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];
    for (int i = 0; i < myFooArray.length; i++) {
      myFooArray[i] = new Foo();
    }

  8. #8
    seizitp is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso

    Quote Originally Posted by Fubarable View Post
    To be as concrete as possible, this:

    Java Code:
    Foo[] myFooArray = new Foo[5];
    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];
    for (int i = 0; i < myFooArray.length; i++) {
      myFooArray[i] = new Foo();
    }

    :D
    Thanks a lot..
    I had included for loop but my syntax was wrong.

    I was doing this:
    Java Code:
    for(int i=0; i<CT.length(); i++)
    {
    CT = new ClearTech[i];
    }
    Thanks a lot
    Fubarable

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Please help me with the code. I get Null Pointer Exception.Can't understand reaso

    You're welcome; glad you've got things working.

Similar Threads

  1. Null pointer exception
    By mathidioticz in forum New To Java
    Replies: 12
    Last Post: 02-04-2012, 06:38 PM
  2. Null pointer exception
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 07-10-2011, 11:16 PM
  3. null pointer exception
    By bequick01 in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 08:31 PM
  4. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  5. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •