Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

i cannot be resolved to a variable

 
enrique garcia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21001
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The for loop isn't what you think it is - there is one very crucial character that you need to delete.
 
enrique garcia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
enrique garcia
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omg! the problem was the semicolon after for (int i = 5; i < 60; i+= 4); {

so now it is correct:

 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic