0

the instructions I was given are as follows: Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a double annual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields.

Write an application named DemoSalesperson that declares an array of 10 Salesperson objects.

Set each ID number to 9999 and each sales value to zero.

Display the 10 Salesperson objects. Save the files as Salesperson.java and DemoSalesperson.java.

my code so far is as follows:

public class Salesperson{
private int idNumber;
private double salesAmount;

    public Salesperson(int idNum, double salesAmnt){
        idNumber = idNum;
        salesAmount = salesAmnt;
    }

    public int getIdNumber(){
        return idNumber;
    }

    public void setIdNumber(int idNum){
        idNumber = idNum;
    }

    public double getSalesAmount(){
        return salesAmount;
    }

    public void setSalesAmount(double salesAmnt){
        salesAmount = salesAmnt;
    }
}

this much, I believe, is correct. The issues I am having (I believe) is in the program that uses this info:

public class SalespersonDemo{
public static void main(String[] args){
    final int NUM_PERSON = 10;
    final double TOTAL_SALES = 10;
    int x = 0;
    double y = 0.0;
    Salesperson[] id = new Salesperson[NUM_PERSON];
    Salesperson saleperson = new Salesperson(x, y);

    getSalesAssociateInfo(id);

    for(x = 0; x < NUM_PERSON; ++x){
        System.out.println("Amount sold by associate " + id[x].getIdNumber() + " :");
        for(y = 0; y < TOTAL_SALES; ++y)
            System.out.print(id[x].getSalesAmount());
            System.out.println();
    }

}
public static void getSalesAssociateInfo(Salesperson[] id){
    final int NUM_PERSON = 10;
    final double TOTAL_SALES = 10;
    int x = 9999;
    double y = 0;}
} 

I'm not sure what I did wrong, but when compiled the program gives me an error that says "Exception in thread "main" java.lang.NullPointerException at SalespersonDemo.main(SalespersonDemo.java:13)."

I would be very thankful if anyone has any suggestions on what I did wrong and how to fix it.

I tried to write this program in a way that I could easily go back and edit the SalespersonDemo at a later date in order to finish this project, as the second part requires that I assign ID numbers to each Salesperson with numbers 111-120, and the sales amount being between the numbers 20,000-70,000, increasing by 5,000 for each successive salesperson. If you could give me tips on completing that part of the problem, I would greatly appreciate it. Edit: I tried changing the getSalesAssociateInfo method in an attempt to assign a specific number to my id array. This is what I have right now:

    public static void getSalesAssociateInfo(Salesperson[] id){
    final int NUM_PERSON = 10;
    final double TOTAL_SALES_AMOUNT = 10;
    int x ;
    double y;
    for(x = 0; x < NUM_PERSON; ++x){
        id[x] = new Salesperson(9999, 0);
        id[x].setIdNumber(x);
        for(y = 0; y < TOTAL_SALES_AMOUNT; ++y)
            id[x].setSalesAmount(y);
    }
}

the result is that I no longer get the null pointer exception error, but the salespersons do not show up as "9999" and the amount sold is a ridiculous looking "09.09.09.09.09..." I'm starting to feel real silly at this point.

Edit #2: okay, a little more fiddling with the program and I got the results that I wanted, but I am not sure how sound the logic in my code is. Any tips would be appreciated, but here is the code:

public class SalespersonDemo{
public static void main(String[] args){
    final int NUM_PERSON = 10;
    final int SALES_AMOUNT= 1;
    double totalSales = 0;
    int x = 0;
    double y = 0.0;
    Salesperson[] id = new Salesperson[NUM_PERSON];
    Salesperson saleperson = new Salesperson(x, y);

    getSalesAssociateInfo(id);

    for(x = 0; x < NUM_PERSON; ++x){
        System.out.println("Amount sold by associate " + id[x].getIdNumber() + ":");
        for(y = 0; y < SALES_AMOUNT; ++y)
            System.out.print(id[x].getSalesAmount());
            System.out.println();

    }

}
    public static void getSalesAssociateInfo(Salesperson[] id){
        final int NUM_PERSON = 10;
        final int SALES_AMOUNT = 0;
        double totalSales = 1;
        int x = 0;
        double y = 0.0;
        for(x = 0; x < NUM_PERSON; ++x){
            id[x] = new Salesperson(x, y);
            id[x].setIdNumber(9999);
            for(y = 0; y < SALES_AMOUNT ; ++y)
                totalSales = 0;
                id[x].setSalesAmount(y);

       }
   }
}

again, I would like to apologize if my questions are not clear, or if I come across as a "noob" (which I am very much an amateur). I will read all of the thread that are linked in this post and will make adjustments to the code. When I am satisfied with the results, I will answer the question myself if a suitable answer does not exist by the time I have one. I still have to figure out how to set the ID#'s to start at 111 and end at 120, and the sales values have to start at 25,000 and increase by 5,000 for each salesperson.

Edit #3: here is my final code. I would appreciate some feedback. Anyway, thanks to everyone who assisted with this problem.

public class DemoSalesperson2{
public static void main(String[] args){
    final int NUM_PERSON = 10;
    final int SALES_AMOUNT= 1;
    int x = 0;
    double y = 0;
    Salesperson[] id = new Salesperson[NUM_PERSON];
    Salesperson saleperson = new Salesperson(x, y);

    getSalesAssociateInfo(id);

    for(x = 0; x < NUM_PERSON; ++x){
        System.out.println("Amount sold by associate " + id[x].getIdNumber() + ":");
        for(y = 0; y < SALES_AMOUNT; ++y)
            System.out.print(id[x].getSalesAmount());
            System.out.println();

    }

}
public static void getSalesAssociateInfo(Salesperson[] id){
    final int NUM_PERSON = 10;
    final int SALES_AMOUNT = 1;
    int ident[] = {111, 112, 113, 114, 115, 116, 117, 118, 119, 120};
    double amnt[] = {25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000, 65000, 70000};
    int x;
    double y = 0;
        for(x = 0; x < NUM_PERSON; ++x){
            id[x] = new Salesperson(x, y); 
            id[x].setIdNumber(ident[x]);
            for(y = 0; y < SALES_AMOUNT ; ++y)
                id[x].setSalesAmount(amnt[x]);

        }
    }
}
  • You'd never initialize `id` array – Michele d'Amico Nov 13 '15 at 13:01
  • in my attempt to initialize the id array, I changed the getSalesAssociateInfo method. I'm not quite sure if I'm on the right track, but it actually runs the program now. But you guys are right, as the salepeople show up as 0-9, when they all need to be 9999 I will edit my question to show my code – Roberson Terry Nov 13 '15 at 14:46

1 Answers1

0

You never added any Salesperson to Salesperson[] id = new Salesperson[NUM_PERSON]; array ,all it has are 10 null elements.

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
  • read : http://stackoverflow.com/questions/3426843/what-is-the-default-initialization-of-an-array-in-java – Ramanlfc Nov 13 '15 at 12:33