aspose file tools*
The moose likes Java in General and the fly likes ArrayList of ArrayList<String> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList of ArrayList<String>" Watch "ArrayList of ArrayList<String>" New topic
Author

ArrayList of ArrayList<String>

Umesh Vajpai
Greenhorn

Joined: Mar 14, 2010
Posts: 26
Hi All,

I am doing some integration stuff and adding some xml nodes values in a list for a xml file. Once I am done with one file I have to add that list to another list.
There are multiple xml files add I want to make a list of list.
I created something like this but it didn't work-
ArrayList<ArrayList<String>> arr=new ArrayList<ArrayList<String>>();

Can you guys get me some ideas on this...
Note- I dont want to create a Array of ArrayList as I dont know size. I am dealing dynamacally and there may be addition to the xml file dynamically.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

That ought to work fine, so I suspect that the problem is really somewhere else.

Though I'd prefer to use
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

ItDoesntWorkIsUseless. What does it not do, and what should it do?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Umesh Vajpai
Greenhorn

Joined: Mar 14, 2010
Posts: 26
What basically I am trying to do is that get all the properties/nodes from the object xml file and write them in the salesforce database.
I dont know why I am getting Null Pointer/Array Index out of bound Exception when I use ArrayList of ArrayList..
If I use Arraylist of String with single record that I stored by reading xml, then I dont see any issue.However, for Arraylist of arraylist its throwing exception.
Here you can see the Sample code-




This method works fine with hardcoded values and in the same way I am trying to do with more than one list .


I am getting a list by reading a xml file and adding them in another list in this way..
Sample code-
Suppose I added 10 elements in it..I would now add this in my another list as I am done with one xml file and will read another.

I will pass this collection to createObjects(....) method with all required arguments but it throws null pointer exception..
I have put comments on the line where I got exception.

Please do help me to understand what wrong I have done and what needs to correct to make it work...

[Edit - added code tags - MB]

Umesh Vajpai
Greenhorn

Joined: Mar 14, 2010
Posts: 26
Would be great help if someone can provide me sample code to write arrayList of ArrayList to any database.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Umesh Vajpai wrote:Would be great help if someone can provide me sample code to write arrayList of ArrayList to any database.

Well, first of all, this site is NotACodeMill. Second, you could do yourself a lot of favours by reading the UseCodeTags page.

The reason for both exceptions is very simple:
1. IndexOutOfBoundsException is produced when you are trying to access an element that doesn't exist in your ArrayList; and I can certainly see why that might be - the "record" that you're returning almost certainly has fewer elements than your 'ObjectApiNames' array. What I suggest you do is to print out what you're getting from your 'get()' statement.
2. NullPointerException is produced because something is null. Since I have no idea what binding.create() does, it would be useless to speculate why, but you could try printing out the contents of 's' immediately before you call it. Also, reading the API documentation for that method might tell you if/when it throws an NPE.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Umesh Vajpai wrote:Suppose I added 10 elements in it...

But that's precisely what you can't do. You must add at least 12 elements to it, because that's the number of labels you have (at least I assume that's what's in ObjectApiNames).

I would now add this in my another list as I am done with one xml file and will read another.

As Matthew pointed out, you're much better off using List<List<String>> - in your method parameter as well as your field definition. Have a look at his post again.

Winston
Umesh Vajpai
Greenhorn

Joined: Mar 14, 2010
Posts: 26
Thanks everyone for your inputs..I will try out the way your guys suggested.
I appreciate your help..!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayList of ArrayList<String>