This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes i cannot be resolved to a variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "i cannot be resolved to a variable" Watch "i cannot be resolved to a variable" New topic
Author

i cannot be resolved to a variable

enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
Hi guys, I have a problem with a variable which cannot be resolved. The entire code is here:

pastebin.com

but the mistake seems to be here, as this is the area which eclispe marks

for (int i = 5; i < 60; i+= 4); {

track.add(eventErzeugen(144,1,i,100,i));

track.add(eventErzeugen(176,1,127,0,i));

track.add(eventErzeugen(128,1,i,100,i + 2));


eclipse gives 5 errors, saying:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
i cannot be resolved to a variable


My question is: what is wrong with the variable i??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

enri Fsfaed wrote:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
i cannot be resolved to a variable



Can you please give us the errors from the compiler? All that error is saying is ... "there were some compile errors, but it was ran anyway, and now we hit some invalid code"..... It is really not a good idea to run any code that has compile errors.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
The for loop isn't what you think it is - there is one very crucial character that you need to delete.
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
the compiler says

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
i cannot be resolved to a variable
i cannot be resolved to a variable
i cannot be resolved to a variable
i cannot be resolved to a variable
i cannot be resolved to a variable

at minimusikplayer2.MiniMusikPlayer2.los(MiniMusikPlayer2.java:25)
at minimusikplayer2.MiniMusikPlayer2.main(MiniMusikPlayer2.java:9)



I will be thankfull for any support. Now I have to go but I will be back in 5 hours!!!

The problems are in line 25,26 and 27

package minimusikplayer2;

import javax.sound.midi.*;
public class MiniMusikPlayer2 implements ControllerEventListener {


public static void main(String[] args) {
MiniMusikPlayer2 mini = new MiniMusikPlayer2();
mini.los();
}

public void los() {
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();

int[] gewuenschteEvents = {127
};
sequencer.addControllerEventListener(this, gewuenschteEvents);

Sequence seq = new Sequence(Sequence.PPQ, 4);
Track track = seq.createTrack();

for (int i = 5; i < 60; i+= 4); {

track.add(eventErzeugen(144,1,i,100,i));

track.add(eventErzeugen(176,1,127,0,i));

track.add(eventErzeugen(128,1,i,100,i + 2));

}
sequencer.setSequence(seq);
sequencer.setTempoInBPM(220);
sequencer.start();
Thread.sleep(5000);
sequencer.close();
}catch (Exception ex) {ex.printStackTrace();}
}
public void controlChange(ShortMessage event) {
System.out.println("la");
}

public MidiEvent eventErzeugen(int comd, int chan, int one, int two, int tick){
MidiEvent event = null;
try{
ShortMessage a = new ShortMessage();
a.setMessage(comd,chan,one,two);
event = new MidiEvent(a, tick);

}catch (Exception e){}
return event;

}
}
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

As Lester has already mentioned- There's something really crucial 'character' around your for loop statement- You might have to focus around your for loop declaration.

Basically the exception says that its not able to resolve the variable- 'i'- The reason is that it 's not declared in the scope your are using it.

And just a tip: Use- [code] tags for your source code


Mohamed Sanaulla | My Blog
enrique garcia
Greenhorn

Joined: Dec 03, 2010
Posts: 20
omg! the problem was the semicolon after for (int i = 5; i < 60; i+= 4); {

so now it is correct:

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

There you go Enri, So the exception shouldn't be seen anymore. So what happens wen we add a semicolon at the end of the for statement is that- the compiler thinks that it is the end of the for and the following block within {} is not part of the for-loop. So it complains that the variable i was not in scope.
 
Consider Paul's rocket mass heater.
 
subject: i cannot be resolved to a variable
 
Similar Threads
Is it necessary to restart the server each time before running , when using JSTL?
file cannot be resolved to a type
Question on primitive types and collections
SWT: Why do I get "MeterToInch2.java:24:5: non-static variable label cannot be.."?
Need help getting started