-1

I'm trying to insert a key of type String and a value of type LinkedList (which itself is of type Sprite) into a TreeMap. However, when my code calls the put() method, it throws a null pointer exception and I cannot figure out why.

My entire class can be found here. But for brevity, I've narrowed it down to the problem parts below.

Here is my Treemap declaration (I'm using Java 7, so it likes the diamond.

TreeMap<String, LinkedList<Sprite>> paneSprites = new TreeMap<>();

Here is part of my method (the rest does the same thing with other sprites and commenting out the put() method here just throws another exception later on at another put() method.

public void initGUIControls()
{
    canvas = new RambleOnPanel(this, (RambleOnDataModel)data);
    LinkedList<Sprite> spriteList = new LinkedList<>();

    //Sprite for Welcome Pane
    SpriteType st = new SpriteType(settings.WELCOME_BACKGROUND_TYPE);
    BufferedImage img = loadImage(settings.WELCOME_IMAGE_PATH);
    st.addState(settings.DEFAULT_STATE, img);
    st.addState(settings.INVISIBLE_STATE, img);
    int x = 0;
    int y = 0;
    int vX = 0;
    int vY = 0;
    Sprite s = new Sprite(st,x,y,vX,vY, settings.DEFAULT_STATE);
    spriteList.add(s);
    guiButtons.put(st.getSpriteTypeID(), s);

    //put sprite types for this pane in the hash map and clear the list
    getPaneSprites().put(settings.WELCOME_PANE_TYPE, spriteList);  //EXCEPTION THROWN HERE
    spriteList.clear();

    //Sprites for Select Account Pane

Also, all Strings used here are instantiated in another class as follows

package ramble_on;

public class Settings
{
//Game Settings
public final String APP_TITLE = "Ramble On!";
public final int FRAME_RATE = 30;
public final int GAME_HEIGHT = 700;
public final int GAME_WIDTH = 1200;

//Sprite States
public final String INVISIBLE_STATE = "INVISIBLE STATE";
public final String  DEFAULT_STATE = "DEFAULT STATE";

//Pane Types
public final String WELCOME_PANE_TYPE = "WELCOME PANE TYPE";
public final String SELECT_ACCOUNT_PANE_TYPE = "SELECT ACCOUNT PANE TYPE";
public final String CURRENT_ACCOUNT_PANE_TYPE = "CUTTENT ACCOUNT PANE TYPE";

//Sprite Types
public final String WELCOME_BACKGROUND_TYPE = "WELCOME BACKGROUND TYPE";
public final String SELECT_ACCOUNT_BACKGROUND_TYPE = "SELECT ACCOUNT TYPE";
public final String NEW_USER_BUTTON_TYPE = "NEW USER BUTTON TYPE";
public final String CURRENT_ACCOUNT_BACKGROUND_TYPE = "CURRENT ACCOUNT BACKGROUND TYPE";
public final String LARGE_RETURN_BUTTON_TYPE = "LARGE RETURN BUTTON TYPE";

//Sprite Image paths
public   final String IMAGES_PATH = "./data/images/";

public final String WELCOME_IMAGE_PATH = IMAGES_PATH + "welcome.png";
public final String SELECT_ACCOUNT_BACKGROUND_IMAGE_PATH = IMAGES_PATH + "selectAccount.png";
public final String NEW_USER_BUTTON_PATH = IMAGES_PATH + "newUser.png";
public final String Current_Account_SCREEN_BACKGROUND_IMAGE_PATH = IMAGES_PATH + "currentAccount.png";
public final String LARGE_RETURN_BUTTON_IMAGE_PATH = IMAGES_PATH + "newUser.png";

}
audiFanatic
  • 2,296
  • 8
  • 40
  • 56

1 Answers1

1

You have two options for a nullpointerexception in the line

getPaneSprites().put(settings.WELCOME_PANE_TYPE, spriteList);

  1. getPaneSprited() returns null
  2. settings.WELCOME_PANE_TYPE is null

To see if it's 1 you can split the get call and the put into two different lines. Then debug on if the getter is returning null.

Treemap keys may not be null, so that could be it as well. It has nothing to do with the generics because once it's compiled the generics are gone anyway due to type erasure.

ryber
  • 4,537
  • 2
  • 26
  • 50
  • It seems to be the first option, however, I'm not sure why. I declared it as a new TreeMap in the declarative part of the class. I'll edit my post and add my entire class in the question. – audiFanatic Apr 14 '13 at 17:20
  • http://pastebin.com/0M7ZJy6e – audiFanatic Apr 14 '13 at 17:23
  • I don't know why, but I initialized the TreeMap at the top of my method rather than in the declarative part along with the LinkedList and it fixed it! Thanks! – audiFanatic Apr 14 '13 at 19:21