Store an Array of Classes e.g. Order / Line Items example

Hello, I am having trouble storing an array of classes, they all end up having the same value.

Consider the following simple class definition:

Class Order

private LineItem[] lineItems;

Class LineItem(amount, price)

private int amount;

private float price;

Consider the following pseudo code as part of the constructor of class order (to fill the array with a bunch of LineItems):

for x = 1 to 10

lineItems[x] = new LineItem(amount, price);

however all lineItems[x] have the same LineItem

any ideas?

[571 byte] By [xarfoxa] at [2007-11-27 10:16:33]
# 1

Post the actual code please. Until then we're just debugging your design, which isn't very helpful.

Are the amount and price values changing in the loop?

hunter9000a at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 2

i posted the design since its a bit more complicated then my summarized version, but at any rate i will post the full solution for those who want to view either or...

basically this takes an XML field and creates an object Order which has some attributes plus an array of Line Item objects.

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import org.dom4j.Node;

import org.dom4j.Document;

public class SiebelOrder

{

private String accountId;

private String backOfficeOrderNumber;

private String contactFirstName;

private String contactId;

private String contactLastName;

private String currencyCode;

private String description;

private String entryAssociate;

private String legalEntity;

private String orderRevenue;

private String orderDate;

private String orderNumber;

private String orderType;

private String orderTypeId;

private String primaryOrganization;

private String primaryServiceRegion;

private String primaryShipToAddress;

private String PrimaryShipToAddressId;

private String PrimaryShipToCity;

private String PrimaryShipToCountry;

private String PrimaryShipToPostalCode;

private String PrimaryShipToState;

private String RegionAddress;

private String RegionAddressId;

private String RegionCity;

private String RegionCountry;

private String RegionPostalCode;

private String RegionState;

private String Revision;

private String SalesRecognizedDate;

private String status;

private String taxExempt;

private String taxExemptNumber;

private String taxExemptReason;

private SiebelLineItem[] lineItems;

protected SiebelOrder()

{

//default constructor

}

protected SiebelOrder(Node pOrderNode)

{

//gets the attribute by using an xpath query of the given node

accountId = pOrderNode.valueOf("//AccountId/text()");

backOfficeOrderNumber = pOrderNode.valueOf("//BackOfficeOrderNumber/text()");

contactFirstName = pOrderNode.valueOf("//ContactFirstName/text()");

contactId = pOrderNode.valueOf("//ContactId/text()");

contactLastName = pOrderNode.valueOf("//ContactLastName/text()");

currencyCode = pOrderNode.valueOf("//CurrencyCode/text()");

description = pOrderNode.valueOf("//Description/text()");

entryAssociate = pOrderNode.valueOf("//EntryAssociateEMP/text()");

legalEntity = pOrderNode.valueOf("//LegalntityEMP/text()");

orderRevenue = pOrderNode.valueOf("//OrderRevenueEMP/text()");

orderDate = pOrderNode.valueOf("//OrderDate/text()");

orderNumber = pOrderNode.valueOf("//OrderNumber/text()");

orderType = pOrderNode.valueOf("//OrderType/text()");

orderTypeId = pOrderNode.valueOf("//OrderTypeId/text()");

primaryOrganization = pOrderNode.valueOf("//PrimaryOrganization/text()");

primaryServiceRegion = pOrderNode.valueOf("//PrimaryServiceRegionEMP/text()");

primaryShipToAddress = pOrderNode.valueOf("//PrimaryShipToAddress/text()");

PrimaryShipToAddressId = pOrderNode.valueOf("//PrimaryShipToAddressId/text()");

PrimaryShipToCity = pOrderNode.valueOf("//PrimaryShipToCity/text()");

PrimaryShipToCountry = pOrderNode.valueOf("//PrimaryShipToCountry/text()");

PrimaryShipToPostalCode = pOrderNode.valueOf("//PrimaryShipToPostalCode/text()");

PrimaryShipToState = pOrderNode.valueOf("//PrimaryShipToState/text()");

RegionAddress = pOrderNode.valueOf("//RegionAddress/text()");

RegionAddressId = pOrderNode.valueOf("//RegionAddressId/text()");

RegionCity = pOrderNode.valueOf("//RegionCity/text()");

RegionCountry = pOrderNode.valueOf("//RegionCountry/text()");

RegionPostalCode = pOrderNode.valueOf("//RegionPostalCode/text()");

RegionState = pOrderNode.valueOf("//RegionState/text()");

Revision = pOrderNode.valueOf("//RegionRevision/text()");

SalesRecognizedDate = pOrderNode.valueOf("//SalesRecognizedDateEMP/text()");

status = pOrderNode.valueOf("//Status/text()");

taxExempt = pOrderNode.valueOf("//TaxExempt/text()");

taxExemptNumber = pOrderNode.valueOf("//TaxExemptNumber/text()");

taxExemptReason = pOrderNode.valueOf("//TaxExemptReason/text()");

// -- create line items --

//queries the order node using xpath and returns it as a list of line item nodes

List lineItemList = pOrderNode.selectNodes("//OrderEntry-LineItemsEmp");

//sizes the array to the number of line items

lineItems = new SiebelLineItem[lineItemList.size()];

//update the array to be a list of blank lineitems

for(int i=0; i < lineItemList.size(); i++)

lineItems[i] = new SiebelLineItem();

//store the lineitem details in each array item

int i = 0;

for (Iterator iter = lineItemList.iterator(); iter.hasNext(); )

{

Node lineItemNode = (Node) iter.next();

// ALL lineItems[i] ARE THE SAME VALUE

lineItems[i].setLineDetails(lineItemNode);

i++;

}

}

}

import java.util.HashMap;

import java.util.Map;

import org.dom4j.Document;

import org.dom4j.Node;

public class SiebelLineItem

{

private String actualSalesPrice;

private String costProduct;

private String ESRPLabor;

private String inactiveFlag;

private String installationMethod;

private String integrationId;

private String laborOptionFlag;

private String lineItemType;

private String lineNumber;

private String lineSequenceNumber;

private String lineStatus;

private String orderHeaderId;

private String product;

private String productId;

private String productSeries;

private String productStyle;

private String servicePartFlag;

private String taxRate;

private String taxType;

private String UOMLine;

private String UOMQuantity;

private String vendorCode;

private String vendorName;

protected SiebelLineItem()

{

//default constructor

}

protected void setLineDetails(Node pLineItemNode)

{

//gets the attribute by using an xpath query of the given node

actualSalesPrice = pLineItemNode.valueOf("//ActualSalesPriceEMP/text()");

costProduct = pLineItemNode.valueOf("//CostProductEMP/text()");

ESRPLabor = pLineItemNode.valueOf("//ESRPLaborEMP/text()");

inactiveFlag = pLineItemNode.valueOf("//InactiveFlagEMP/text()");

installationMethod = pLineItemNode.valueOf("//InstallationMethodEMP/text()");

integrationId = pLineItemNode.valueOf("//IntegrationIdEMP/text()");

laborOptionFlag = pLineItemNode.valueOf("//LaborOptionFlagEMP/text()");

lineItemType = pLineItemNode.valueOf("//LineItemTypeEMP/text()");

lineNumber = pLineItemNode.valueOf("//LineNumberEMP/text()");

lineSequenceNumber = pLineItemNode.valueOf("//LineSequenceNumberEMP/text()");

lineStatus = pLineItemNode.valueOf("//LineStatusEMP/text()");

orderHeaderId = pLineItemNode.valueOf("//OrderHeaderId/text()");

product = pLineItemNode.valueOf("//Product/text()");

productId = pLineItemNode.valueOf("//ProductId/text()");

productSeries = pLineItemNode.valueOf("//ProductSeriesEMP/text()");

productStyle = pLineItemNode.valueOf("//ProductStyleEMP/text()");

servicePartFlag = pLineItemNode.valueOf("//ServicePartFlagEMP/text()");

taxRate = pLineItemNode.valueOf("//TaxRateEMP/text()");

taxType = pLineItemNode.valueOf("//TaxTypeEMP/text()");

UOMLine = pLineItemNode.valueOf("//UOMLineEMP/text()");

UOMQuantity = pLineItemNode.valueOf("//UOMQuantityEMP/text()");

vendorCode = pLineItemNode.valueOf("//VendorCodeEMP/text()");

vendorName = pLineItemNode.valueOf("//VendorNameEMP/text()");

}

}

xarfoxa at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 3

1) Did you really have to post ALL that? Wouldn't a small representative sample have been enough?

2) I have no idea what problem you're having.

jverda at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 4

hunter9000 requested the full code, in my first post i did post a small example of pseudocode

the problem i'm having is that all the array values contain the same object

so if in the XML: Line item 1 = $500 & Line item 2 = $1500

in my array it ends up being: myarray[0].price = $500 and myarray[1].price = $500

in debugging when the setLineDetails method is called, the incoming node does contain the correct price for the 2nd iteration ($1500) - except when it executes price = my xpath query, price gets set to $500, not $1500

i've been stuck on this way too long

xarfoxa at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 5

> hunter9000 requested the full code, in my first post

> i did post a small example of pseudocode

He didn't really mean foo("a");

foo("b");

...

foo("z");

(but withall the c-y filled in instead of dots, as you did. In fact, putting dots would have been much better.)

He meant enough actual code to show what you're doing. If foo("a") through foo("z") all give the same problem, you don't need to show all of them.

> the problem i'm having is that all the array values

> contain the same object

Showing the code you use to determine that would be helpful. Also the code you use to put stuff into the list or whatever.

jverda at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 6

if you review the area that says:

// -- create line items --

this is where i am putting it into the list

i feel like it is somehow querying the old node even though i passed it a new one. the debugger however disagrees

xarfoxa at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 7

List lineItemList = pOrderNode.selectNodes("//OrderEntry-LineItemsEmp");

You're iterating through this list in a perfectly ordinary way, and you are finding that all the elements of the list contain the same information. So one of my guesses would be that all of the elements of the list do really contain the same information. You might consider looking at the elements to see if they do.

Following on from that, you might consider debugging other possibilities. Just looking at the code tells you nothing without knowing what data is going through the code. I don't suppose I need to explain what debugging is, do I?

DrClapa at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 8

Yes, I did view the objects during debugging. I tried to explain in previous posts but maybe I was unclear.

I am testing with two line items, the first line item has a price of 500 whereas the second has a price of 1500.

During the 2nd iteration of the loop - the following happens:

Node lineItemNode = (Node) iter.next();

lineItems[i].setLineDetails(lineItemNode);

//lineItemNode.actualSalesPrice = 1500

//now it steps into my setLineDetails method

actualSalesPrice = pLineItemNode.valueOf("//ActualSalesPriceEMP/text()");

//pLineItemNode.actualSalesPrice = 1500

//actualSalesPrice gets set to 500

//weird!!

xarfoxa at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...
# 9

ok I pretty much figured this out

i thought I was breaking down the nodes each time when going to the next iteration, but it actually retains the original XML, so it did indeed contain both nodes containing price = 500 and price = 1500

however the debugger only displayed the current node that i was on, not the fact that more xml elements existed

im going to maybe chalk this up as a limitation in the jdeveloper ide or perhaps my own incompetence

thanks!

xarfoxa at 2007-7-28 15:46:16 > top of Java-index,Java Essentials,Java Programming...