Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: "x" cannot be resolved to a variable - error

  1. #1
    Naxix is offline Member
    Join Date
    Jul 2011
    Rep Power

    Default "x" cannot be resolved to a variable - error

    Hello, a wierd problem that i can't seem to understand, came up 2 hours ago, been trying to find out how to fix it,
    but i can't seem to get it right. My problem is that i get a "x cannot be resolved to a variable" error on this code:

    Java Code:
    int bki = AppStart.comboBox.getSelectedIndex();
        	if(bki < 0){
        		int bkp = 0;
        		int bkp = (AppStart.BundkortP[bki]);
    but if i did this:

    Java Code:
    int bki = AppStart.comboBox.getSelectedIndex();
    int bkp = (AppStart.BundkortP[bki]);
    It gives no error, but it will give me an error if nothing is selected in the comboBox yet,
    So i thought that the "if" statement could fix my problem just fine, but i guess i was wrong.

    Oh, and the error is on the line when i try to plus two together, like:

    Java Code:
    number = bkp + bkp;
    If what i have read is true, it's something about the variable bkp isn't being passed down to the "number = bkp + bkp;" line.


  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: "x" cannot be resolved to a variable - error

    Java Code:
    int bki = AppStart.comboBox.getSelectedIndex();
    There are two different things going on on this line. First "int bki" declares the variable bki to be of type int. Secondly, "bki = AppStart.comboBox.getSelectedIndex();" assigns a value to the variable bki - the value is whatever the right hand side evaluates to.

    Assignments like this last until you assign something else to the variable. The is fairly straightforward.

    Declarations only last for the scope of the declaration. After the variable goes out of scope the compiler will completely forget about it and anything that was assigned to it. A local variable declared in a block - code inside a { ... } - has scope only until the end of the block. Once you reach the matching } the variable will no longer be known about and if you use it, you will get a "cannot be resolved" message.

    Mostly you only declare a variable once, as it can get difficult when there are multiple declarations to know which one is "in scope" at some later point in the program. Each declaration creates a new variable and using the same text for them is confusing. An exception is made in the case of commonly used variables like loop counters and the like where it is clear from the code when you have finished using a variable: it is quite safe, and not confusing to use ndx as a variable in a number of for loops. (*)

    The variable bkp should be declared just once - before and in the same block as all the intended use of it. Basically at the same place you declare bki. It has to be initialised (assigned a value) before you can use it, but there is no reason, in general, to give it a value until you know what value it should have.

    Java Code:
    int bki = AppStart.comboBox.getSelectedIndex();
    int bkp;
    if(bki < 0) {
        bkp = 0;
    } else {
        bkp = (AppStart.BundkortP[bki]);
    System.out.println(bkp + bkp);

    (*) For loops being (the only?) exception to the scope rule described above. In "for(int ndx=0;ndx<42;ndx++){...}", the scope of ndx includes the block that follows.
    martensd likes this.

Similar Threads

  1. Error "variable is never read"
    By Phitur in forum New To Java
    Replies: 6
    Last Post: 09-07-2010, 06:30 AM
  2. Error - "The import org.junit cannot be resolved"
    By Jessaurum in forum New To Java
    Replies: 7
    Last Post: 11-03-2009, 04:20 AM
  3. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 10:51 PM
  4. Eclipse "import not resolved" package problem?
    By spamsickle in forum New To Java
    Replies: 3
    Last Post: 08-24-2009, 12:44 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts