Why does Java throw NullPointerException here?

what you did is called shadowing you shadowed your field x with local variable x. so all you need to do is avoiding this:

int[] x = new int [N]; is wrong, if you want your field to initialize instead of a local variable then you could do something like : x = new int [N]; for more information read this

Leave a Comment