Null pointer exception for Array of Objects

后端 未结 4 910
长情又很酷
长情又很酷 2020-12-22 09:55

I am new to using arrays of objects but can\'t figure out what I am doing wrong and why I keep getting a Null pointer exception. I am trying to create an Theatre class with

4条回答
  •  庸人自扰
    2020-12-22 10:10

    replace

    arrayOfSpotlights[i].turnOn();
    

    with

    arrayOfSpotLights[i] = new Spotlight();
    arrayOfSpotlights[i].turnOn();    
    

    The line

    arrayOfSpotlights =  new spotlight[N];
    

    will create an array of spotlights. It will however not populate this array with spotlights.

提交回复
热议问题