Java - NullPointerException after initializing array and when testing array with class method

后端 未结 4 1238
借酒劲吻你
借酒劲吻你 2021-01-13 10:26

I\'ve been trying to initialize an array and then test its values with a class method. I have initialized the array and have already tested it successfully within the constr

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-13 10:41

    In the constructor, you created a local array with the same name as the class member grid which is being shadowed by the local grid array and that is the reason for the null pointer exception since the class member was never initialized.

    Simply change:

     boolean[] grid = new boolean[sides]
    

    to

     grid = new boolean[sides]
    

    This will ensure that you access the class member you want.

提交回复
热议问题