Java变量不能被解析或不是字段。

时间:2021-03-09 20:53:18

I'm getting a "Cannot be resolved or is not a field" error on a variable in one of my Java classes, and I don't understand why... I've had a look online, but can't find anything that really explains why I'm getting it. The error is happening in the following for loop:

在我的Java类中,我在一个变量上得到了一个“不能解决的或者不是一个字段”的错误,我不明白为什么……我上网看了一遍,但找不到任何能解释我为什么得到它的原因。该错误发生在以下for循环中:

int i;
getFilterConditions();
for(i = 0; i < sitesToBeFiltered.size(); i++){
    if(sitesToBeFiltered.get(i) == filter1Value){
        Gui.displayFilteredOutput.append("\n");
    Gui.displayFilteredOutput.append("EID: [" + sitesToBeFiltered.get(i) + ", " + applicationsToBeFiltered.get(i) + ", " + IDsToBeFiltered.get(i) + "]. ");
    Vector3Double filteredEntityPosition = 
    Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + sitesToBeFiltered.get(i).positionsToBeFilteredX.get(i));
    }
}

It's being generated on the positionsToBeFilteredX.get(i) variable at the end of the for loop. I have defined that variable as a global variable at the top of the class using the line:

它是由positionsToBeFilteredX.get(i)在for循环的末尾生成的。我已经将变量定义为在类顶部的全局变量,使用该行:

public static ArrayList<Double> positionsToBeFilteredX = new ArrayList<Double>();

To explain what I'm trying to do here:

为了解释我在这里要做的事情:

I have a program that is reading the PDUs that are being sent/ received over a network, and storing both the PDUs themselves, and information held by each of the PDUs in a number of ArrayLists. What I'm trying to do with this code, is to take a value entered by the user on a form (stored in the filter1Value integer variable), and check whether that value is equal to any of the elements in a particular ArrayList (sitesToBeFiltered).

我有一个程序,它读取通过网络发送/接收的pdu,并存储pdu本身,以及每个pdu在许多arraylist中保存的信息。我尝试使用这个代码,是获取用户在表单上输入的值(存储在filter1Value integer变量中),并检查该值是否等于特定ArrayList中的任何元素(sibefilter)。

So, I am looping through the sitesToBeFiltered ArrayList, and checking every element to see whether it is exactly equal to the value of filter1Value. If it is, I am then appending some text about the matching ArrayList element to a JTextArea (displayFilteredOutput).

因此,我将遍历sibefilter ArrayList,并检查每个元素,看看它是否完全等于filter1Value的值。如果是,我将在JTextArea (displayFilteredOutput)中附加一些关于匹配的ArrayList元素的文本。

One of the things I want to add to the JTextArea is the X position of the matching element (which was added to the positionsToBeFilteredX when it was found that that the element matched the user's search criteria.

我想要向JTextArea添加的一个东西是匹配元素的X位置(当发现该元素与用户的搜索条件相匹配时,它被添加到positionsToBeFilteredX中)。

So what I'm trying to do with the last line of code is to append the X coordinate (stored in an array of X coordinates) of the matching element in the sitesToBeFiltered ArrayList, to the displayFilteredOutput JTextArea, but for some reason, I'm getting this "cannot be resolved, or is not a field" compile error on the variable.

所以我想做的最后一行代码是附加X坐标(X坐标存储在一个数组)的匹配的元素sitesToBeFiltered ArrayList,displayFilteredOutput JTextArea,但出于某种原因,我得到这个“无法解决,或者不是一个领域”编译错误的变量。

Can anyone explain to me why this is? I suspect that I am not referencing the X coordinate of the element matching the filter value correctly, but I'm not sure how I should be doing this... Could someone point me in the right direction?

有人能解释一下这是为什么吗?我怀疑我不是在引用匹配过滤器值的元素的X坐标,但是我不确定我应该怎么做……有人能告诉我正确的方向吗?

2 个解决方案

#1


2  

Your code is written as though positionsToBeFiltered is a field in the object returned by sitesToBeFiltered.get(i). Evidently it isn't.

您的代码是这样写的,就好像在sibefilter. .get(i)所返回的对象中,positionstobefilter是一个字段。显然不是。

#2


1  

Should have seen it sooner: the issue was because I was trying to assign the value to a variable that was of an incompatible type. To solve this, I just needed to append the value to the JTextArea in the Gui without assigning it to a variable first: i.e. instead of writing

应该早点看到它:问题是,因为我试图将值赋给一个不兼容类型的变量。为了解决这个问题,我只需要将值附加到Gui中的JTextArea,而无需先将其赋值给一个变量:即,而不是写入。

Vector3Double filteredEntityPosition = Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionsToBeFiltered.get(i);

I just needed to write:

我只需要写:

Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionToBeFiltered.get(i) + "]. ");

#1


2  

Your code is written as though positionsToBeFiltered is a field in the object returned by sitesToBeFiltered.get(i). Evidently it isn't.

您的代码是这样写的,就好像在sibefilter. .get(i)所返回的对象中,positionstobefilter是一个字段。显然不是。

#2


1  

Should have seen it sooner: the issue was because I was trying to assign the value to a variable that was of an incompatible type. To solve this, I just needed to append the value to the JTextArea in the Gui without assigning it to a variable first: i.e. instead of writing

应该早点看到它:问题是,因为我试图将值赋给一个不兼容类型的变量。为了解决这个问题,我只需要将值附加到Gui中的JTextArea,而无需先将其赋值给一个变量:即,而不是写入。

Vector3Double filteredEntityPosition = Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionsToBeFiltered.get(i);

I just needed to write:

我只需要写:

Gui.displayFilteredOutput.append("Location in DIS coordinates: [" + positionToBeFiltered.get(i) + "]. ");