Question

I have a program that calls a function, requestAudioBlocks to request some data. In it, it initialises an array, then calls a request for the data.

private ReadAudioData[] receivedAudioMessages;

private int receivedMessageCount;

private boolean requestAudioBlocks(long blocksPerRequest)
{
    receivedAudioMessages = new ReadAudioData[2];

    receivedMessageCount = 0;

    connection.sendRequest();  //Send the command
}

When connection.sendRequest is run, connection.sendRequest calls saveAudioBlocks, sending it a variable download which is to be put into the array.

private ReadAudioData audioDataPacket;

public void sendRequest()
{
    ReadAudioData readAudioData;

    while (int count = 0; count < 2; count++)
    {
        readAudioData = new ReadAudioData();

        //Add the received information into the readAudioData variable

        audioDataPacket = readAudioData;

        saveAudioBlocks(audioDataPacket);
    }
}

Every time a new ReadAudioData object is received, saveAudioBlocks is called.

public void saveAudioBlocks(ReadAudioData download)
{
    receivedAudioMessages[receivedMessageCount] = download;

    receivedMessageCount++;
}

In the running of the program, saveAudioBlocks is called twice, which should put two ReadAudioData objects into the array receivedAudioMessages.

It works the first time, as the correct ReadAudioData object goes into receivedAudioMessages[0].

A ReadAudioData object has a variable String, which in this case is "111". If you put a break point in saveAudioBlocks at the line receivedMessageCount++, you can look in the array and it contains the ReadAudioData object with String variable "111" as it should at location 0.

However, when saveAudioBlocks is called again with a new ReadAudioData object, if you look in receivedAudioMessages array at location 0, the ReadAudioData object is the new ReadAudioData object contained in the variable download passed to the function, which contains a String variable "122".

It is almost as if I am saving a pointer to the variable download in the array and not the actual object as it currently is.

I have tried to use ArrayLists as well, but the same problem occurs.

I have a break point at saveAudioBlocks to ensure it is not being called are replacing the data when I am not aware and it is not.

Can anyone help?

Was it helpful?

Solution

It is almost as if I am saving a pointer to the variable download in the array and not the actual object as it currently is.

This is what happens. You store the reference to the object that is referenced by the local variable download. So if properties of that object change, then the change becomes "visible" in your array too.

Arrays do not store the objects themselves, they store references to objects.

OTHER TIPS

It is most likely because in your calling code (where you call saveAudioBlocks you reuse the same ReadAudioData variable instead of creating a new one.

In other words, if you do this:

ReadAudioData data = new ReadAudioData();
data.setId("111");
receivedAudioMessages[0] = data;

data.setId("222");
receivedAudioMessages[1] = data;

Both receivedAudioMessages[0] and receivedAudioMessages[1] contain a reference to the same data, which now has an id of 222. You would get the same behaviour with an ArrayList.

This is a guess only as you have not provided the code that calls the saveAudioBlocks method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top