Java 找不到数组

Java 找不到数组,java,processing,Java,Processing,我在第110行得到错误“找不到任何命名坐标”: System.out.println(coordinates[k][l]); 尝试运行此操作时: import TUIO.*; TuioProcessing tuioClient; int cols = 15, rows = 10; boolean[][] states = new boolean[cols][rows]; int videoScale = 50; // these are some helper variables wh

我在第110行得到错误“找不到任何命名坐标”:

 System.out.println(coordinates[k][l]); 
尝试运行此操作时:

import TUIO.*;
TuioProcessing tuioClient;

int cols = 15, rows = 10;
boolean[][] states = new boolean[cols][rows];
int videoScale = 50;

// these are some helper variables which are used
// to create scalable graphical feedback
int x, y, i, j;
float cursor_size = 15;
float object_size = 60;
float table_size = 760;
float scale_factor = 1;
PFont font;

boolean verbose = false; // print console debug messages
boolean callback = true; // updates only after callbacks



void setup(){
  size(500,500);
noCursor();

  noStroke();
  fill(0);

  // periodic updates
  if (!callback) {
    frameRate(60); //<>//
    loop();
  } else noLoop(); // or callback updates 

  font = createFont("Arial", 18);
  scale_factor = height/table_size;

  // finally we create an instance of the TuioProcessing client
  // since we add "this" class as an argument the TuioProcessing class expects
  // an implementation of the TUIO callback methods in this class (see below)
  tuioClient  = new TuioProcessing(this);

}
void draw(){
  // Begin loop for columns
  for (int k = 0; k < cols; k++) {
    // Begin loop for rows
    for (int l = 0; l < rows; l++) {

      // Scaling up to draw a rectangle at (x,y)
      int x = k*videoScale;
      int y = l*videoScale;

      fill(255);
      stroke(0);

      String[][] coordinates = new String[cols][rows]; 

for (int i = 0; i < cols; i++) {
  for (int j = 0; j < rows; j++) { 

 coordinates[i][j] = String.valueOf((char)(i+65)) + String.valueOf(j).toUpperCase();

  }
}

      //check if coordinates are within a box (these are mouse x,y but could be fiducial x,y)
      //simply look for bounds (left,right,top,bottom)
      if( (mouseX >= x &&  mouseX <= x + videoScale) && //check horzontal
          (mouseY >= y &&  mouseY <= y + videoScale)){
        //coordinates are within a box, do something about it
       System.out.println(coordinates[k][l]); 
        //you can keep track of the boxes states (contains x,y or not) 
        states[k][l] = true;

        if(mousePressed) println(k+"/"+l);

      }else{

        states[k][l] = false;

      }


      rect(x,y,videoScale,videoScale); 
    }
  }

   textFont(font,18*scale_factor);
  float obj_size = object_size*scale_factor; 
  float cur_size = cursor_size*scale_factor; 

  ArrayList<TuioObject> tuioObjectList = tuioClient.getTuioObjectList();
  for (int i=0;i<tuioObjectList.size();i++) {
     TuioObject tobj= tuioObjectList.get(i);
     stroke(0);
     fill(0,0,0);
     pushMatrix();
     translate(tobj.getScreenX(width),tobj.getScreenY(height));
     rotate(tobj.getAngle());
     rect(-obj_size/2,-obj_size/2,obj_size,obj_size);
     popMatrix();
     fill(255);
     text(""+tobj.getSymbolID(), tobj.getScreenX(width), tobj.getScreenY(height));
     System.out.println(tobj.getSymbolID ()+ " " + tobj.getX());

     if( ( tobj.getX()>= x &&  tobj.getX() <= x + videoScale) && //check horzontal
          (tobj.getY() >= y &&  tobj.getY() <= y + videoScale)){
        //coordinates are within a box, do something about it
       System.out.println(coordinates[k][l]); 


   }

}
}
// --------------------------------------------------------------
// these callback methods are called whenever a TUIO event occurs
// there are three callbacks for add/set/del events for each object/cursor/blob type
// the final refresh callback marks the end of each TUIO frame
// called when an object is added to the scene

/* void addTuioObject(TuioObject tobj) {
  if (verbose) println("add obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY()+" "+tobj.getAngle());
}

 // called when an object is moved
void updateTuioObject (TuioObject tobj) {
  if (verbose) println("set obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+") "+tobj.getX()+" "+tobj.getY());
}

// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {
  if (verbose) println("del obj "+tobj.getSymbolID()+" ("+tobj.getSessionID()+")");
}
*/

// --------------------------------------------------------------
// called at the end of each TUIO frame
void refresh(TuioTime frameTime) {
  if (verbose) println("frame #"+frameTime.getFrameID()+" ("+frameTime.getTotalMilliseconds()+")");
  if (callback) redraw();
}
有人知道为什么吗?我是否应该以不同的方式声明它,使它在任何地方都可以访问


感谢您的帮助

您在第一个嵌套for循环中声明了
坐标
变量,因此它仅在该循环执行期间在范围内。每次循环迭代时都会重新创建它

但在循环退出后,您试图访问该变量。此时,
坐标
变量超出范围,因此会出现该错误

您必须重构代码,以便在访问它时,
坐标
在范围内

换句话说,这是行不通的:

for(int i = 0; i < 10; i++){
   String test = "xyz";
}

println(test);
或者这个:

String test = "abc";
for(int i = 0; i < 10; i++){
   test = "xyz";
}

println(test);
String test=“abc”;
对于(int i=0;i<10;i++){
test=“xyz”;
}
println(测试);

您采取哪种方法取决于您希望代码做什么。

我们没有看到代码中行号的好处。如果您正在引用它们,请为我们指出具体的行。抱歉,我添加了行。我在第二个示例中对其进行了修改,但现在我在同一行中得到了错误:“ArrayOutOfBoundException:15”@MatthiasGrahamSlick这是一个单独的问题。您需要真正了解代码在做什么:数组的大小是多少?为什么要访问一个比这个大的索引?如果你仍然被困住了,发布一个更新的(尽量保持小,就像我的例子)作为一个新问题。
for(int i = 0; i < 10; i++){
   String test = "xyz";
   println(test);
}
String test = "abc";
for(int i = 0; i < 10; i++){
   test = "xyz";
}

println(test);