0

Hello daniweb,

I am new to Java and to this forum! I am trying to put some objects into an array like this..

MovingPlatform[] platforms;
platforms = new MovingPlatform[1];

The problem is that I want to be able to access variables of objects within my array. I have tried this but I am getting "java.lang.NullPointerException" errors:

platforms[0].name = "platform1";

I am sure this is a simple syntax problem. Can anyone offer some insight?

Just in case here is what my object class looks like:

public class MovingPlatform {
    int angle;
    int speed;
    int distance;
    Room room;
    String name;
    int dir;
    int progress;
}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by RichardCrickets
1

Here you have declared the type and size of the array

MovingPlatform[] platforms;
platforms = new MovingPlatform[1];

but you must still create an instance of each element

platforms[0] = new MovingPlatform();
0

you must still create an instance of each element

platforms[0] = new MovingPlatform();

Doh! I knew it was something like that.

Thanks Ezzaral!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.