-3

I seem to have a frustrating problem - an element of my arraylist is overwritten. I have looked at many posts in many forums, but I have not managed to solve it. :chomp:

In my program (which is actually a FBDK function block algorithm implemented in Java), I want to store the data of an input variable (PART) into an element of an out variable array (PART_array). This process will happen multiple times (with the occurence of an event) and must thus store the variable in several array elements.

The problem is that the elements of PART_array are overwritten by the last entry. For instance, for the first event occurence, PART_array[] = ["1"," "," "," "," "," "]. Then, with the second occurence, instead of PART_array[] = ["1","2"," "," "," "," "], I find PART_array[] = ["2","2"," "," "," "," "] - thus showing the overwrite. I have realised that overwrite already occurs with the storage to PART_ARRAY ArrayList. I thought that by reinitializing (p = new Part()) the problem would be solved...obviously not.

Any help in solving this problem will be GREATLY appreciated! The code is as follows:

public class STORE_TO_ARRAY extends fb.rt.FBInstance {

    public ArrayList<Part> PART_ARRAY = new ArrayList<Part>();
    Part p = new Part();

    /** The default constructor. */
    public STORE_TO_ARRAY() {
       super();
    }

    /** ALGORITHM REQ IN Java*/ -- a method called in the program
    public void alg_REQ() {
        int ct = 0;
        ct = current_task;
        if (ct <= NumOfTasks) {
            //write received input data to output variable array elements
            //this is where the problem occurs!!!!
            p.setPart(PART); //set value of Part
            PART_ARRAY.add(ct-1,p); //adding to arraylist
            Part p = new Part(); //trying to reinitialise the object    
        }
    }
}

The class file for Part is as follows:

public class Part {
    WSTRING part;
    void setPart(WSTRING part) {
        this.part = part;
    }
    WSTRING getPart() {
        return part;
    }
}
Orbling
  • 20,413
  • 3
  • 53
  • 64
Karel
  • 13
  • 1
  • 4
  • 6
    Wow - that's a confusing code... At first look I would not have said it is Java! I hope you'll remember in a week what the difference is between `PART_ARRAY`, `PART_array` and `ARRAY_PART`... – assylias Nov 23 '12 at 10:09
  • 2
    That code looks so ugly, and doesn't even compile at all. You understand that `INT` and `int` are two different things in `Java`? – Rohit Jain Nov 23 '12 at 10:11
  • 3
    Try to create a much-simplified example that still replicates the problem. A couple of reasons for doing that: 1. About 90% of the time, just the act of doing that helps you understand and fix what's going wrong. 2. If it doesn't, you have a nice, simple, self-contained example to use to ask for help. – T.J. Crowder Nov 23 '12 at 10:12
  • All right, I apologise for the messy code. I have now edited the question to only show the code relative to my problem. It only focusses on the part where I add a Part to the PART_ARRAY ArrayList. If anyone can just tell me why it overwrites the existing ArrayList element, along with a solution, it would be fantastic and greatly appreciated. – Karel Nov 23 '12 at 10:47
  • 1
    Besides that the indention is not proper I miss the declaration of `current_task`. I haven't read further because it's too difficult to track the code without proper formatting. – Uwe Plonus Nov 23 '12 at 11:16
  • There is a standard for Java identifiers. Fix your code to conform to the standard if you want other people to read your code. – Stephen C Nov 23 '12 at 11:51

1 Answers1

0

The below code creates a local variable (which, in the following line, goes out of scope and disappears before it is used), rather than assigning the class variable with a new value (which I'm assuming is what you're trying to do).

Part p = new Part(); //trying to reinitialise the object

Try replacing it with

p = new Part(); //trying to reinitialise the object

On a side note, a decent IDE (like NetBeans) should give you a warning about creating a local variable which hides a class variable.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138