Error Array Dimension Missing

I keep getting array dimension missing

public static Planet[] readPlanets(String filename) {

    allPlanets = new Planet[];
    In in = new In (filename);
    int nplanets = in.readInt();
    double radius = in.readDouble();
    for (int i = 0; i < allPlanets.length; i++) {
        double pxxPos = in.readDouble();
        double pyyPos = in.readDouble();
        double pxxVel = in.readDouble();
        double pyyVel = in.readDouble();
        double pmass = in.readDouble();
        String pimgFileName = in.readString();
    }
    return allPlanets;
}

Planet has six dimensions, and I have an array of multiple planets

2

2 Answers

You have to declare size of the array before you can use it's length attribute.

For example:

allPlanets = new Planet[6];

When you create an array, you have to specify the size. I strongly suspect you want:

In in = new In(filename);
int nPlanets = in.readInt();
allPlanets = new Planet[nPlanets];

Note that it's odd that you're assigning to a field and returning the reference from the method. It would be more usual to do one or the other, e.g. use a local variable:

Planet[] planets = new Planet[nPlanets];
...

return planets;

And then assign to the field in the calling code:

allPlanets = readPlanets(...);
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest