amazingCarrotSoup amazingCarrotSoup - 1 month ago 11
Java Question

class constructor variable error message "Cannot resolve symbol" why?

In the code below I call a constructor called

TestInterface
within the
TestInterface class


one of the constructor parameters is the
int
array
sortedDecending


I try to use the
sortedDecending
array in another class method lower in the code. I also turn the sortedDecending into a instance variable.

The code looks right to me but the
sortedDecending
variable is in red and I am getting the error message "Cannot resolve symbol sortedDecending" What am I doing wrong?

public class TestInterface {

public TestInterface(int[] sortedDecending, int[] sameNum,
int[] sortedAssending, int[] randomNum){
this.sortedDecending = sortedDecending;
int arraySize = sortedDecending.length;
System.out.println("Running test");
long testStartTime = System.currentTimeMillis();
}

public void runTest(){
//sortedDecending tests
System.out.println("starting tests for sorted Decending Array");
long sortedDecendingBubbleSortStartTime = System.currentTimeMillis();
BubbleSort sortedDecendingTest = new BubbleSort();
long sortedDecendingBubbleSortEndTime = sortedDecendingTest.bubbleSort(sortedDecending);

}


}

Answer

Your issue is this line here:

 this.sortedDecending = sortedDecending;

your class doesn't have a variable called sortedDescending. You need to add this to your Interface class. Currently it's in your constructor, and isn't a member variable. :

int[] sortedDecending;

So you should make your code look like this:

public class TestInterface {
//add this line
int[] sortedDecending;
public TestInterface(int[] sortedDecending, int[] sameNum,
                 int[] sortedAssending, int[] randomNum){
    this.sortedDecending = sortedDecending;
    int arraySize = sortedDecending.length;
    System.out.println("Running test");
    long testStartTime = System.currentTimeMillis();
}

public void runTest(){
    //sortedDecending tests
    System.out.println("starting tests for sorted Decending Array");
    long sortedDecendingBubbleSortStartTime = System.currentTimeMillis();
    BubbleSort sortedDecendingTest = new BubbleSort();
    long sortedDecendingBubbleSortEndTime = sortedDecendingTest.bubbleSort(sortedDecending);

}