0

I am writing a piece of software that requires the use of a class in another file. When I run the program and put in a value, I get the error Exception in thread "main" java.lang.NullPointerException class. Here is my code:

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!");
    }

}

Here is my class file 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;
} 
}

Any help is greatly appreciated.

  • The question I linked to will explain what an NPE is, and more importantly, how you diagnose the problem by reading the stacktrace and some logical deduction. – Stephen C Dec 05 '14 at 01:42

1 Answers1

0

Add peep[i] = new Salesperson() before peep[i].setEmpID

ochakov
  • 51
  • 4