未处理的异常类型Java

时间:2017-02-14 05:23:57

标签: java exception sikuli

我正在使用Sikuli和Java来创建一个小型自动化工具。我遇到此Unhandled Exception错误。我试图将我创建的方法传递给GUI actionPerformed方法。

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); 

    }

以下是actionPerformed侦听器按钮的代码:

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();
            }

        }
    }

Story有错误。 runStoryMissions(章节,阶段)它说:未处理的异常类型FindFailed和InterruptedException

堆栈追踪:

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

1 个答案:

答案 0 :(得分:1)

好的,如果有人有兴趣,我找到了问题的答案。

  • Sikuli使用java.awt功能,因此脚本无法实现和使用Swing元素。
  • Java AWT机器人动作不能从Java swing容器调用。

解决方案是创建一个新的线程:

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