0

I'm trying to start learning Java in BlueJ, but I'm struggling quite a lot. At the moment I'm looking at ArrayLists. The following code compiles correctly:

import java.util.ArrayList;

public class TestArrayList
{
    public static int colorCode;
    public ArrayList<String> selection;

    public TestArrayList()
    {
    }

    private void selection()
    {
        selection = new ArrayList<String>();
        {
            selection.add("red");
            selection.add("yellow");
            selection.add("blue");
        }
    }

    public void selectColor(int colorCode)
    {
        if (colorCode == 1)
        System.out.println("You have selected " + selection.get(0) + ", the color of fire!");
        if (colorCode == 2)
        System.out.println("You have selected " + selection.get(1) + ", the color of electricity!");
        if (colorCode == 3)
        System.out.println("You have selected " + selection.get(2) + ", the color of water!");
        else
        System.out.println("Invalid selection");
    }
}

But when I try to run the method "selection", and I type 1, 2, or, 3, in the popup window, I get errors. All other values, like 0, 4, 5, 6 work fine. But for 1, 2, and 3, the terminal window displays the following error:

java.lang.NullPointerException
    at TestArrayList.selectColor(TestArrayList.java:25)

And the editor highlights the line containing the code "selection.get(0)", and returns the error "java.lang.Null.Pointer Exception: null".

I'm sure you can tell from the way I'm talking that I know very little about Java. Is it obvious what I'm doing wrong? Thanks to anyone who can help.

Fang
  • 2,199
  • 4
  • 23
  • 44
Nick Giordano
  • 31
  • 1
  • 6
  • 1
    *"But when I try to run the method "selection""* This is not true, you're calling `selectColor`. – Tom Nov 19 '16 at 12:59

1 Answers1

3

Your method selection is not called. Thus your ArrayList is null. Calling a method on a null reference causes a NullPointerException.

Invoke the method in your constructor.

public TestArrayList()
{
   selection();
}
Fang
  • 2,199
  • 4
  • 23
  • 44
Marcinek
  • 2,144
  • 1
  • 19
  • 25
  • Thanks for the help, everyone! That fixed it. I'm gonna be honest; I don't totally understand why I need to call the method. Also, does it matter if I put the method call in the Constructor, or within the selectColor method? – Nick Giordano Nov 19 '16 at 13:32
  • I have a new problem. If you able to help again that'd be great. The error message from before is now gone. However, now, when I enter 1, 2, or 3, it returns my "Fire" text for example, in one line, but then it also returns "Invalid selection" in the following line. Why does it return that second line? Thanks – Nick Giordano Nov 19 '16 at 13:35
  • @NickGiordano The last `if ... else ...` statement means: if `colorCode != 3` execute the code inside `else`. I would suggest to use [switch statement](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html) which should solve it. – lenny Nov 25 '16 at 17:05