Java – An array of Android objects

An array of Android objects… here is a solution to the problem.

An array of Android objects

I’m quite a novice in Android programming, and I insist on developing a series of classes.

Only 2 string variables are declared in the sample class. Here’s how I imagine the class to be created:

Exampleclass something = new
Exampleclass();

Exampleclass[] something_else= new
Exampleclass[5];

When trying to assign a string variable to one of the array classes, it crashes. When debugging, I found that a single class has both string values and their names, but their names all display fine, but each array only displays a null value, no strings or names.

Is there any particular way to set or create a class for an array?

Solution

You only initialized the array, not the objects inside, you should use :

Exampleclass[] something_else = new Exampleclass[5];  initialize array
for (int i = 0; i < something_else.length; i++)
    something_else[i] = new something_else();     initialize each object in the array

Related Problems and Solutions