Arrays within Objects within ArrayLists... How to Call Array Elements?

edited November 2017 in Questions about Code

Greetings! I started programming a couple weeks ago, watching Daniel Shiffman's videos and messing around with the 3.0 editor; it's so much fun! Usually I can troubleshoot my way out of my mistakes within a few hours. But I've not been so lucky lately! Hence my post...

I've been building parts of a storytelling game, and I've been trying out models for managing non-player character data. Right now I have an ArrayList of "NPC" class objects; it is called "knownNpcs". NPC objects each have an array called "cond". I don't get an error when I write data to the "cond" array elements, but I can't seem to call the elements later. Here is the "NPC" object constructor code, initializing the "cond" array, which runs without error...

int [] cond = new int [3]; cond[0]=TcondA; cond[1]=TcondB; cond[2]=TcondC;

TcondA etc are arguments passed through when the constructor is called. But when I try to call these same elements, I get a "NullPointerException" error... Here's how I'm trying to call the "cond" array element 0 from the "NPC" object which is element 0 of the "knownNpcs" ArrayList:

knownNpcs.get(0).cond[0] (this is just the call, not a full line of code)

Is my syntax wrong? How do I call elements within arrays within objects within ArrayLists?

I really appreciate any help you can give me! Thank you in advance!

Answers

  • edited November 2017

    You are doing everything right; what has tripped you up is a quirk of ArrayLists: They do not know the type of object they are storing unless you are very specific and tell them it.

    I'm guessing that when you define the knownNPC Arraylist, you have a line of code like this:

    ArrayList knownNPCs = new ArrayList();
    

    What you need to do is tell the ArrayList that it is only for storing NPC classes. Like so:

    ArrayList<NPC> knownNPCs = new ArrayList().
    

    This will ensure that when this ArrayList returns an object (from it's get() function) - it will be of the right type.

    You could also fix this by specifying the type of thing you expect the ArrayList to give you when you do your call:

    ((NPC) knownNPCs.get(0)).cond[0];
    

    Or:

    NPC anNPC = knownNPCs.get(0);
    anNPC.cond[0];
    

    Hopefully that should resolve your problems. If not, can we please see the code that defines your ArrayList and NPC class?

  • Thanks for your reply TfGuy44! Unfortunately your suggestion didn't fix the problem for me. I was already declaring the ArrayList as follows:

    ArrayList<NPC> knownNpcs = new ArrayList<NPC>();

    So I tried your second suggestion, by calling as:

    (NPC)knownNpcs.get(0)).cond[0]

    No luck! The NPC class and constructor is:

    class NPC {
    
      String name; //name of NPC
      String desc; //short description of NPC
      String type; //type of NPC (animal, human)
      int [] nhex; //array of character hex values
      int [] cond; //array of character condition values
      int follow; //0 or 1 to denote character following player
      int hunt; //0 or 1 to denote character hunting the player
      String action;
      String method;
      String relate;
    
      NPC(String Ttype, String Tname, String Tdesc, int TcondA, int TcondB, int TcondC, int TnhexA, int TnhexB, int TnhexC, int Tfollow, int Thunt)  { //NPC constructor
        type = Ttype;
        name = Tname;
        desc = Tdesc;
    
        int [] cond = new int [3];
        cond[0]=TcondA;
        cond[1]=TcondB;
        cond[2]=TcondC;
    
        int [] nhex = new int [3];
        nhex[0]=TnhexA;
        nhex[1]=TnhexB;
        nhex[2]=TnhexC;
    
        follow = Tfollow;
        hunt = Thunt;
    
        action = " ";
        method = " ";
        relate = " ";
      }
    

    I ran into an issue calling boolean variables from objects within the ArrayList, maybe that's a clue? Hence the int follow and int hunt being just an int equivalent of a boolean. Let me know if you need more parts of the code! Quite a mystery to me so far... Thank you!

  • Answer ✓

    Ah ha! I see the problem now. For real this time.

    You're doing this:

    int [] cond = new int [3];
    

    When you should just be doing this:

        cond = new int [3];
    

    The former creates a new, local-to-the-constructor array that "hides" access to the class's cond array. The latter uses the class's cond array instead of making a local variable.

    You're doing the same thing with nhex too.

    Let me know if you need this explained better! :-)

  • That fixed it! Thank you thank you! I was confused about how to initialize arrays within a class. Works perfecly now that I'm not declaring the arrays within the constructor. Happy day!

Sign In or Register to comment.