• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

i cannot be resolved to a variable

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;

}
}

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic