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 8 of 8

Thread: console error

  1. #1
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default console error

    hey there, trying to do some college work although i keep getting a console error. i am tying to add three employees (an admin and 2 engineers) to the manager and then call printStaffDetails()

    test class:

     
    public class Test 
    {
    	public static void main(String[] args) 
    	{
    		Engineer eng1 = new Engineer(101, "Jane Smith", "012-34-5678", 120567.36);
    		Manager man = new Manager(207, "Barbara Johnson", "054-12-2367", 109501.36,"US Marketing");
    		Admin ad = new Admin(304, "Bill Munroe", "108-23-6509", 75002.34);
    		Director dir = new Director(12, "Susan Wheeler", "099-45-2340", 120,"Global Marketing", 100000.0);
    		Engineer eng2 = new Engineer(120, "Bill Lecomte", "045-89-1010", 110.450);
     
     
     
    		System.out.println(eng1);
    		System.out.println(man);
    		System.out.println(ad);
    		System.out.println(dir);
    		System.out.println(eng2);
     
    		man.raiseSalary(10000);
    		man.setName("Barbara Johnson-Smythe");
     
    		System.out.println("CHANGE");
    		System.out.println(man);
     
    		man.addEmployee(ad);
    		man.addEmployee(eng1);
    		man.addEmployee(eng2);
    		man.printStaffDetails();
    	}
     
    }

    Manager class:

    import java.text.NumberFormat;
     
     
    public class Manager extends Employee
    {
    	private String deptName;
    	private Employee[] staff;
    	private int employeeCount = 0;
     
    	public Manager(int empid, String name, String ssn, double salary, String deptName)
    	{
    		super(empid, name, ssn, salary);
    		this.deptName = deptName;
    		staff = new Employee[20];
    	}
     
    	public String getDeptName()
    	{
    		return deptName;
    	}
     
    	public String toString()    
    	{        
    		return " Employee ID: "+getId()+"\n Employee Name: "+getName()+"\n Employee SSN: "+getSsn()
    				+"\n Employee Salary: €"+getSalary()+"\n Departmnt: "+deptName+"\n";    
    	} 
     
    	public int findEmployee(String name)
    	{
    		int value = -1;
    		for(int i = 0; i < staff.length; i++)
    		{
    			if(name.equals(staff[i].getName()))
    			{
    				value = i;
    			}
    		}
    		return value;
    	}
     
    	public boolean addEmployee(Employee emp)
    	{
    		boolean b;
    		int found = findEmployee(emp.getName());
    		if(found == -1)
    		{
    			staff[employeeCount] = emp;
    			b = true;
    			employeeCount ++;
     
    		}
    		else
    		{
    			b = false;
    		}
    		return b;
    	}
     
    	public boolean removeEmployee(Employee emp)
    	{
    		boolean b = false;
    		Employee[] temp = new Employee[20];
    		int counter = 0;
    		for(int i = 0; i < staff.length; i++)
    		{
    			if(emp.getId() != staff[i].getId())
    			{
    				temp[i] = staff[i];
    				counter++;
    			}
    			else if(emp.getId() == staff[i].getId())
    			{
    				b = true;
    				staff = temp;
    				employeeCount = counter;
    			}
    		}
    		return b;
    	}
     
    	public void printStaffDetails()
    	{
    		System.out.println("Staff of "+getName()+": \n" );
    		for(int i = 0; i < staff.length; i++)
    		{
    			System.out.println("Name: "+staff[i].getName()+"\t id: "+staff[i].getId());
    		}
    	}
    }

    error in console:
     Employee ID: 101
     Employee Name: Jane Smith
     Employee SSN: 012-34-5678
     Employee Salary: €120567.36
     
     Employee ID: 207
     Employee Name: Barbara Johnson
     Employee SSN: 054-12-2367
     Employee Salary: €109501.36
     Departmnt: US Marketing
     
     Employee ID: 304
     Employee Name: Bill Munroe
     Employee SSN: 108-23-6509
     Employee Salary: €75002.34
     
     Employee ID: 12
     Employee Name: Susan Wheeler
     Employee SSN: 099-45-2340
     Employee Salary: €120.0 
     Department: Global Marketing
     Budget: €100000.0
     
     Employee ID: 120
     Employee Name: Bill Lecomte
     Employee SSN: 045-89-1010
     Employee Salary: €110.45
     
    CHANGE
     Employee ID: 207
     Employee Name: Barbara Johnson-Smythe
     Employee SSN: 054-12-2367
     Employee Salary: €119501.36
     Departmnt: US Marketing
     
    Exception in thread "main" java.lang.NullPointerException
    	at Manager.findEmployee(Manager.java:33)
    	at Manager.addEmployee(Manager.java:44)
    	at Test.main(Test.java:26)

    cheers


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: console error

    Exception in thread "main" java.lang.NullPointerException
    at Manager.findEmployee(Manager.java:33)
    There is a variable with a null value on line 33. Look at line 33 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 33 and print out the values of all the variables on that line.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    darego (April 23rd, 2013)

  4. #3
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: console error

    i really can't see how there can be a null value on line 33:
    if(name.equals(staff[i].getName()))

    i have been trying to get this problem solved for the last 2 hours so i am getting very frustrated

    i get no error when i delete this code that adds the admin and engineers to the manager..

    man.addEmployee(ad);
    man.addEmployee(eng1);
    man.addEmployee(eng2);
    man.printStaffDetails();

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: console error

    how there can be a null value on line 33:
    What are the values of:
    name
    and
    staff[i]
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    darego (April 23rd, 2013)

  7. #5
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: console error

    the value of name would be whatever i passed in as parameters for name and staff[i].getName() gets the name from whatever number staff object in the staff array. i can't see how these would be null?

    apologies if i am missing something so simple.. i have been at this for so long now my brain doesn't seem to be working anymore lol

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: console error

    I wasn't asking what the values of those variables should be or what they could be.
    I was asking what their values were just before the exception happened. If one is null, that will cause the error.
    Add a println() statement to print their values.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    darego (April 23rd, 2013)

  10. #7
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: console error

    i get a name returned for name but i don't get anything for staff[i].getName().. so this is my problem? not sure why staff[i].getName() is not returning a name

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: console error

    i don't get anything for staff[i].getName()
    Please explain. What does the getName() method return? An empty String? Like ""?

    Are you still getting the NullPointerException?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Asking Console
    By righi in forum Threads
    Replies: 5
    Last Post: June 20th, 2012, 11:46 AM
  2. console port Cisco
    By phantom89 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: May 13th, 2012, 10:47 AM
  3. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM
  4. get console to work
    By clankill3r in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 22nd, 2011, 07:31 PM
  5. From Console to GUI : problem !!
    By hexwind in forum AWT / Java Swing
    Replies: 33
    Last Post: August 20th, 2011, 10:50 PM