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.
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, 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:
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.
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 declares 13 SomeObject references, but no actual objects until you instantiate them. That help? bear [ May 30, 2003: Message edited by: Bear Bibeault ]
Joined: May 30, 2003
Yeah, thank you all. BTW Is there a clear all method for a JTable?