Results 1 to 6 of 6
  1. #1
    Darego is offline Member
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    12
    Rep Power
    0

    Default error in console

    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() method from the manager class.

    test class:
    Java Code:
    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:
    Java Code:
    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());
    		}
    	}
    }
    console (with error at the end):
    Java Code:
     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)
    desired console output:
    Java Code:
    Employee ID:     101
    Employee Name:   Jane Smith
    Employee SSN:    012-34-5678
    Employee Salary: €120,345.27
    -----------------------------
    Employee ID:     120
    Employee Name:   Bill Lecomte
    Employee SSN:    045-89-1010
    Employee Salary: €110,450.34
    -----------------------------
    Employee ID:     304
    Employee Name:   Bill Munroe
    Employee SSN:    108-23-6509
    Employee Salary: €75,002.34
    -----------------------------
    Employee ID:     207
    Employee Name:   Barbara Johnson
    Employee SSN:    054-12-2367
    Employee Salary: €109,501.36
    Department:      US Marketing
    -----------------------------
    Employee ID:     12
    Employee Name:   Susan Wheeler
    Employee SSN:    099-45-2340
    Employee Salary: €120,567.36
    Department:      Global Marketing
    Budget:          1000000.0
    -----------------------------
    
    Testing raiseSalary and setName on Manager:
    Employee ID:     207
    Employee Name:   Barbara Johnson-Smythe
    Employee SSN:    054-12-2367
    Employee Salary: €119,501.36
    Department:      US Marketing
    
    Success: added admin
    Success: added eng1
    Success: added eng2
    
    Staff of Barbara Johnson-Smythe:
    Name: Bill Munroe Employee id: 304
    Name: Jane Smith Employee id: 101
    Name: Bill Lecomte Employee id: 120
    Last edited by Darego; 04-23-2013 at 09:44 PM.

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: error in console

    what is the stack trace of the error thats printed to the console? what line does it get thrown?

  3. #3
    Join Date
    Apr 2013
    Posts
    41
    Rep Power
    0

    Default Re: error in console

    yes like the user above lets see the stack trace. edit it for this:

    Java Code:
    try{
    System.out.println(eng1);
            System.out.println(man);
            System.out.println(ad);
            System.out.println(dir);
            System.out.println(eng2);}
    catch(IOException e){
    	e.printStackTrace();
    		}

  4. #4
    Darego is offline Member
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    12
    Rep Power
    0

    Default Re: error in console

    haven't covered exception handling yet guys unfortunately.. still getting this error, can anyone help please? :)

    when i take away this code from Test class everything is fine:

    Java Code:
    man.addEmployee(ad);
            man.addEmployee(eng1);
            man.addEmployee(eng2);
            man.printStaffDetails();

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,006
    Blog Entries
    7
    Rep Power
    20

    Default Re: error in console

    In your Manager class you have an array ready for Employees (20 elements); when you try to add an Employee the array doesn't contain Employee objects in each of the array elements (yet); they are null.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Darego is offline Member
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    12
    Rep Power
    0

    Default Re: error in console

    thanks a lot mate. will try fix it now

Similar Threads

  1. Console
    By Paus34 in forum New To Java
    Replies: 5
    Last Post: 03-27-2013, 01:14 PM
  2. Help with Console
    By thiagohe in forum Advanced Java
    Replies: 7
    Last Post: 08-01-2011, 01:30 PM
  3. Applet Error - Can't see console
    By eZs3 in forum Java Applets
    Replies: 3
    Last Post: 05-21-2008, 09:54 PM
  4. I want to do is clear the console
    By paul in forum Advanced Java
    Replies: 7
    Last Post: 08-03-2007, 06:54 PM

Posting Permissions

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