Leo
Leo

Reputation: 1

Debugger stops working

My program needs to allow the user to input an employee's name and total annual sales. When the user is finished adding employees to the array, the program should determine which employee had the highest sales and which had the lowest sales. It should then print out the difference between the two numbers.

In my code below, I have a totalPay class that holds the annual sales input by the user (it includes other variables and methods from a previous assignment that are not used here). The salesPerson class holds the employee's name and totalPay object, which includes their annual sales. (I realize this is overcomplicated, but I'm modifying my previous assignment rather than starting from scratch.)

When I run this code, it allows me to enter the name and sales, but when I enter "yes or no" to add another employee, it crashes and tells me there is a NullPointerException on line 58, noted in the code.

I've ran the debugger (without any breakpoints) and it just stops at line 46, noted in the code. It doesn't give an error message, it just doesn't update that variable and my "step into" buttons for the debugger grey out and I can't click them anymore. (I'm using NetBeans, if that's relevant.)

Any ideas would be much appreciated!

EDIT: Here is the output and error message.

Name? captain America

Input annual sales: 80

Add another employee? yes or no

no

Exception in thread "main" java.lang.NullPointerException at commission.Commission.main(Commission.java:58)

package commission;
//Commicaion calulator


import java.util.Scanner;


public class Commission 
{
    public static void main(String args []) 
     {   
     salesPerson[] emps = new salesPerson[10]; //Employee Array
     String cont = "yes";
     String n="";
     double s=0;
     int i=0;
     salesPerson high = new salesPerson();
     salesPerson low = new salesPerson();

      // scanner object for input
        Scanner keyboard = new Scanner(System.in);

      //Enter in employee name 
     while (cont == "yes"){
        System.out.print("Name? ");
        n = keyboard.nextLine();
        emps[i] = new salesPerson();
        emps[i].setName(n);

         //Loop of yes or no entering more employees
        //If yes add another name if no continue with total Commision
        //Enter in the sales amount of commistion
        System.out.print("Input annual sales: ");  
        s=keyboard.nextDouble();
        emps[i].pay.annual = s;

        System.out.println("Add another employee? yes or no ");
        keyboard.nextLine();
        cont = keyboard.next(); //Line 46: Debugger stops here.

        if (cont =="yes")
            i++;
        if (i==9){
            System.out.println("You have reached the maximum number of employees.");
            cont = "no";
        }
     }

     i=0;
     for (i=0; i<emps.length; i++){
         if (emps[i].pay.annual > high.pay.annual) //Line 58: It claims the error is here.
             high = emps[i];

         if (emps[i].pay.annual < low.pay.annual)
             low = emps[i];
     }

     double diff = high.pay.annual - low.pay.annual;
     System.out.println("Employee "+low.getName()+" needs to earn "+diff+" more to match Employee "+high.getName());

    // Output table for composation with increments of $5000
//        int tempAnnual =(int) pay.annual;
//        for (i=tempAnnual; i<= pay.annual; i+=5000)
//            System.out.println(i+"   "+ pay.getReward(i));   
    }


  public static class totalPay
  {

      double salary=50000.0; //Yearly earned 50000 yr fixed income

      double bonusRate1=.05; //bounus commission rate of 5% per sale

      double commission; //Commission earned after a sale

      double annual; //Sales inputted

      double reward; // Yearly pay with bonus

      double bonusRate2= bonusRate1 + 1.15 ; // Sales target starts at 80%

    public double getReward(double annual)
    {
       double rate;
       if (annual < 80000) 
        rate=0;
       else if ((annual >= 80000) || (annual < 100000 ))
        rate=bonusRate1;
       else 
         rate=bonusRate2;

      commission = annual * rate;

      reward=salary + commission; 

      return reward;
    }

  }

  public static class salesPerson
      {
    String name; //Employee Name
    totalPay pay = new totalPay();

    public void setName(String n) //Name
    {      
     name=n;         
      }
    public String getName()
    {
        return name;
    }
      }

  }

Upvotes: 0

Views: 65

Answers (3)

Makoto
Makoto

Reputation: 106430

Your debugger is stopped because it's blocked on input coming in from the Scanner. This is specified in the documentation:

Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.

That aside, you're fortunate to have entered that code block at all. You're comparing Strings incorrectly, so at a glance it'd look like you wouldn't enter that loop except under certain special circumstances. This is also the reason that your NPE occurs; you're initializing elements of your array under false pretenses (== with a String), so:

  • You may never initialize anything
  • You may only initialize the first thing (if (cont =="yes"))

I've only gone over a few of the high points, but for the most part, the blocking IO is why your debugger has stopped. The other errors may become easier to see once you start using .equals, but I'd encourage you to get an in-person code review with a classmate, tutor, or TA. There are a lot of misconceptions strewn about your code here which will make it harder to debug or fix later.

Upvotes: 0

kaykay
kaykay

Reputation: 556

It stops the debugger because it waits for your input using the keyboard. If you type the input and hit enter, the debugger will continue from there on.

By the way, your should read up on naming conventions and coding best practices for java

Upvotes: 0

OldProgrammer
OldProgrammer

Reputation: 12169

You create this array of max size 10:

salesPerson[] emps = new salesPerson[10];

but only create and assign an object reference for each SalesPerson object entered. Since you only enter 1 name, only the 1st entry in the array is valid, then remaining 9 are null. You then attempt to iterate through the entire array (emps.length is 10 ):

  for (i=0; i<emps.length; i++){
         if (emps[i].pay.annual > high.pay.annual) 

which leads to the NPE when indexing the first null reference. You need to change your loop to something like:

  int numEntered = i;  //last increment

      for (i=0; i< numEnetered; i++){
             if (emps[i].pay.annual > high.pay.annual) 

Upvotes: 1

Related Questions