Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I get the error:

TestCounter.java:115: variable counters might not have been initialized counters[i] = new Counter(i);

And I can't figure out how to fix it. I know that my class, Counter, works. Below is my code, if you could have a look at it I would be very happy. This code is wrapped in the main method of a TestCounter class.

  if(success) 
  {  
   Counter[] counters;

   for(int i=0; i<30; i++)
   {
       counters[i] = new Counter(i);
       System.out.println(counters[i].whatIsCounter());
   }
  }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
455 views
Welcome To Ask or Share your Answers For Others

1 Answer

You haven't created the array, you've just declared the variable.

You need to do this:

Counter[] counters = new Counter[30];

or something similar


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...