我的程序需要允许用户输入员工姓名和年度总销售额。当用户完成向阵列添加员工时,程序应确定哪个员工的销售额最高,哪个员工的销售额最低。然后它应该打印出两个数字之间的差异。
在我的下面的代码中,我有一个totalPay类,用于保存用户的年度销售输入(它包括此前未使用的先前任务中的其他变量和方法)。 salesPerson类包含员工姓名和totalPay对象,其中包括年度销售额。 (我意识到这是过于复杂的,但我正在修改我之前的任务,而不是从头开始。)
当我运行此代码时,它允许我输入名称和销售额,但当我输入“是或否”添加另一名员工时,它会崩溃并告诉我第58行有一个NullPointerException,在代码中注明
我已经运行了调试器(没有任何断点),它只是停在第46行,在代码中注明。 它没有给出错误消息,它只是不更新该变量,我的“步入”按钮使调试器灰显,我不能再点击它们了。(我正在使用NetBeans,如果这是相关的。)
任何想法都会非常感激!
编辑:这是输出和错误消息。
名称?美国队长
输入年销售额:80
添加另一名员工?是或否
没有
在Commission.Commission.main(Commission.java:58)中的线程“main”java.lang.NullPointerException中的异常
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;
}
}
}
答案 0 :(得分:1)
您创建最大尺寸为10的数组:
salesPerson[] emps = new salesPerson[10];
但仅为输入的每个SalesPerson对象创建和分配对象引用。由于您只输入1个名称,因此只有数组中的第1个条目有效,其余9个为空。然后,您尝试遍历整个数组(emps.length为10):
for (i=0; i<emps.length; i++){
if (emps[i].pay.annual > high.pay.annual)
在索引第一个空引用时会导致NPE。您需要将循环更改为:
int numEntered = i; //last increment
for (i=0; i< numEnetered; i++){
if (emps[i].pay.annual > high.pay.annual)
答案 1 :(得分:0)
它会停止调试器,因为它会使用键盘等待您的输入。如果您输入输入并按Enter键,调试器将从那里继续。
顺便说一下,您应该阅读有关java的命名约定和编码最佳实践
答案 2 :(得分:0)
您的调试器已停止,因为它在来自扫描仪的输入中被阻止。 This is specified in the documentation:
查找并返回此扫描仪的下一个完整令牌。在完成令牌之前和之后是与分隔符模式匹配的输入。即使先前的
hasNext()
调用返回true,此方法也可能在等待输入扫描时阻塞。
除此之外,您很幸运能够完全进入该代码块。你comparing Strings incorrectly,所以一眼就看出你不会进入那个循环,除非在某些特殊情况下。这也是你的NPE发生的原因;你在虚假借口(==
带有字符串)下初始化数组元素,所以:
if (cont =="yes")
)我只是超过了几个高点,但在大多数情况下,阻塞IO是调试器停止的原因。一旦开始使用.equals
,其他错误可能会变得更容易看到,但我鼓励您与同学,导师或TA进行面对面的代码审查。这里对你的代码存在很多误解,这将使以后调试或修复变得更加困难。