• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing values to a 2D array

 
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object that I want to create from an xml document. The problem is that there are certain values in the xml document I want to put in a 2D array. I.E



The 2D array shall a fixed amount of columns but N amount of rows depending on the device. How would you go about parsing this? My current set-up gets me an ArrayIndexOutOfBoundsException



the Set row method is meant to change the value of the snmpObject's 2D array. Any pointers would be helpful.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi SeanMichael Hayes,

Welcome to CodeRanch!

Good to see that you are using code tags.

Now, coming to your problem, an ArrayIndexOutOfBoundsException is natural because of line no. 3 and 4 of your Java code.

Please note that in Java, there are no n-dimensional (2D,3D etc.) arrays. What you are declaring is an array of arrays (and not a 2D array). In your code, you are initializing count as 0 and then passing it as size of array. So, in turn, you are creating an array of size 0. Obviously, the moment you access any element of that array, its gonna throw that typical exception.

If value of count is not known beforehand, and you are only concerned about key-value pairs (i.e. number of 'columns' is always 2), then how about using a Map (say HashMap)? That way, you don't need to declare its size during initialization.

I hope this helps.
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you write



how can you expect to have multiple rows? You defined a zero-rows array.
You will always get an ArrayIndexOutOfBoundsException there, when you try to populate it.

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you not creating a class to encapsulate your data, instead of arrays?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And use a List to store that data.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic