java - How to create a array of an object that requires parameters -


i have code here

final ship[] ships = new ship[3];  for(int x = 0; x < 3; x++) {     ships[x].settext("set ship length: " + (x+1)); // line in particular } 

i getting null pointer exception, think because ships object requires string , int in parameters constructors contains, how supposed create , array of objects each object requires parameters?

change code below..

final ship[] ships = new ship[3];  for(int x = 0; x < 3; x++) {     ships[x].settext("set ship length: " + (x+1)); // line in particular } 

to...

final ship[] ships = new ship[3];  for(int x = 0; x < 3; x++) {     string stringvalue = "set ship length: " + (x+1); //string want in ship object     int intvalue = 0; //int want in ship object..     ship ship = new ship(stringvalue, intvalue);     ships[x] = ship;     ships[x].settext("whatever string heart desires"); // line in particular } 

you making array of type ship, but never assign contents array. therefore when try set text of element in array, trying set text on null value give null pointer exception.

instead need first create object of type ship have shown above , pass in string , integer parameters spoke of in question. add new object array. can access object in array , call methods on object.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -