gpt4 book ai didi

java - 调试器停止工作

转载 作者:行者123 更新时间:2023-12-01 11:33:15 25 4
gpt4 key购买 nike

我的程序需要允许用户输入员工姓名和年销售额总额。当用户完成将员工添加到数组后,程序应该确定哪个员工的销售额最高,哪个员工的销售额最低。然后它应该打印出两个数字之间的差异。

在下面的代码中,我有一个 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;
}
}

}

最佳答案

您创建最大大小为 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)

关于java - 调试器停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com