简体   繁体   English

Java数组索引超出范围异常:-1

[英]Java Array Index Out Of Bounds Exception: -1

Alright , I have a problem which I can't figure out whats wrong. 好吧,我有一个问题,我不知道怎么了。 So I have jList and List . 所以我有jList和List。 I need a function that when I click on item (in jList any item ) and it would change in my label icon , (I'm dealing with images). 我需要一个函数,当我单击项目(在jList中的任何项目中)时,它会在标签图标(我正在处理图像)中发生变化。 It works somehow, it changes my label icon to the image I pick from jList, but it throws me Exception and the program crashes , usually first 2 items cause no errors , third and further items cause it. 它以某种方式工作,将我的标签图标更改为我从jList中选择的图像,但是它引发了异常,程序崩溃了,通常前2个项目没有引起错误,第三个项目导致了它。 After it crashes and throws me bunch of red text , I can still change my icon. 当它崩溃并抛出红色文本后,我仍然可以更改图标。

This is the function where I get the images and add them to List (adding path to them) 这是我获取图像并将其添加到列表(向其添加路径)的功能

private static void getImages(String src) throws IOException {
    //Exctract the name of the image from the src attribute
    int indexname = src.lastIndexOf("/");

    if (indexname == src.length()) {
        src = src.substring(1, indexname);
    }

    indexname = src.lastIndexOf("/");
    String name = src.substring(indexname, src.length());

    //Open a URL Stream
    URL url = new URL(src);
    InputStream in = url.openStream();

    GPath=fPath+name;
    OutputStream out = new BufferedOutputStream(new FileOutputStream( GPath));
    //Im adding to the list string (link to image) here
    imagesListN.add(GPath);

    System.out.println("list size: "+imagesListN.size());


    for (int b; (b = in.read()) != -1;) {

        out.write(b);
    }

    out.close();
    in.close();

}

It adds them normally . 它通常添加它们。 Yes I'm downloading them , that's why I want to see them once they are downloaded. 是的,我正在下载它们,这就是为什么我要在下载它们后查看它们。

This is where I click on jList function. 这是我单击jList函数的地方。

     list.addListSelectionListener(new ListSelectionListener() {

        private int a;

        @Override
        public void valueChanged(ListSelectionEvent arg0) {
            if (!arg0.getValueIsAdjusting()) {
                String h;
                int k;
                k = list.getSelectedIndex();
                System.out.println("List id: "+k);
                a = Main.imagesListN.indexOf(k);
                System.out.println("imagesListN id: "+a);

                h = Main.imagesListN.get(k);
                System.out.println("h : "+h);
                ImageIcon img = new ImageIcon(h);

                imageReview.setIcon(img);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

Here is the jList (name list) . 这是jList(名称列表)。 Exception is at 例外在

a = Main.imagesListN.indexOf(k); 一个= Main.imagesListN.indexOf(k);

it gives me -1 , but the h = Main.imagesListN.get(k); 它给我-1,但是h = Main.imagesListN.get(k); gives me the link I need and gives it to ImageIcon img = new ImageIcon(h); 给我所需的链接,并将其提供给ImageIcon img = new ImageIcon(h); and then imageReview.setIcon(img); 然后是imageReview.setIcon(img); . Label icon changes everytime when I click on item I need. 当我单击所需项目时,标签图标每次都会更改。 Maybe it's not a = Main.imagesListN.indexOf(k); 也许不是= Main.imagesListN.indexOf(k); that I'm looking at , but something gives me -1. 我正在看的东西,但是某些东西让我为-1。 Btw I'm excecuting everything in Thread. 顺便说一句,我正在执行线程中的所有内容。

`public class Crawler extends Thread {

Main main = new Main();

public void run(){
        try {
                main.download();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}`

nothing fancy here. 这里没什么好看的。 Each functions are in there own class , getImages() is in main , listListener is in class Langas (Class where are all buttons,labels , etc. are created nothing else) and Thread well , Thread. 每个函数都在自己的类中,getImages()在main中,listListener在Langas类中(所有按钮,标签等均未创建的Class)以及Thread和Thread。 Also it works fine after everything is downloaded , no exceptions . 一切下载完成后,它也可以正常工作,没有例外。 Error appears durring downloading proccess 在下载过程中出现错误

indexOf api accepts Object as parameter, while get accepts Object as well as primitive types. indexOf api接受Object作为参数,而get接受Object以及原始类型。 So when you call get with primitive type element, it looks for element by an index which you may have found. 因此,当您使用基本类型element调用get时,它将通过您可能已找到的索引来查找element。

But when you do indexOf, you are searching for an object within your list and hence you get -1. 但是当执行indexOf时,您正在列表中搜索对象,因此得到-1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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