0
public void method1 (int x) {
    if (x == 1) {
        try {
            Robot r = new Robot();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        r.mouseMove(50,50);

But this gives r cannot be resolved. Any ideas? =/ Thanks a lot

Jonas
  • 121,568
  • 97
  • 310
  • 388

2 Answers2

4
public void method1 (int x) {
if (x == 1) {
    try {
        Robot r = new Robot(); // R is defined in the try block. It is not visible outside of this block.
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    r.mouseMove(50,50); // R is not defined anymore here

Replace it with

public void method1 (int x) {
if (x == 1) {
    try {
        Robot r = new Robot();
        r.mouseMove(50,50);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
2

It should be:

public void method1 (int x) {
 Robot r = null;
if (x == 1) {

    try {
         r = new Robot();
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       return;
    }
    r.mouseMove(50,50);
Mubin
  • 4,192
  • 3
  • 26
  • 45