0

below is an app im working on:

  • MainClass
  • Employee
  • Manager
  • QATester
  • Devloper

This is the Example Scenario:

1) For the given hierarchy:
    Manager A  $300
        Manager B  $300
            Developer $1000
            QA Tester $500

2) Manager A’s allocation should be: $2100

The issue I am currently having is when I run the code I get the following Error:

Exception in thread "main" java.lang.NullPointerException
at expenseApp.Manager.add(Manager.java:28)
at main.MainClass.main(MainClass.java:15)

I am a little confused as to what is going on and how it can be fixed. Could you please help? I'm relatively new to programming so examples of suggests do a lot for me. In Manager I left in some commented out code which represents a few of my attempts to fix.

Thank you.

package main;

import expenseApp.Developer;
import expenseApp.Manager;
import expenseApp.QATester;

public class MainClass {

    public static void main(String[] args) 
    {
        QATester tester1 = new QATester();
        Developer dev1 = new Developer();

        Manager managerB = new Manager();
        **managerB.add(tester1);**
        managerB.add(dev1);

        Manager managerA = new Manager();
        managerA.add(managerB);

    }

}

package expenseApp;

public abstract class Employee 
{
    public abstract int getExpenses();
}

package expenseApp;

import java.util.ArrayList;

public class Manager extends Employee
{

        private ArrayList<Manager> managerList; //= new ArrayList<>();
        private ArrayList<Employee> employeeList; //= new ArrayList<>();

        // public Manager(ArrayList<Manager> managers, ArrayList<Employee> employees) 
        // {
        //    this.managerList = new ArrayList<>(managers);
        //    this.employeeList = new ArrayList<>(employees);
        // }

    public void add(Employee employee) 
    {
        if(employee instanceof Manager) 
        {
            //managerList = new ArrayList<>();
            managerList.add((Manager) employee);

        }
        else 
        {
            //employeeList = new ArrayList<>();
            **employeeList.add(employee);**

        }
    }

    @Override
    public int getExpenses() 
    {
         return 300;
    }

    public int getTotalExpenses() 
    {
        int totalExpenses = 0;

        for(Manager manager : managerList)
        {
            totalExpenses += manager.getTotalExpenses();
        }

        for(Employee employee : employeeList)
        {
            totalExpenses += employee.getExpenses();
        }

        return totalExpenses;
    }
}

package expenseApp;

public class QATester extends Employee
{

    @Override
    public int getExpenses() 
    {
        return 500;
    }

}


package expenseApp;

public class Developer extends Employee
{
    @Override
    public int getExpenses() 
    {
        return 1000;
    }
}
Reed Williams
  • 127
  • 1
  • 1
  • 12
  • Perhaps try uncommenting the code which initializes `managerList` etc. – Andy Turner Feb 22 '15 at 14:49
  • Can you highlight the line which the exception was thrown at? – A. Abramov Feb 22 '15 at 14:49
  • list is not initialized so its throwing null pointer `managerList = new Arraylist();` will do the needful – Bhargav Modi Feb 22 '15 at 14:49
  • the first item in your stack trace: 'at expenseApp.Manager.add(Manager.java:28)'. Look at line 28 in Manager.java. There should be a '.' operator, and the lefthand side of the '.' is null. This is the normal way to figure out null pointer exceptions. – Randy Kamradt Sr. Feb 22 '15 at 14:50

2 Answers2

1

You have the code to initialize your member variables commented out, e.g.

private ArrayList<Manager> managerList; //= new ArrayList<>();
private ArrayList<Employee> employeeList; //= new ArrayList<>();

Try:

private ArrayList<Manager> managerList = new ArrayList<>();
private ArrayList<Employee> employeeList = new ArrayList<>();
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
1

That's because your list is not initialized.

private ArrayList<Manager> managerList; //= new ArrayList<>();

You are trying:

managerList.add((Manager) employee);

On a null instance of list as instance variable gets initialized to null by default.

Same applies for employeeList.

SMA
  • 36,381
  • 8
  • 49
  • 73