线程“main”中的异常AWT 中的 java.lang.ArrayIndexOutOfBoundsException: 0

发布于 2025-01-01 01:18:15 字数 2779 浏览 6 评论 0原文

这是我现在拥有的代码,它编译得很好,但当我尝试运行它时,出现了上述错误。

import java.awt.*;
import java.awt.event.*;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class GunningBot {

    public static void main(String[] args) throws Exception {

        Robot robot = new Robot();
        Color color1 = new Color(195, 174, 196);

        {
            Rectangle rectangle = new Rectangle(0, 0, 1075, 700);

            {
                BufferedImage image = robot.createScreenCapture(rectangle);
                search:
                for (int x = 0; x < rectangle.getWidth(); x++) {
                    for (int y = 0; y < rectangle.getHeight(); y++) {
                        if (image.getRGB(x, y) == color1.getRGB()) {
                            robot.mouseMove(x, y);
                            break search;
                        }
                    }
                }
            }
        }

        robot.delay(0);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.delay(0);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(15);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.delay(0);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(1500);

        robot.mouseMove(510, 440); //DO THIS BOTTOM LEFT

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(500, 140);  //MOVE TO LEFT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(250);

        robot.mouseMove(500, 370); //DO THIS TOP RIGHT

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(510, 370);  //MOVE TO RIGHT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(250);

        robot.mouseMove(700, 440); //DO THIS RIGHT CORNER MOVE LEFT 5 

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(690, 440);  //MOVE TO LEFT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(250);

        robot.mouseMove(315, 370); //DO THIS LEFT CORNER MOVE RIGHT 5 

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(320, 370);  //MOVE TO RIGHT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);


    }
}

我发现了许多关于过度扩展数组等的示例修复,但我不使用数组。所以我不明白如何修复这个脚本的这个错误。任何帮助都会很棒。

完整的错误是:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 GunningBot.main(GunningBot.java:39)

bufferimage 使用光栅,这可能是问题吗? 矩形只是设置要截图的屏幕矩形。

另外,我正在使用 Notepad++ 编辑代码,并且不知道如何在没有 Eclipse 的情况下设置异常。

This is the code I have now, it compiles fine but gives me the above error when I try to run it.

import java.awt.*;
import java.awt.event.*;
import java.awt.Robot;
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class GunningBot {

    public static void main(String[] args) throws Exception {

        Robot robot = new Robot();
        Color color1 = new Color(195, 174, 196);

        {
            Rectangle rectangle = new Rectangle(0, 0, 1075, 700);

            {
                BufferedImage image = robot.createScreenCapture(rectangle);
                search:
                for (int x = 0; x < rectangle.getWidth(); x++) {
                    for (int y = 0; y < rectangle.getHeight(); y++) {
                        if (image.getRGB(x, y) == color1.getRGB()) {
                            robot.mouseMove(x, y);
                            break search;
                        }
                    }
                }
            }
        }

        robot.delay(0);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.delay(0);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(15);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.delay(0);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(1500);

        robot.mouseMove(510, 440); //DO THIS BOTTOM LEFT

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(500, 140);  //MOVE TO LEFT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(250);

        robot.mouseMove(500, 370); //DO THIS TOP RIGHT

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(510, 370);  //MOVE TO RIGHT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(250);

        robot.mouseMove(700, 440); //DO THIS RIGHT CORNER MOVE LEFT 5 

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(690, 440);  //MOVE TO LEFT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.delay(250);

        robot.mouseMove(315, 370); //DO THIS LEFT CORNER MOVE RIGHT 5 

        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseMove(320, 370);  //MOVE TO RIGHT 5 PXLS
        robot.mouseRelease(InputEvent.BUTTON1_MASK);


    }
}

I have found many example fixes that go on about over extending the Array and so on but I don't use an array. So I do not understand how to fix this error for this script. Any help at all would be amazing.

The full error is:

exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at GunningBot.main(GunningBot.java:39)

bufferimage uses a Raster, could this be the problem?
rectangle is just setting the rectangle of the screen to get a screenshot of.

Also I'm editing the code with Notepad++ and don't know how to set an exception without eclipse.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半透明的墙 2025-01-08 01:18:15

错误在于以下几行:

robot.delay(0);

我不确定您在这里要做什么,并且需要查看 Robot 类才能知道此方法实际上应该做什么。但由于它是 ArrayIndexOutOfBoundsException ,请尝试将参数更改为非零值。例如,尝试这样做:

robot.delay(1);

即使这解决了您的问题,它仍然不是做事的“正确”方法,因为您需要了解此方法期望什么以及它应该做什么,以便您可以通过相应的论证。如果这不能解决您的问题,请向我们展示 Robot 类的代码。

The error is in these lines:

robot.delay(0);

I'm not sure what you're trying to do here and would need to see the Robot class to know what this method is actually supposed to do. But since it's an ArrayIndexOutOfBoundsException, try changing the argument to a non-zero value. For example, try this:

robot.delay(1);

Even if this solves your problem, it still wouldn't be the 'right' way to do things because you need to be aware of what this method expects and what it is supposed to do so that you can pass the argument accordingly. If this doesn't solve your problem, show us the code for the Robot class.

陌上青苔 2025-01-08 01:18:15

这可能不是完整的答案,但是当我得到这个或类似的异常(使用 Eclipse)时,我在 异常

This may not be the full answer, but when I get this or similar exceptions (using eclipse) I set a breakpoint on the exception

手心的温暖 2025-01-08 01:18:15

你说完整的异常堆栈跟踪是:

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 0 在 GunningBot.main(GunningBot.java:39)

但是,您提供的源代码的第 39 行是:

    robot.delay(0);

这不是数组访问/更新,因此 无法抛出ArrayIndexOutOfBoundsException。 (被调用的方法可能会抛出异常,但堆栈跟踪看起来会有所不同。)事实上,您问题中的“GunningBot.java”版本根本没有数组访问/更新。

这意味着什么?

它意味着以下之一:

  • 这不是真正的代码,或者

  • 这不是真正的(完整)堆栈跟踪,或者

  • 您并没有真正运行您认为的代码;例如,您忘记重新编译,或者您的执行和构建类路径不同。

最重要的是,如果您的问题不准确,您将不会得到像样的答案。

You say that the complete exception stacktrace is:

exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at GunningBot.main(GunningBot.java:39)

However, line 39 of the source code that you provided is:

    robot.delay(0);

which is not an array access/update and therefore cannot throw an ArrayIndexOutOfBoundsException. (The called method could throw the exception, but then the stacktrace would look different.) In fact, there are no array accesses / updates at all in the version of "GunningBot.java" in your question.

What does this mean?

It means one of the following:

  • this is not the real code, or

  • this is not the real (complete) stacktrace, or

  • you are not really running the code that you think you are; e.g. you forgot to recompile, or your execution and build classpaths are different.

The bottom line is that if your Question is inaccurate, you won't get decent Answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文