I am retrieving values from database. I initialized a variable outside the loop. I am using while loop to move one record after other to last using ResultSet object.next(). Inside the while I am assigning the record value to the variable initialized outside while. But It shows variable might not have been initialized error inside while and in succeeding code wherever that variable is used.
If I move them as class variable outside the method too i got error "Non Static variable cannot be initialized from static context".

Re: Variable might not have been initialised 80 80

Then you may have declared the variable outside the loop, but you didn't initialise (i.e. define) it. IOW if you do

String bogus;
while (true) {
  if (bogus.equals("Whatever")) {
    break;
  }
}

(as a contrived example)
you will get that compiler message, whereas with

String bogus = ""; // or even null
while (true) {
  if (bogus.equals("Whatever")) {
    break;
  }
}

you won't. Do you see the difference?

Re: Variable might not have been initialised 80 80

ya.I can. But i am using array.

int i=0; String bogus[i]="";

This statement preceding the while loop also shows error.

Re: Variable might not have been initialised 80 80

Am i did any syntax error in that?
It shows

int i=0; String bogus[i]="";

shows "] expected
; expected"
error.

Re: Variable might not have been initialised 80 80

This is an error:

int i = 0;
String bogus[i]= "";

Perhaps you wanted this?

int i = 0;
String bogus[] = new String[10]
bogus[i]= "";

This line would be an error no matter where you put it:

String bogus[i] = "";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.