-
Help with array
I've created a new DataType called OrderItem that contains a bunch of Strings, and call it in my main program and create an array out of it. I then stuff values into each of the Strings from my main program, then work with those later on... my problem is all of my array elements seem to contain exactly the same data, instead of the correct data for each element. Can someone help show me what i'm doing wrong? Thanks in advance!
Code:
My OrderItem DataType:
Code:
public class OrderItem {
static String iShipmentID = "";
static String iProductID = "";
static String iProductName = "";
static String iQuantity = "";
static String iUnitPrice = "";
static String iUnitCost = "";
static String iOptionPrice = "";
static String iWeight = "";
static String iDimension = "";
static String iWarehouseID = "";
static String iDateAdded = "";
static String iPageAdded = "";
static String iProdType = "";
static String iTaxable = "";
static String iItemPrice = "";
static String iTotal = "";
}
part of my main program that works with that dataType
Code:
numberOfItem = itemNodeList.getLength();
OrderItem orderItems = null;
orderItems = new OrderItem[numberOfItem];
for (int c = 0; c < numberOfItem; c++) {
Element itemElement = (Element) itemNodeList.item(c);
Element e7Element = itemElement;
iShipmentID = getTagValue("ShipmentID", e7Element).trim();
iProductID = getTagValue("ProductID", e7Element).trim();
System.out.println("OrderNum: " + oInvoiceNumber + " iProductID: " + iProductID);
iProductName = getTagValue("ProductName", e7Element).trim();
iQuantity = getTagValue("Quantity", e7Element).trim();
iUnitPrice = getTagValue("UnitPrice", e7Element).trim();
iUnitCost = getTagValue("UnitCost", e7Element).trim();
iOptionPrice = getTagValue("OptionPrice", e7Element).trim();
iWeight = getTagValue("Weight", e7Element).trim();
iDimension = getTagValue("Dimension", e7Element).trim();
iWarehouseID = getTagValue("WarehouseID", e7Element).trim();
iDateAdded = getTagValue("DateAdded", e7Element).trim();
iPageAdded = getTagValue("PageAdded", e7Element).trim();
iProdType = getTagValue("ProdType", e7Element).trim();
iTaxable = getTagValue("Taxable", e7Element).trim();
iItemPrice = getTagValue("ItemPrice", e7Element).trim();
iTotal = getTagValue("Total", e7Element).trim();
orderItems[c].iShipmentID = iShipmentID;
orderItems[c].iProductID = iProductID;
orderItems[c].iProductName = iProductName;
orderItems[c].iQuantity = iQuantity;
orderItems[c].iUnitPrice = iUnitPrice;
orderItems[c].iUnitCost = iUnitCost;
orderItems[c].iOptionPrice = iOptionPrice;
orderItems[c].iWeight = iWeight;
orderItems[c].iDimension = iDimension;
orderItems[c].iWarehouseID = iWarehouseID;
orderItems[c].iDateAdded = iDateAdded;
orderItems[c].iPageAdded = iPageAdded;
orderItems[c].iProdType = iProdType;
orderItems[c].iTaxable = iTaxable;
orderItems[c].iItemPrice = iItemPrice;
orderItems[c].iTotal = iTotal;
iShipmentID = "";
iProductID = "";
iProductName = "";
iQuantity = "";
iUnitPrice = "";
iUnitCost = "";
iOptionPrice = "";
iWeight = "";
iDimension = "";
iWarehouseID = "";
iDateAdded = "";
iPageAdded = "";
iProdType = "";
iTaxable = "";
iItemPrice = "";
iTotal = "";
}
code i'm using to debug this problem:
Code:
for (int tempC = 0; tempC < numberOfItem; tempC++) {
System.out.println("Array Product ID's: " + orderItems[tempC].iProductID);
}
the output i'm getting:
Code:
OrderNum: 72014789 iProductID: BD315
OrderNum: 72014789 iProductID: BC173
OrderNum: 72014789 iProductID: NK002
Array Product ID's for row 0: NK002
Array Product ID's for row 1: NK002
Array Product ID's for row 2: NK002
my expected output would be:
Code:
OrderNum: 72014789 iProductID: BD315
OrderNum: 72014789 iProductID: BC173
OrderNum: 72014789 iProductID: NK002
Array Product ID's for row 0: BD315
Array Product ID's for row 1: BC173
Array Product ID's for row 2: NK002
Thanks again!!!
-
Re: Help with array
All your String items in that class are static elements.
kind regards,
Jos
-
Re: Help with array
Thanks Jos... so I removed all the "static" tags from my OrderItem datatype... however now when I run my program i'm getting a NullPointerException at the 26 of the above code... seems I can put anything into that object i created? not sure what that means... did I create my datatype wrong? It looks exactly as above except the static tags are now removed...
-
Re: Help with array
Quote:
Originally Posted by
SnakeDoc
Code:
OrderItem orderItems = null;
orderItems = new OrderItem[numberOfItem];
You created an OrderItem variable named orderItems which can contain an OrderItem object (or null), and you assign null to it. Then, you attempt to create an array of OrderItems and store it into orderItems. This doesn't work. If you want to store an array of OrderItems into a variable, you need to do something like this:
Code:
OrderItem[] orderItems = new OrderItem[numberOfItem];
-
Re: Help with array
thanks awinston,
I was attempting to ensure that the old orderItems object was cleared out on the next iteration through the processing loop...
I took your advice and removed the null, however i'm still getting a NullPointerException at the same line... and if i try to stuff anything into one of those strings from the created object, then i get the nullpointer at that line...
here's some updated code:
DataType:
Code:
public class OrderItem {
String iShipmentID = "";
String iProductID = "";
String iProductName = "";
String iQuantity = "";
String iUnitPrice = "";
String iUnitCost = "";
String iOptionPrice = "";
String iWeight = "";
String iDimension = "";
String iWarehouseID = "";
String iDateAdded = "";
String iPageAdded = "";
String iProdType = "";
String iTaxable = "";
String iItemPrice = "";
String iTotal = "";
}
main code:
Code:
numberOfItem = itemNodeList.getLength();
// orderItems = null;
OrderItem[] orderItems = new OrderItem[numberOfItem];
for (int c = 0; c < numberOfItem; c++) {
Element itemElement = (Element) itemNodeList.item(c);
Element e7Element = itemElement;
iShipmentID = getTagValue("ShipmentID", e7Element).trim();
iProductID = getTagValue("ProductID", e7Element).trim();
System.out.println("OrderNum: " + oInvoiceNumber + " iProductID: " + iProductID);
iProductName = getTagValue("ProductName", e7Element).trim();
iQuantity = getTagValue("Quantity", e7Element).trim();
iUnitPrice = getTagValue("UnitPrice", e7Element).trim();
iUnitCost = getTagValue("UnitCost", e7Element).trim();
iOptionPrice = getTagValue("OptionPrice", e7Element).trim();
iWeight = getTagValue("Weight", e7Element).trim();
iDimension = getTagValue("Dimension", e7Element).trim();
iWarehouseID = getTagValue("WarehouseID", e7Element).trim();
iDateAdded = getTagValue("DateAdded", e7Element).trim();
iPageAdded = getTagValue("PageAdded", e7Element).trim();
iProdType = getTagValue("ProdType", e7Element).trim();
iTaxable = getTagValue("Taxable", e7Element).trim();
iItemPrice = getTagValue("ItemPrice", e7Element).trim();
iTotal = getTagValue("Total", e7Element).trim();
orderItems[c].iShipmentID = iShipmentID;
orderItems[c].iProductID = iProductID;
orderItems[c].iProductName = iProductName;
orderItems[c].iQuantity = iQuantity;
orderItems[c].iUnitPrice = iUnitPrice;
orderItems[c].iUnitCost = iUnitCost;
orderItems[c].iOptionPrice = iOptionPrice;
orderItems[c].iWeight = iWeight;
orderItems[c].iDimension = iDimension;
orderItems[c].iWarehouseID = iWarehouseID;
orderItems[c].iDateAdded = iDateAdded;
orderItems[c].iPageAdded = iPageAdded;
orderItems[c].iProdType = iProdType;
orderItems[c].iTaxable = iTaxable;
orderItems[c].iItemPrice = iItemPrice;
orderItems[c].iTotal = iTotal;
iShipmentID = "";
iProductID = "";
iProductName = "";
iQuantity = "";
iUnitPrice = "";
iUnitCost = "";
iOptionPrice = "";
iWeight = "";
iDimension = "";
iWarehouseID = "";
iDateAdded = "";
iPageAdded = "";
iProdType = "";
iTaxable = "";
iItemPrice = "";
iTotal = "";
}
debug code:
Code:
for (int tempC = 0; tempC < numberOfItem; tempC++) {
//System.out.println("Number of Items; " + numberOfItem);
System.out.println("Array Product ID's for row " + tempC + ": " + orderItems[tempC].iProductID);
}
console out results:
Code:
OrderNum: 72014789 iProductID: BD315
java.lang.NullPointerException
at jdbGetOrders.ASWTransformer.interpreterXML(ASWTransformer.java:554)
at jdbGetOrders.ASWTransformer.aswTransformer(ASWTransformer.java:160)
at jdbGetOrders.APITalk.main(APITalk.java:7)
line 554 of ASWTransformer is line 26 in my main program code above.
Thanks again guys...
EDIT: I should add that lines 8 - 24 are Strings...
-
Re: Help with array
When you want to do, say, this:
Code:
orderItems[c].iShipmentID = iShipmentID;
That particular array element should refer to an OrderItem object and it doesn't; before you can do the above you should create an OrderItem:
Code:
orderItems[c]= new OrderItem();
kind regards,
Jos
-
Re: Help with array
Jos -- thank you very much!
That did the trick perfectly... i now get my expected results. -- just for completeness, i'm posting the main section of my code that was fixed by your post so others can see how it was structured.
ps: i ended up re-implementing the orderItems = null and it seems to still work as expected... this way i'm comfortable in that the object is destroyed properly in between iterations of my loop so i don't accidentally reference old data in the array from a previous iteration... (the entire structure below is nested inside a larger loop).
Code:
numberOfItem = itemNodeList.getLength();
orderItems = null;
orderItems = new OrderItem[numberOfItem];
for (int c = 0; c < numberOfItem; c++) {
Element itemElement = (Element) itemNodeList.item(c);
Element e7Element = itemElement;
iShipmentID = getTagValue("ShipmentID", e7Element).trim();
iProductID = getTagValue("ProductID", e7Element).trim();
System.out.println("OrderNum: " + oInvoiceNumber + " iProductID: " + iProductID);
iProductName = getTagValue("ProductName", e7Element).trim();
iQuantity = getTagValue("Quantity", e7Element).trim();
iUnitPrice = getTagValue("UnitPrice", e7Element).trim();
iUnitCost = getTagValue("UnitCost", e7Element).trim();
iOptionPrice = getTagValue("OptionPrice", e7Element).trim();
iWeight = getTagValue("Weight", e7Element).trim();
iDimension = getTagValue("Dimension", e7Element).trim();
iWarehouseID = getTagValue("WarehouseID", e7Element).trim();
iDateAdded = getTagValue("DateAdded", e7Element).trim();
iPageAdded = getTagValue("PageAdded", e7Element).trim();
iProdType = getTagValue("ProdType", e7Element).trim();
iTaxable = getTagValue("Taxable", e7Element).trim();
iItemPrice = getTagValue("ItemPrice", e7Element).trim();
iTotal = getTagValue("Total", e7Element).trim();
// Jos' FIX
orderItems[c] = new OrderItem();
// Jos' FIX
orderItems[c].iShipmentID = iShipmentID;
orderItems[c].iProductID = iProductID;
orderItems[c].iProductName = iProductName;
orderItems[c].iQuantity = iQuantity;
orderItems[c].iUnitPrice = iUnitPrice;
orderItems[c].iUnitCost = iUnitCost;
orderItems[c].iOptionPrice = iOptionPrice;
orderItems[c].iWeight = iWeight;
orderItems[c].iDimension = iDimension;
orderItems[c].iWarehouseID = iWarehouseID;
orderItems[c].iDateAdded = iDateAdded;
orderItems[c].iPageAdded = iPageAdded;
orderItems[c].iProdType = iProdType;
orderItems[c].iTaxable = iTaxable;
orderItems[c].iItemPrice = iItemPrice;
orderItems[c].iTotal = iTotal;
iShipmentID = "";
iProductID = "";
iProductName = "";
iQuantity = "";
iUnitPrice = "";
iUnitCost = "";
iOptionPrice = "";
iWeight = "";
iDimension = "";
iWarehouseID = "";
iDateAdded = "";
iPageAdded = "";
iProdType = "";
iTaxable = "";
iItemPrice = "";
iTotal = "";
}