0
我在写一篇软件,需要在另一个文件中使用一个类。当我运行程序并放入一个值时,我在线程“main”java.lang.NullPointerException类中得到错误Exception。 这里是我的代码:线程“main”中的异常java.lang.NullPointerException类
package chap8lab.salesperson;
import javax.swing.JOptionPane;
public class Chap8LabSalesPerson {
public static void main(String[] args) {
final int LIMIT = 10;
Salesperson[] peep = new Salesperson[LIMIT];
int i = 0, a = 0;
String q = "q", strInput = "";
JOptionPane.showMessageDialog(null,
"This program collects salesperson data.");
while (!strInput.equals(q))
{
strInput = JOptionPane.showInputDialog(null,
"Enter sales person ID (\"q\" to quit)");
if (!strInput.equals(q) && !strInput.matches("[a-z][A-Z]"))
peep[i].setEmpID(Integer.parseInt(strInput));
if (!strInput.equals(q))
strInput = JOptionPane.showInputDialog(null,
"Enter sales amount (\"q\" to quit)");
if (!strInput.equals(q) && !strInput.matches("[a-z][A-Z]"))
peep[i].setSalesAmt(Double.parseDouble(strInput));
i++;
if (strInput.equals(q))
i--;
}
while (a < i)
{
JOptionPane.showMessageDialog(null,
"Employee ID: " + peep[a].getEmpID()
+ " Sales Ammount: " + peep[a].getSalesAmt() + "\n");
a++;
}
JOptionPane.showMessageDialog(null,
"Thank you for using this program.\nHave a nice day!");
}
}
这里是我的类文件Salesperson.java:
package chap8lab.salesperson;
public class Salesperson {
private int empID;
private double salesAmt;
public Salesperson()
{
empID = 9999;
salesAmt = 0;
}
public void setEmpID(int newEmpID)
{
empID = newEmpID;
}
public int getEmpID()
{
return empID;
}
public void setSalesAmt(double newSalesAmt)
{
salesAmt = newSalesAmt;
}
public double getSalesAmt()
{
return salesAmt;
}
}
任何帮助是极大的赞赏。
我挂将解释的NPE是什么,更重要的是,如何通过阅读堆栈跟踪诊断问题的问题,一些合乎逻辑的推论。 – 2014-12-05 01:42:05