Results 1 to 7 of 7

Thread: Help with array

  1. #1
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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:

    Java 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

    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 = "";
    	    	    		   
    	    	    	   }
    code i'm using to debug this problem:

    Java Code:
    for (int tempC = 0; tempC < numberOfItem; tempC++) {
    	    			   System.out.println("Array Product ID's: " + orderItems[tempC].iProductID);
    	    		   }
    the output i'm getting:

    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
    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: BD315
    Array Product ID's for row 1: BC173
    Array Product ID's for row 2: NK002
    Thanks again!!!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with array

    All your String items in that class are static elements.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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...

  4. #4
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Help with array

    Quote Originally Posted by SnakeDoc View Post
    Java 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:

    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

  5. #5
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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:

    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 = "";
    }
    main 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 = "";
    	    	    		   
    	    	    	   }
    debug code:

    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);
    	    		   }
    console out results:

    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)
    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...
    Last edited by SnakeDoc; 07-17-2012 at 11:18 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: Help with array

    When you want to do, say, this:

    Java 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:

    Java Code:
    orderItems[c]= new OrderItem();
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    SnakeDoc is offline Senior Member
    Join Date
    Apr 2012
    Posts
    129
    Rep Power
    0

    Default 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

  1. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  2. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  3. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  4. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •