java - Debugger stops working -


my program needs allow user input employee's name , total annual sales. when user finished adding employees array, program should determine employee had highest sales , had lowest sales. should print out difference between 2 numbers.

in code below, have totalpay class holds annual sales input user (it includes other variables , methods previous assignment not used here). salesperson class holds employee's name , totalpay object, includes annual sales. (i realize overcomplicated, i'm modifying previous assignment rather starting scratch.)

when run code, allows me enter name , sales, when enter "yes or no" add employee, crashes , tells me there nullpointerexception on line 58, noted in code.

i've ran debugger (without breakpoints) , stops @ line 46, noted in code. it doesn't give error message, doesn't update variable , "step into" buttons debugger grey out , can't click them anymore. (i'm using netbeans, if that's relevant.)

any ideas appreciated!

edit: here output , error message.

name? captain america

input annual sales: 80

add employee? yes or no

no

exception in thread "main" java.lang.nullpointerexception @ 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 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 name if no continue total commision         //enter in sales amount of commistion         system.out.print("input annual sales: ");           s=keyboard.nextdouble();         emps[i].pay.annual = s;          system.out.println("add 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 maximum number of employees.");             cont = "no";         }      }       i=0;      (i=0; i<emps.length; i++){          if (emps[i].pay.annual > high.pay.annual) //line 58: claims error 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 earn "+diff+" more match employee "+high.getname());      // output table composation increments of $5000 //        int tempannual =(int) pay.annual; //        (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 sale        double annual; //sales inputted        double reward; // yearly pay bonus        double bonusrate2= bonusrate1 + 1.15 ; // sales target starts @ 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;     }       }    } 

you create array of max size 10:

salesperson[] emps = new salesperson[10]; 

but create , assign object reference each salesperson object entered. since enter 1 name, 1st entry in array valid, remaining 9 null. attempt iterate through entire array (emps.length 10 ):

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

which leads npe when indexing first null reference. need change loop like:

  int numentered = i;  //last increment        (i=0; i< numenetered; i++){              if (emps[i].pay.annual > high.pay.annual)  

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -