We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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:
What you need to do is tell the ArrayList that it is only for storing NPC classes. Like so:
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:
Or:
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:
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!
Ah ha! I see the problem now. For real this time.
You're doing this:
When you should just be doing this:
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!