I have constructed a class to mimic a C# struct:
public class Favourite {
protected String favName;
protected String favText;
protected String favDelay;
protected GeoPoint favPoint;
protected Uri favUri;
}
I want to create an array of this class:
Favourite[] fav;
When I try to access this array:
fav[s].favName = bufr;
I get a NullPointerException. bufr does contain data. I have tracked it down to accessing the array as the following code:
fav[s].favName = "";
also produces a NullPointerException.
I have searched high and low for some indication as to whether or not what I am doing is allowed but cannot find anything.
I suppose my questions are:
Are you allowed to create an array of a class object?
If so, how do you refer to that array?
I know I could do this using five separate arrays of the variables but I feel that putting them into a class gives a better structure and is more elegant (I like elegance).
The problem is that
fav[s]is null.I don’t know about C#, but in Java, you have to initialize the elements of the array individually; you can’t just declare the array and get it automatically filled.
You’re going to have to loop through
favand fill it with newFavouriteobjects.Either assign
fav[s] = new Favourite()the first time you usefav[s], or initialize it all at once by doing