线程" main"中的例外情况java.lang.NullPointerException类

时间:2014-12-05 01:02:17

标签: nullpointerexception

我正在编写一个需要在另一个文件中使用类的软件。当我运行程序并输入一个值时,我在线程“main”java.lang.NullPointerException类中得到错误Exception。 这是我的代码:

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;
} 
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在窥视[i] .setEmpID

之前添加peep [i] = new Salesperson()