0
 public void FillArray()
{
    for (int i = 1; i < numEmp; i++)
    {
        employees[i] = true;
    }
}

I think this part may be the reason I'm getting the NullPointerException error, but I don't know what I'm missing. I have put my full code for the program below just in case there is something that is wrong with that that is giving me that error message.

//client class

public class Downsize
{
    public static void main (String [] args)
    {
        System.out.print("Do you want to downsize the company? (Y/N): ");
        String dummy = APIO.getString().toUpperCase();        
        while (dummy.equals("Y"))
        {
            Employee employee = new Employee();
            System.out.print("Do you want to downsize the company? (Y/N): 
            dummy = APIO.getString().toUpperCase();
        }
    }
}  

//object class

 public class Employee
    {
        int numEmp;
        int sprayer;
        int winner;
        boolean [] employees;

        public Employee()
        {
            System.out.print("How many employees? (0 to end): ");
            int numEmp = APIO.getInt();
            System.out.print("Who gets the spray can first?: ");
            int sprayer = APIO.getInt();
            FillArray();
            Selection();
            Winner();
        }

        public void FillArray()
        {
            for (int i = 1; i < numEmp; i++)
            {
                employees[i] = true;
            }
        }

        public void Selection()
        {        
            System.out.println("EM="); //debugging method
            for (boolean em: employees)
            {
                System.out.println(em);
            }

            int complete = numEmp;
            while (complete > 1)
            {
                System.out.print("spraycan passed to #" + sprayer);
                if ((sprayer + 1) > numEmp)
                {
                    sprayer = 0;
                }
                while (employees[sprayer + 1] == false)
                {
                    sprayer++;
                    if (sprayer >= numEmp)
                    {
                        sprayer = 0;
                    }
                }
                employees[sprayer + 1] = false;
                System.out.print("  -  sprays #" + (sprayer + 1) + "'s hair");
                complete--;
                sprayer++;
                while (employees[sprayer] == false)
                {
                    sprayer++;
                    if (sprayer > numEmp)
                    {
                        sprayer = 1;
                    }
                }
            }
        }

        public void Winner()
        {
            if (sprayer == 0)
            {
                sprayer = 1;
                System.out.print("\nThe Winner is #" + sprayer);
                System.out.print("\n");
            }
        }
    }

//stack trace for the error message

java.lang.NullPointerException
    at Employee.FillArray(Employee.java:22)
    at Employee.<init>(Employee.java:14)
    at Downsize.main(Downsize.java:9)
Emma Dillon
  • 11
  • 1
  • 5
  • Perhaps you could show the stack trace to see where NPE occurs (and indicate which source line the line number corresponds to) – DBug Dec 04 '15 at 23:12
  • I'm not sure what exactly you mean by showing the stack trace – Emma Dillon Dec 04 '15 at 23:15
  • 2
    Your array is not instancied. Meaning the `employees` variable only contains a null pointer, hence the exception when you try to access `employess[i]`. – Kilazur Dec 04 '15 at 23:16
  • so how exactly would i instantiate the array using a boolean? – Emma Dillon Dec 04 '15 at 23:17
  • `employees = new boolean[numEmp];` – Andy Turner Dec 04 '15 at 23:17
  • The stack trace is the multi-line message that tells you that you had a Null Pointer Exception. Near the top of the stack trace, it will tell you which Java file and which line number the exception occurred on. Start looking there. – Dawood ibn Kareem Dec 04 '15 at 23:23
  • @EmmaDillon the stack trace is what is printed out when the exception occurs. It starts with the type of exception (java.lang.NullPointerException), has a message, and then shows the lines of code/method calls being executed when the exception occurs. It generally provides very useful information about what you are doing when the exception occurs; you should always provide it if you are asking about an exception. – Andy Turner Dec 04 '15 at 23:23
  • i'm still getting the error – Emma Dillon Dec 04 '15 at 23:24
  • Please provide your stack trace :) Edit your question and add it. Please also edit your code to show how/where you instantiate `employees`. – Andy Turner Dec 04 '15 at 23:25
  • I added it into the question and where I instantiate employees is the first part of the question where i mention that I believe that is where the problem is – Emma Dillon Dec 04 '15 at 23:27
  • Please can you add the actual code, just so there is no confusion or ambiguity? – Andy Turner Dec 04 '15 at 23:29
  • all of the code I have is presented in the question @Andy Turner – Emma Dillon Dec 04 '15 at 23:31
  • Your presented code does not contain `employees =`, so you are not currently instantiating the array. – Andy Turner Dec 04 '15 at 23:32

2 Answers2

1

You must initialize the employees array. Once you read in the number of employees, do employees = new boolean[numEmp]; (after the line int numEmp = APIO.getInt();). Otherwise, it is null, so trying to access employees[i] throws a NullPointerException.

mapeters
  • 1,067
  • 7
  • 11
1

I see that you guys are beating around the bush in @mook's answer so let me just clarify so the OP can understand it well instead of guessing.

class Example {

    int size;
    boolean[] array;

    void initArray() {

        size = 5;
        array = new boolean[size];
    }
}

After declaring the size of the array and its type, you need to initialize the array. In the above example array holds 5 booleans.

If instead you write

class Example {

    int size;
    boolean[] array = new boolean[size];;

    void initArray() {

        size = 5;
    }
}

Then since int is set to 0 by default, the size of array will be 0 even if you change the variable you used to declare the size later in the code. This will give you an error since you will iterate up to 5.

@mook's answer is correct.

user1803551
  • 12,965
  • 5
  • 47
  • 74
  • I understand what you are saying, so how do I instantiate the array so it is empty until i fill it with a boolean? – Emma Dillon Dec 05 '15 at 00:01
  • @EmmaDillon The variables in the array are not initialized when the array is initialized! It's very important to remember that. For primitives (like boolean), there are default values which are used. For objects, they are all set to null. If you want to initialize the objects in an array (or change the primitives default value), you have to iterate over the array and initialize each one. – user1803551 Dec 05 '15 at 00:04
  • I got it now! thank you so much for making it clearer – Emma Dillon Dec 05 '15 at 00:05
  • @EmmaDillon BTW, the default value for a boolean in Java is `false`, so my array in the first example holds 5 booleans each with a value of `false` when initialized. Arrays are never "empty", they always hold something (null is also something for this point). – user1803551 Dec 05 '15 at 00:07
  • okay. thanks for the tips. my program works now. thank you so much – Emma Dillon Dec 05 '15 at 00:08
  • 1
    @EmmaDillon Good job. You will want to accept an answer (the green checkmark) to mark the question as answered. (mook's answer is correct and he was first to answer, so it might be fair to choose his, albeit less detailed.) You can also upvote helpful answers regardless. – user1803551 Dec 05 '15 at 00:11