Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Can't Figure out Why I am Getting Null Pointer Exception

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Can't Figure out Why I am Getting Null Pointer Exception

    Hello,
    Following is my 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.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Can't Figure out Why I am Getting Null Pointer Exception

    Quote Originally Posted by seizitp View Post
    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
     myExistingCode(anObjectIMade); // <-- NPE thrown here

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

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

  4. The Following User Says Thank You to andreas90 For This Useful Post:

    seizitp (March 11th, 2012)

  5. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't Figure out Why I am Getting Null Pointer Exception

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

  6. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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.
              public void initialiseArray {
                 Empoloyee[] employee = new Employee[5];
                 for(int i = 0; i < employee.length; i++) {
                    employee[i] = new Employee();
                 }
              }

  7. The Following User Says Thank You to andreas90 For This Useful Post:

    seizitp (March 11th, 2012)

  8. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Can't Figure out Why I am Getting Null Pointer Exception

    Thanks andreas90
    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 '
    Last edited by seizitp; March 11th, 2012 at 10:33 AM.

Similar Threads

  1. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 09:00 PM
  2. Null Pointer Exception?
    By SeanEE89 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 16th, 2011, 06:21 PM
  3. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM