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

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

java - Array variable "might not have been initialized"

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

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

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...