NullPointerException Error trouble
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: NullPointerException Error trouble

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    3

    NullPointerException Error trouble

    Hello,
    I have 2 classes, Employee, and EmployeeProgram. I keep getting a NullPointerException Error from two of my lines in my main method in EmployeeProgram. I highligted the lines below in green as well, but the lines i'm getting the errors from are:
    double[] monthlyPay = computeMonthlyPay(employees); And
    switch(employees[i].employeeType){

    I am fairly new to java program and have already asked my TA and professor to no avail. If anyone could help me shed some light on this that would be great.

    the assignment was to write a public class Employee with String fields name, address, employeeType (possible types are H for Hourly, S for Salary, M for Management), ssn, and a double field monthlyPay.
    An Hourly Employee has two additional double fields hourlyRate and hoursWorked. A Salary Employee
    has one double field annualSalary. A Management Employee has two double fields regularSalary and
    bonus.
    Then give Employee three constructors one appropriate to initialize the fields for an Hourly Employee, another
    appropriate for a Salary Employee, and the third appropriate for a Management Employee. In main,
    initialize field values for 3 of each type of Employee (Hourly, Salary, Management) into a 9-dimensional array for each field. Now, inside a for-loop, use an appropriate switch statement to select the correct constructor (based on
    employeeType) to instantiate a 9-dimensional Employee object array.
    Write a computePay() method for each of the 3 Employee types. Write a
    second for-loop to call computeMonthlyPay() for each of the 9 Employees, and print out the Employee
    name and monthlyPay.


    here is my Employee class

    public class Employee{

    public String name;
    public String address;
    public char employeeType;
    public String ssn;
    public double monthlyPay;
    public double parameter1;
    public double parameter2;

    public Employee(String n, String a, char eT, String ss, double p1, double p2){
    this.name = n;
    this.address = a;
    this.employeeType = eT;
    this.ssn = ss;
    this.parameter1 = p1;
    this.parameter2 = p2;
    }
    public Employee()
    {}
    public Employee(String n, String a, char eT, String ss, double p1){
    this(n, a, eT, ss, p1, 0);
    }
    public Employee(double p1, double p2, String n, String a, char eT, String ss){
    this(n, a, eT, ss, p1, p2);
    }
    public static double computePay(double hourlyRate, double hoursWorked){

    double pay = hoursWorked * hourlyRate;

    return pay;
    }
    public static double computePay(double annualSalary){
    double pay = annualSalary/12;

    return pay;
    }
    public static double computePay(double regularSalary, double bonus, int x){
    double pay = bonus + (regularSalary/12);
    return pay;
    }
    }


    Here is my EmployeeProgram class

    public class EmployeeProgram extends Employee{
    public static void main(String[] args){
    String[] n = {"Pete Za", "Pepe Roni", "Mary Nara", "Buff Alo", "Frank Pucinni", "Alfred Oh", "Michael Jackson", "Jack Johnson", "Beyonce"};
    String[] a ={"21 Italian ln", "42 Pizza st", "116 Shulze Hall", "14 Oak Street", "17 Safety Dance st", "81 Victory Lap", "42 Thriller St", "13 Hawaii Rd", "33 Single Ladies Ln"};
    char[] eT = {'h', 'h', 'h', 's', 's', 's', 'm', 'm', 'm'};
    String[] ss = {"131451834", "164278937", "893456723", "673567845", "44226633", "78562395", "23436512", "564782345", "45458749"};
    double[] p1 ={8.00, 8.00, 8.00, 53760, 53760, 53760, 67200, 67200, 67200};
    double[] p2 ={40, 40, 40, 0, 0, 0, 40, 40, 40};
    Employee [] employees = new Employee[9];

    for(int i = 0; i < 9; i++){

    switch(eT[i]){
    case'h':
    employees[i] = new Employee(n[i], a[i], eT[i], ss[i], p1[i], p2[i]);
    break;
    case's':
    employees[i] = new Employee(n[i], a[i], eT[i], ss[i], p1[i]);
    break;
    case'm':
    employees[i] = new Employee(p1[i], p2[i], n[i], a[i], eT[i], ss[i]);
    break;
    default:
    System.out.println("Invalid Employee Type");

    }
    double[] monthlyPay = computeMonthlyPay(employees);
    for(int j = 0; j < 9; j++){
    System.out.println(n[j] + " " + monthlyPay[j]);
    }
    }

    }
    public static double[] computeMonthlyPay(Employee[] employees){
    double[] monthlyPay = new double[9];
    for(int i = 0; i < 9; i++){
    switch(employees[i].employeeType){
    case'h':
    monthlyPay[i] = computePay(employees[i].parameter1, employees[i].parameter2);
    break;
    case's':
    monthlyPay[i] = computePay(employees[i].parameter1);

    break;
    case'm':
    monthlyPay[i] = computePay(employees[i].parameter1, employees[i].parameter2, 1);
    break;
    default:
    System.out.println("Invalid Employee Type");

    }
    }
    return monthlyPay;
    }
    }


    I'm sorry this is such a long post but I didn't know where else to go for help after asking my TA and my professor.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: NullPointerException Error trouble

    Please use code tags when posting code.

    Don't worry about the length of your post, it's nice to get a well structured question and lots of info. What you did forget was to post the full exception message and stack trace which helps us pin down the problem.

    Also the code you posted can't be the code you are running as it doesn't compile.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2012
    Posts
    3

    Re: NullPointerException Error trouble

    Thank you and sorry about that!
    here is the exception message:

    Exception in thread "main" java.lang.NullPointerException
    at EmployeeProgram.computeMonthlyPay(EmployeeProgram.java:38)
    at EmployeeProgram.main(EmployeeProgram.java:28)

    As for the code not compiling, i'm not exactly sure what I did before, but I thought I just copy pasted it in without changing anything. I compiled both just now and it worked so I'm not sure what I did before but I will paste the two classes in again.


    public class Employee{

    public String name;
    public String address;
    public char employeeType;
    public String ssn;
    public double monthlyPay;
    public double parameter1;
    public double parameter2;

    public Employee(String n, String a, char eT, String ss, double p1, double p2){
    this.name = n;
    this.address = a;
    this.employeeType = eT;
    this.ssn = ss;
    this.parameter1 = p1;
    this.parameter2 = p2;
    }
    public Employee()
    {}
    public Employee(String n, String a, char eT, String ss, double p1){
    this(n, a, eT, ss, p1, 0);
    }
    public Employee(double p1, double p2, String n, String a, char eT, String ss){
    this(n, a, eT, ss, p1, p2);
    }
    public static double computePay(double hourlyRate, double hoursWorked){

    double pay = hoursWorked * hourlyRate;

    return pay;
    }
    public static double computePay(double annualSalary){
    double pay = annualSalary/12;

    return pay;
    }
    public static double computePay(double regularSalary, double bonus, int x){
    double pay = bonus + (regularSalary/12);
    return pay;
    }

    }








    public class EmployeeProgram extends Employee{
    public static void main(String[] args){
    String[] n = {"Pete Za", "Pepe Roni", "Mary Nara", "Buff Alo", "Frank Pucinni", "Alfred Oh", "Michael Jackson", "Jack Johnson", "Beyonce"};
    String[] a ={"21 Italian ln", "42 Pizza st", "116 Shulze Hall", "14 Oak Street", "17 Safety Dance st", "81 Victory Lap", "42 Thriller St", "13 Hawaii Rd", "33 Single Ladies Ln"};
    char[] eT = {'h', 'h', 'h', 's', 's', 's', 'm', 'm', 'm'};
    String[] ss = {"131451834", "164278937", "893456723", "673567845", "44226633", "78562395", "23436512", "564782345", "45458749"};
    double[] p1 ={8.00, 8.00, 8.00, 53760, 53760, 53760, 67200, 67200, 67200};
    double[] p2 ={40, 40, 40, 0, 0, 0, 40, 40, 40};
    Employee [] employees = new Employee[9];

    for(int i = 0; i < 9; i++){

    switch(eT[i]){
    case'h':
    employees[i] = new Employee(n[i], a[i], eT[i], ss[i], p1[i], p2[i]);
    break;
    case's':
    employees[i] = new Employee(n[i], a[i], eT[i], ss[i], p1[i]);
    break;
    case'm':
    employees[i] = new Employee(p1[i], p2[i], n[i], a[i], eT[i], ss[i]);
    break;
    default:
    System.out.println("Invalid Employee Type");

    }
    double[] monthlyPay = computeMonthlyPay(employees);
    for(int j = 0; j < 9; j++){
    System.out.println(n[j] + " " + monthlyPay[j]);
    }
    }

    }
    public static double[] computeMonthlyPay(Employee[] employees){
    double[] monthlyPay = new double[9];
    for(int i = 0; i < 9; i++){
    switch(employees[i].employeeType){
    case'h':
    monthlyPay[i] = computePay(employees[i].parameter1, employees[i].parameter2);
    break;
    case's':
    monthlyPay[i] = computePay(employees[i].parameter1);

    break;
    case'm':
    monthlyPay[i] = computePay(employees[i].parameter1, employees[i].parameter2, 1);
    break;
    default:
    System.out.println("Invalid Employee Type");

    }
    }
    return monthlyPay;
    }
    }

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: NullPointerException Error trouble

    There are still no code tags around your code (See the blue text at the bottom of this post for how to use code tags).

    Sorry that was the correct code. The lack of code tags means it is hard to read and I missed something I only spotted when I copied the code into my IDE and re-formatted it.

    Your problem is you are calling the computeMonthlyPay method inside the loop which assigns Employee objects to the employees array so the first time through the array only contains 1 object and lots of null values.

    BTW when iterating over arrays don't use a hard coded value to specify the length of the array always use array's .length attribute ie
    Code:
    for(int i = 0; i < employees.length; i++){
    The main reasons that using .length is better is that you can't enter the wrong value by mistake and if you ever change the size of your array your code will still work without you having to search through it for everywhere you put a '9'.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Oct 2012
    Posts
    3

    Re: NullPointerException Error trouble

    I can't thank you enough for your help!
    I moved the method call outside of the loop and it works perfectly. I guess it was another case of me misplacing curly braces! I also took your advice and used the .length attribute. Thanks for the tip!

    As for the code tags, I apologize about that. I have never posted in a coding forum before and didn't know the concept. I will make sure to use those in the future.

    Once again thank you for your help!

Tags for this Thread

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center