Results 1 to 7 of 7
Thread: Help with array
- 07-17-2012, 09:29 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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:
part of my main program that works with that dataTypeJava 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 = ""; }
code i'm using to debug this problem:Java 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 = ""; }
the output i'm getting:Java Code:for (int tempC = 0; tempC < numberOfItem; tempC++) { System.out.println("Array Product ID's: " + orderItems[tempC].iProductID); }
my expected output would be:Java 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
Thanks again!!!Java 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
- 07-17-2012, 09:35 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with array
All your String items in that class are static elements.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-17-2012, 09:59 PM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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...
- 07-17-2012, 10:36 PM #4
Student
- Join Date
- Jul 2012
- Location
- United States
- Posts
- 328
- Rep Power
- 1
Re: Help with array
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:
Java Code:OrderItem[] orderItems = new OrderItem[numberOfItem];
"Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill
- 07-17-2012, 11:04 PM #5
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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:
main code:Java 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 = ""; }
debug code:Java 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 = ""; }
console out results:Java 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); }
line 554 of ASWTransformer is line 26 in my main program code above.Java 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)
Thanks again guys...
EDIT: I should add that lines 8 - 24 are Strings...Last edited by SnakeDoc; 07-17-2012 at 11:18 PM.
- 07-18-2012, 07:44 AM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with array
When you want to do, say, this:
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:Java Code:orderItems[c].iShipmentID = iShipmentID;
kind regards,Java Code:orderItems[c]= new OrderItem();
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-18-2012, 06:17 PM #7
Senior Member
- Join Date
- Apr 2012
- Posts
- 127
- Rep Power
- 0
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).
Java 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 = ""; }
Similar Threads
-
Reading a text file into an Array and spliting the content into another Array
By jtothemax in forum New To JavaReplies: 15Last Post: 05-14-2012, 12:42 PM -
Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05-12-2011, 07:04 PM -
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
Trying to make an array list // inserting an element to middle of array
By javanew in forum New To JavaReplies: 2Last Post: 09-06-2010, 01:03 AM -
How to add an integer to a array element and the store that backinto an array.
By Hannguoi in forum New To JavaReplies: 1Last Post: 03-31-2009, 06:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks