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;
}

Recommended Answers

All 2 Replies

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();

you must still create an instance of each element

platforms[0] = new MovingPlatform();

Doh! I knew it was something like that.

Thanks Ezzaral!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.