I am new to Java, and I have been trying to get an assignment coded and keep gettin java.lang.NullPointerException in Main Class.java:39. Ok, no prob. Unfortunately I can't quite get this fixed. Any suggestions? I'm using Eclipse Juno. Also, any suggestions on any books, sites, etc that review java in layman's terms would be greatly appreciated.

Main

import javax.swing.JOptionPane;

public class Main_Class {

    public static void main(String[] args) {

        //declare variables and make objects
        String inputVal;
        Employee newEmployee1 = new Employee();
        Benefit benefit1 = new Benefit("Cigna", 25000, 10);
        Salaried salary1 = new Salaried(1);

        //Introduction message
        System.out.println("Welcome to the Employee Lab");
        System.out.println("CIS247, Week 5 Lab");
        System.out.println("Name:  Phil Sutton");
        System.out.println("This program makes instances of the Employee class and outputs the data");

        //gather input of employee one and display 
        displayDivider("Employee Information");
        inputVal = getInput("first name");
        newEmployee1.setFirstName(inputVal);
        inputVal = getInput("last name");
        newEmployee1.setLastName(inputVal);
        inputVal = getInput("gender - Male (M), Female (F)");
        char g = inputVal.charAt(0);
        newEmployee1.setGender(g);
        inputVal = getInput("dependents");
        newEmployee1.setDependents(inputVal);
        inputVal = getInput("annual income");
        newEmployee1.setAnnualSalary(inputVal);
        inputVal = getInput("health insurance");
        newEmployee1.benefit.setHealthInsurance(inputVal);
        inputVal = getInput("life insurance");
        newEmployee1.benefit.setLifeInsurance(Double.parseDouble(inputVal));
        inputVal = getInput("vacation days");
        newEmployee1.benefit.setVacation(Integer.parseInt(inputVal));
        inputVal = getInput("management level");
        newEmployee1.salary.setManagementLevel(Integer.parseInt(inputVal));
        System.out.println(newEmployee1.toString());
        System.out.println("Number of employees: " + Employee.getNumEmployees());

        //display info for employee two
        Salaried newEmployee = new Salaried("Maria", "Noia", 'F', 5, 24000, benefit1, salary1, g);
        displayDivider("Employee Information");
        System.out.println(newEmployee.toString());
        System.out.println("Number of employees: " + Employee.getNumEmployees());

        //display info for employee three



    }

    //method for the dividers
    public static void displayDivider(String outputTitle)
    {
        System.out.println("*************"+outputTitle+"***************");
    }

    //gathering input from user
    public static String getInput(String inputType)
    {
        String strInput;
        strInput = JOptionPane.showInputDialog("Enter " + inputType + ": ");
        return strInput;
    }

}

Employee

import java.text.NumberFormat;

public class Employee{
    protected String firstName;
    protected String lastName;
    protected char gender;
    protected int dependents;
    protected double annualSalary;
    private static int numberOfEmployees = 0;
    protected Benefit benefit;
    protected Salaried salary;
    NumberFormat nf = NumberFormat.getCurrencyInstance();

    //default constructor
    public Employee() {
        firstName = null;
        lastName = null;
        gender = 'u';
        dependents = 0;
        annualSalary = 0;
        benefit = new Benefit(firstName, dependents, dependents);
        numberOfEmployees++;
    }

    //constructor
    public Employee(String firstName, String lastName, char gender, int dependents, double annualSalary, Benefit benefit1, Salaried salary1) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.dependents = dependents;
        this.annualSalary = annualSalary;
        this.benefit = benefit1;
        numberOfEmployees++;
    }

    //display the employee information when invoked
    public String toString(){
        String s;
        s = "First Name: " + firstName  + "\n";
        s += "Last Name: " + lastName + "\n";
        s += "Gender: " + gender + "\n";
        s += "Dependents: " + dependents + "\n";
        s += "Annual Salary: " + nf.format(annualSalary) + "\n";
        s += "Weekly Income: " + nf.format(calculatePay()) + "\n";
        s += benefit.toString();
        s += salary.toString();
        return s;
    }

    //First Name
    public String getFirstName(){
        return firstName;
    }

    public void setFirstName(String firstName){
        this.firstName = firstName;
    }

    //Last Name
    public String getLastName(){
        return lastName;
    }

    public void setLastName(String lastName){
        this.lastName = lastName;
    }

    //Gender
    public char getGender(){
        return gender;
    }

    public void setGender(char gender){
        this.gender = gender;
    }

    //Dependents
    public int getDependets(){
        return dependents;
    }

    public void setDependents(int dependents){
        this.dependents = dependents;
    }

    //overloaded Dependents
    public void setDependents(String sDepend){
        int depend = Integer.parseInt(sDepend);
        this.dependents = depend;
    }

    //Annual Salary
    public double getAnnualSalary(){
        return annualSalary;
    }

    public void setAnnualSalary(double annualSalary){
        this.annualSalary = annualSalary;
    }

    //overloaded Annual Salary that accepts a string
    public void setAnnualSalary(String sSalary){    
        double salary = Double.parseDouble(sSalary);
        this.annualSalary = salary;
    }

    //Calculate the weekly income
    public double calculatePay(){
        double weeklyPay = annualSalary / 52;
        return weeklyPay;
    }

    public static int getNumEmployees(){
        return numberOfEmployees;
    }


}

Salaried

import javax.swing.JOptionPane;


public class Salaried extends Employee {

    //declare variables
    private static final int MIN_MANAGEMENT = 0;
    private static final int MAX_MANAGEMENT = 3;
    private static final double BONUS_PERCENT = 0.1;
    private int managementLevel;
    String inputVal;

    //Constructors
    public Salaried() {
        super();
         managementLevel = 0;
    }

    public Salaried(int managementLevel){
        super();
        this.managementLevel = managementLevel;
    }

    public Salaried(String firstName, String lastName, char gender,int dependents, double annualSalary, Benefit benefit1, Salaried salary1, int managementLevel) {
        super(firstName, lastName, gender, dependents, annualSalary, benefit1, salary1);

        this.managementLevel = managementLevel;
    }


    public void setManagementLevel(int managementLevel){
        if (managementLevel >= MIN_MANAGEMENT && managementLevel <= MAX_MANAGEMENT) {
            this.managementLevel = managementLevel;
        }
        else {
            this.managementLevel = 0;
        }
    }

    public double getManagementLevel(){
        return managementLevel;
    }

    @Override
    public String toString(){
        String s = super.toString();
        s += "Management level: " + managementLevel + "\n";
        return s;
    }

    //calculate managerial bonus
    public double calculatePay(){
        double bonusPercent = getAnnualSalary() * (getManagementLevel() * BONUS_PERCENT);
        return  bonusPercent;
    }

    public static String getInput(String inputType)
    {
        String strInput;
        strInput = JOptionPane.showInputDialog("Enter " + inputType + ": ");
        return strInput;
    }

}

Benefit

public class Benefit {

    public Benefit(String string, int i, int j) {
        // TODO Auto-generated constructor stub
    }

    public void setHealthInsurance(String inputVal) {
        // TODO Auto-generated method stub

    }

    public void setLifeInsurance(double parseDouble) {
        // TODO Auto-generated method stub

    }

    public void setVacation(int parseInt) {
        // TODO Auto-generated method stub

    }

}

NullPointerException in Main Class.java:39

Look at line 39 in Main_Class and find the variable that has a null value. Then backtrack in the code to find out why that variable does not have a valid non-null value.

Edited 4 Years Ago by NormR1

That's where my issue comes up. I have not found anything that appears to have a null value regarding line 39 in Main_Class. I tracked my changes and edited line 7 in Salaried to clean that up. Still the same output.
I just want to know if anyone can tell me which line(s) to edit? I am totally lost, this is way different than C++ for me.

//declare variables
    private static final int MIN_MANAGEMENT = 1;
    private static final int MAX_MANAGEMENT = 3;
    private static final double BONUS_PERCENT = 0.1;
    private int managementLevel;
    String inputVal;

    //Constructors
    public Salaried() {
        super();
         managementLevel = 0;
    }

    public Salaried(int managementLevel){
        super();
        this.managementLevel = managementLevel;
    }

    public Salaried(String firstName, String lastName, char gender,int dependents, double annualSalary, Benefit benefit1, Salaried salary1, int managementLevel) {
        super(firstName, lastName, gender, dependents, annualSalary, benefit1, salary1);

        this.managementLevel = managementLevel;
    }


    public int setManagementLevel(int managementLevel){
        if (managementLevel >= MIN_MANAGEMENT && managementLevel <= MAX_MANAGEMENT) {
            this.managementLevel = managementLevel;
        }
        else {
            this.managementLevel = 0;
        }
        return managementLevel;
    }

    public double getManagementLevel(){
        return managementLevel;
    }

    @Override
    public String toString(){
        String s = super.toString();
        s += "Management level: " + managementLevel + "\n";
        return s;
    }

    //calculate managerial bonus
    public double calculatePay(){
        double bonusPercent = getAnnualSalary() * (getManagementLevel() * BONUS_PERCENT);
        return  bonusPercent;
    }

Edited 4 Years Ago by phil.sutton

try initializing your salary variable in the constructor of Employee. since you didn't do that, that variable remains null, and will throw a NullPointerException once you try to perform an action on it.

I have not found anything that appears to have a null value regarding line 39 in Main_Class.

Try debugging the code by printing out the values of all the variables used on line 39. The printed values will show what variable is null.

This article has been dead for over six months. Start a new discussion instead.