java - Retrieve data from an ArrayList item -


apologies in advance, new java , using else's code part please bear me. have looked around couldn't find problem

i've retrieved arraylist method , i've attempted write foreach loop retrieve specific piece of data 'observation' below.

for whatever reason won't allow me retrieve of data stored inside observation when accessing through arraylist.

    arraylist<observation>[] npcpositions = stateobs.getnpcpositions();     vector2d playerposition = stateobs.getavatarposition();      int npccount = npcpositions.length;      (int = 0; <= npccount; i++)     {          if (playerposition.x == npcpositions[i].position)         {          }     } 

position being value within observation error cannot resolved or not field. part of observation class below , can not access of these variables doing i'm doing.

public class observation implements comparable<observation> {  /**  * category of observation (static, resource, npc, etc.).  */ public int category;  /**  * type of sprite of observation.  */ public int itype;  /**  * unique id observation  */ public int obsid;  /**  * position of observation.  */ public vector2d position;  /**  * reference position used comparing  * observation others.  */ public vector2d reference; 

so need use access variables. noticed have use [] when want store data stateobs.getnpcpositions , seems reason why other examples weren't working me unsure on how fix it.

update

the original issue seems fixed, when attempting retrieve length of arraylist, nullpointerexception. how can number of items able run through them in loop each time.

update #2

    /**  * returns list of observations of npc in game. there can  * npcs of different type, each entry in array corresponds sprite type.  * every arraylist contains list of objects of type observation.  * each observation holds position, unique id ,  * sprite id of particular sprite.  *  * @return observations of npcs in game.  */ public arraylist<observation>[] getnpcpositions() {     return model.getnpcpositions(null); }   /**  * returns list of observations of npc in game. there can  * npcs of different type, each entry in array corresponds sprite type.  * every arraylist contains list of objects of type observation, ordered asc.  * distance reference passed. each observation holds position, sprite type id ,  * sprite id of particular sprite.  *  * @param reference   reference position use when sorting array,  *                    ascending distance point.  * @return observations of npcs in game.  */ public arraylist<observation>[] getnpcpositions(vector2d reference) {     return model.getnpcpositions(reference); } 

this:

arraylist<observation>[] npcpositions = stateobs.getnpcpositions(); 

is getting array of arraylist. can single arraylist index i of array using:

arraylist<observation> list = npcpositions[i]; 

you can observation @ index j of list using:

observation obs = list.get(j); 

or can use them in combination:

observation obs = npcpositions[i].get(j); 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -