aspose file tools*
The moose likes Beginning Java and the fly likes Creating a data structure array. 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 » Beginning Java
Bookmark "Creating a data structure array." Watch "Creating a data structure array." New topic
Author

Creating a data structure array.

Greg Spiegel
Greenhorn

Joined: May 30, 2003
Posts: 4
I am trying to create a data structure that contains 13 records with different data types, so that rules out a multi-dimensional array. I created a class with the structure that I would like to use and I can use it as a single variable. I then try to create an array of 13 of these and then I am unable to work with it, I get a java.lang.NullPointerException.

Please let me know what I am doing wrong, I looked into using collections but I figured since they array will not be growing I should just use an array. Do I need a collection to do this type of thing.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Hi Greg,
Welcome to the Ranch!

In your code above you: (1) create mrResults to hold the reference to the array, (2) create the array of 13 references to MonthlyResults, (3) try to reference the first element.
What you didn't do is to create the 13 MonthyResult instances.
When you created the array with new MonthlyResults[13], you created 13 reference variables, but this does not create any instances of a MonthlyResult object.
For that, you would need to add something like:

after you create the array.
Does this help?
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Greg Spiegel
Greenhorn

Joined: May 30, 2003
Posts: 4
Thank you,
I did not realize that the instances were not created when the array was created. That helped alot.
Greg Spiegel
Greenhorn

Joined: May 30, 2003
Posts: 4
Why don't the instances get created like they would with other arrays.

How come you wouldn't have to add a line
intArray[0] = new int();
prior to using the array?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

This is because the other types of arrays are primitive arrays; they cannot be null, and they do not need to be created. (It doesn't make sense to say "new int()")
If you want to think of it this way, you can think that when you create an array, all elements of the array are set to 0. For numeric arrays, this is the appropriate 0 (0L, 0.0, etc.). For boolean arrays, this is false (where false = 0 and true = !0), For object arrays, this is null (in C terms, a pointer to memory location 0, even though that is not necessarily how it is implemented in Java...).
Hope that this helps.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

I'd add that it's just the basic difference between primitive types and objects. Nothing to do with arrays really.
In the following code:

a is an integer value, while b is a SomeObject reference; no SomeObject instance is created until you "new" one up.
Same with arrays. new SomeObject[13] declares 13 SomeObject references, but no actual objects until you instantiate them.
That help?
bear
[ May 30, 2003: Message edited by: Bear Bibeault ]
Greg Spiegel
Greenhorn

Joined: May 30, 2003
Posts: 4
Yeah, thank you all. BTW Is there a clear all method for a JTable?
Pin Ting
Greenhorn

Joined: May 28, 2003
Posts: 22
I guess you'd better restart a new discussion thread for the JTable question


Never do things the hard way if you don't have a good reason.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a data structure array.