简体   繁体   中英

Retrieve data from an ArrayList item

Apologies in advance, I am new to Java and I am using someone else's code for the most part so please bear with me. I have looked around but couldn't find anything to help my problem

I've retrieved an ArrayList from a method and then I've attempted to write a foreach loop to retrieve a specific piece of data from what is an 'Observation' below.

For whatever reason it won't allow me to retrieve any of the data stored inside an observation when accessing through the ArrayList.

    ArrayList<Observation>[] npcPositions = stateObs.getNPCPositions();
    Vector2d playerPosition = stateObs.getAvatarPosition();

    int npcCount = npcPositions.length;

    for (int i = 0; i <= npcCount; i++)
    {

        if (playerPosition.x == npcPositions[i].position)
        {

        }
    }

position being a value within the Observation but I get the error that it cannot be resolved or is not a field. Part of the observation class is below and I can not access any of these variables doing what I'm currently doing.

public class Observation implements Comparable<Observation>
{

/**
 * Category of this observation (static, resource, npc, etc.).
 */
public int category;

/**
 * Type of sprite of this observation.
 */
public int itype;

/**
 * unique ID for this observation
 */
public int obsID;

/**
 * Position of the observation.
 */
public Vector2d position;

/**
 * Reference to the position used for comparing this
 * observation with others.
 */
public Vector2d reference;

So what do I need to use to access those variables. I noticed that I have to use [] when I want to store data from stateObs.getNPCPositions and that seems to be the reason why other examples weren't working for me but I am unsure on how to fix it.

UPDATE

The original issue seems to be fixed, however when attempting to retrieve the length of the ArrayList, I get nullpointerexception. How can I get the number of items to be able to run through them in the loop each time.

UPDATE #2

    /**
 * Returns a list of observations of NPC in the game. As there can be
 * NPCs of different type, each entry in the array corresponds to a sprite type.
 * Every ArrayList contains a list of objects of type Observation.
 * Each Observation holds the position, unique id and
 * sprite id of that particular sprite.
 *
 * @return Observations of NPCs in the game.
 */
public ArrayList<Observation>[] getNPCPositions()
{
    return model.getNPCPositions(null);
}


/**
 * Returns a list of observations of NPC in the game. As there can be
 * NPCs of different type, each entry in the array corresponds to a sprite type.
 * Every ArrayList contains a list of objects of type Observation, ordered asc. by
 * distance to the reference passed. Each Observation holds the position, sprite type id and
 * sprite id of that particular sprite.
 *
 * @param reference   Reference position to use when sorting this array,
 *                    by ascending distance to this point.
 * @return Observations of NPCs in the game.
 */
public ArrayList<Observation>[] getNPCPositions(Vector2d reference)
{
    return model.getNPCPositions(reference);
}

In line:

npcPositions[i].position

Is an array of ArrayList which does not have any property position . Possibly you would try:

npcPositions[i].get(0).position

Edited:

As you said that this line gives NPE:

int npcCount = npcPositions.length;// possibly npcPositions is null

Below line is executed to get the array list:

public ArrayList<Observation>[] getNPCPositions()
{
    return model.getNPCPositions(null);//<-- note this, possibly model is also null
}

This:

ArrayList<Observation>[] npcPositions = stateObs.getNPCPositions();

is getting an array of ArrayList . You can get a single ArrayList from index i of the array using:

ArrayList<Observation> list = npcPositions[i];

You can get the Observation at index j of your list using:

Observation obs = list.get(j);

Or you can use them in combination:

Observation obs = npcPositions[i].get(j);

I am not sure what you are doing in the first two lines of your code, but assumming that what you are doing is correct then the problem lies with your if statement. You are trying to test if a Vector2D.x is equal to a Vector2D which can never happen. try doing this

for(int i = 0; i < npcCount; < i++)
{

 if(playerPosition == npcPositions.get(i).position)
    {
      //do something here
    }
 }

or you can try this

 for(int i = 0; i < npcCount; < i++)
{

 if(playerPosition.x == npcPositions.get(i).position.x)
    {
      //do something here
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM