Marcos Guala
Marcos Guala

Reputation: 11

How to resolve "unreported exception AWTException ; must be caught or declared to be thrown". Robot instance

I have the error "unreported exception AWTException ; must be caught or declared to be thrown" instantiating a class that contain methods with mouse and key movements using Robot. I tried with try catch In the instance but the "click" doesnt work this way, what is the problem how to solve it?

package Ventanas;

    enter code here

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class Sel {

    Robot robot = new Robot();

    public void apos() throws AWTException {

        //mouseMv(1408, 1001);  
        //leftClick(); 
        mouseMv(1383, 216);
        leftClick();
        //mouseMv(1408, 1001);
        //leftClick(); 
    }

    public Sel() throws AWTException {

        robot.setAutoDelay(40);
        robot.setAutoWaitForIdle(true);
    }

    public void leftClick() throws AWTException {

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.delay(200);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(200);
    }

    public void mouseMv(int x, int y) throws AWTException {

        robot.mouseMove(x, y);
    }

    public void abrirFavoritos() throws AWTException {
        //1408 999
        try {
            mouseMv(1408, 999);
            leftClick();
        } catch (NullPointerException e) {
            System.out.println(e);
        }

    }

}

-----------------------------------------------------------------------

//Another class
    private void IniciarActionPerformed(java.awt.event.ActionEvent evt) {                                        

            Metodos a = new Metodos();

            Sel s = new Sel(); //Here is the error
    }

Upvotes: 1

Views: 1373

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201437

By using a try-catch like

try {
    Sel s = new Sel();
    // ...
} catch (AWTException ae) {
    ae.printStackTrace();
}

Or modifying the signature of this method to also throw the exception. That is, change

private void IniciarActionPerformed(java.awt.event.ActionEvent evt)

to

private void IniciarActionPerformed(java.awt.event.ActionEvent evt) throws AWTException

Upvotes: 1

Related Questions