简体   繁体   中英

Unhandled Exception Type Java

I am using Sikuli with Java to create a small automation tool. I am having trouble with this Unhandled Exception error. I am trying to pass a method I created to the GUI actionPerformed method.

package mission;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

import tools.ChooseMission;

public class Story {

    public static  void runStoryMissions(int chapter, int stage) throws FindFailed, InterruptedException {
        Screen screen = new Screen(); 
        ChooseMission.ChooseChapter(screen,chapter);
        ChooseMission.ChooseStage(screen, stage);
        int dailyBiometricCount = ChooseMission.dailyBiometricCount(screen); 

        Pattern start = new Pattern("img/chapters/start.png"); 
        Pattern replay = new Pattern("img/chapters/replay.png"); 
        Pattern next = new Pattern("img/chapters/next.png"); 
        Pattern mission_finish_bar = new Pattern("img/chapters/mission_finish_bar.png");


        screen.click(start); 
        System.out.println("The mission has started.");


        Thread.sleep(2000);
        while (find(screen, mission_finish_bar) == false) {

            System.out.println("Still playing the mission...");

        }
        if (screen.exists(mission_finish_bar) != null){
            System.out.println("The mission has finished.");

        }
        System.out.println("Wait for 5 Seconds");
        Thread.sleep(5000);
        System.out.println("Click repeat button");
        screen.click(replay); 

    }

Here is the code for my actionPerformed listener button:

btnStartMissions.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            int chapter = Integer.parseInt(txtFldChapter.getText()); 
            int stage = Integer.parseInt(txtFldStage.getText());

            System.out.println("Chapter #: " + chapter);
            System.out.println("Stage #: " + stage);


            try {
                Story.runStoryMissions(chapter, stage);
            } catch (FindFailed e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

There is a error at Story. runStoryMissions(chapter,stage) it says: Unhandled Exception type FindFailed and InterruptedException

Stack Trace:

Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
    at mission.Story.runStoryMissions(Story.java:12)
    at GuiFrame1$2.actionPerformed(GuiFrame1.java:94)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
    at java.awt.Robot.checkNotDispatchThread(Unknown Source)
    at java.awt.Robot.waitForIdle(Unknown Source)
    at org.sikuli.script.Mouse.move(Mouse.java:345)
    at org.sikuli.script.Mouse.move(Mouse.java:318)
    at org.sikuli.script.Mouse.init(Mouse.java:59)
    at org.sikuli.script.Screen.initScreens(Screen.java:89)
    at org.sikuli.script.Screen.<clinit>(Screen.java:58)
    ... 38 more

Okay, I found the answer to my problem if anyone is interested.

  • Sikuli uses the java.awt features so scripts cannot implement and use Swing elements.
  • Java AWT Robot actions cannot be called froma Java swing container.

Solution is to create a new Thread:

        new Thread(() -> {
            try {
                Story.runStoryMissions(chapter, stage);
            } catch (FindFailed | InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }).start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM