Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collection

Status
Not open for further replies.

extempore

Programmer
Jun 1, 2001
71
US
Hi,
I am newbie to Java who had been given this task of debugging a bug. I would really appreaicte if you can help me in this. I have to find out why exportAssets.length is zero in the final method call down below(last method). Since it had a value before the method was called. Can someone tell me y? I have also put in my comments to tell you the return values I am getting after each call.Thanks

public void updateExportAssets(String uoiId, String mediaTypeName, ExportAsset[] newExportAssets) {
ExportAssetDao dao = new ExportAssetDao();
ExportAsset[] oldExportAssets = dao.getSelectedExportAssets(uoiId, mediaTypeName);
Set oldSet = new HashSet();
oldSet.addAll(Arrays.asList(oldExportAssets));
Set newSet = new HashSet();
newSet.addAll(Arrays.asList(newExportAssets));
Collection addedItems = getChangedItems(oldSet, newSet);
log.error("addedItems:" + addedItems);
-- This returns addedItems:[]

Collection deletedItems = getChangedItems(newSet, oldSet);
log.error("deletedItems:" + deletedItems);
-- This returns deletedItems:[]

log.error("OLD EXPORT ASSETS LENGTH:" + oldExportAssets.length);
-- This returns OLD EXPORT ASSETS LENGTH:8

log.error("NEW EXPORT ASSETS LENGTH:" + newExportAssets.length);
-- This returns NEW EXPORT ASSETS LENGTH:8

dao.removeExportAssetLinks( (ExportAsset[]) deletedItems.toArray(new ExportAsset[deletedItems.size()]));

dao.addExportAssetLinks( (ExportAsset[]) addedItems.toArray(new ExportAsset[addedItems.size()]));
}

/**
* Returns a collection with all items in newCol not in oldCol.
*/
protected Collection getChangedItems(Collection oldCol, Collection newCol) {
Iterator it = newCol.iterator();
Object o = null;
Collection retval = new ArrayList();
while (it.hasNext()) {
o = it.next();
if (!oldCol.contains(o)) {
retval.add(o);
}
}
return retval;
}

}

public void removeExportAssetLinks(ExportAsset[] exportAssets) {
for (int i=0; i<exportAssets.length; i++)

-- HERE the exportAssets.length becomes 0 (ZERO) so the subsequent call below does not work

{
removeExportAssetLink(exportAssets);
}
}
 
As a general comment, when I'm having trouble with arguements using casting, I like to seperate the casted variable from the function call to better examine it.

If you have...

ExportAsset test[] = (ExportAsset[])
deletedItems.toArray(new ExportAsset[deletedItems.size()]);

dao.removeExportAssetLinks( test );


...and you add a breakpoint before the call to removeExportAssetLinks(), you can more easily examine test and its properties to track down what's going haywire.
 
your code
Code:
public void removeExportAssetLinks(ExportAsset[] exportAssets) {
    for (int i=0; i<exportAssets.length; i++) 
    
    -- HERE the exportAssets.length becomes 0 (ZERO) so the subsequent call below does not work
    
    {
        removeExportAssetLink(exportAssets);
    }

looks weird.

Are you sure you don't mean to do
Code:
public void removeExportAssetLinks(ExportAsset[] exportAssets) {
    for (int i=0; i<exportAssets.length; i++) 
        {
        removeExportAssetLink(exportAssets[i]);
    }

if the function removeExportAssetLink() wants (as I would think) a single ExportAsset instead of the entire array?
Most likely your array isn't zero length after all, but you're getting an exception trying to send an array into a function expecting a single object contained in that array?

 
If you want to send the whole array as an argument wont you delete the [] brackets from the method call. if you have brackets it means a single item is being sent down the pipe....! home that helps...
libaax
 
I didn't catch it before, but ExportAsset[] exportAssets should be ExportAsset exportAssets[].
 
jwenting:

I think the code posted was right, but the [ i ] gets changed by the TGML. I have had to put spaces in here!


This has been noted before so it's best to use a different letter or name than &quot;i&quot;.

[smile]
 
Folks,

thanks for you kinda consideration in helping me out with my issue. I was able to fix the problem by myself. Thanks again.

 
Extempore,

You have to put your code between [ code ] and [ /code ] (without the spaces) otherwhise [ i ] gets interpreted as &quot;display in italics&quot;.
When you do a &quot;Preview Post&quot; you can see this.
 
Thanks a lot for all your help guys. I was able to resolve the issue. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top