0

my friend has programmed a Breakout game in java. I know C++, which tranfers relatively well to java.

I had a problem when trying to insert a MultiBall brick.

Here are the relevent bits of my function:

private Balle[] balle;
public BriqueMultiBalle(Balle[] bal) {
        super();
        balle = bal;
        SCORE = 100;
    }
public void touched() {
        visible = false;
        balle[Balle.getNInstance()].makeVisible();
    }

I get no error but I found out when debugging that balle corresponds to a null pointer. I tried using these different declarations, however, none of them worked:

1.

    public BriqueMultiBalle(Balle[] bal) {
                super();
                for(int i = 0; i < 6; i++)
                    {
                         balle[i] = bal[i];
                    }
                SCORE = 100;
            }

2.

public BriqueMultiBalle(Balle[] bal) {
            super();
            balle = new Balle[](bal);
            SCORE = 100;
        }

However, these methods do not work.

Thanks,

Ghi102

2
  • 1
    Which constructor are you calling, the default, or the one where you pass in a Balle[]? If the latter, are you sure you're passing in an initialized Balle[]? Commented Jun 20, 2014 at 19:28
  • How are you creating the Balle[] that you're passing in to the constructor? Perhaps you can show us the code where you do this? Commented Jun 20, 2014 at 19:44

1 Answer 1

1

You are getting a null pointer on balle because you never initialize the array, you leave it as

private Balle[] balle;

Try initializing you code

balle = new Balle[bal.length];
for(int i = 0; i < bal.length; i++){
        balle[i] = bal[i];
}

Here's an example I wrote using an int array. Same concept, just apply it to your object.

public static void main(String[] args) throws Exception {

    int[] arrayInts;
    int[] originalInts = { 1, 2, 3, 4, 5 };

    arrayInts = new int[originalInts.length];

    for(int i = 0; i < originalInts.length; i++){
        arrayInts[i] = originalInts[i];
    }

    originalInts[0] = 10;
    for (int i : arrayInts) {
        System.out.println(i);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.