如何修复'java.lang.IndexOutOfBoundsException:Index:0,Size:0'

时间:2019-05-04 17:16:24

标签: java indexoutofboundsexception

我正在使用Java数组,但出现此异常

java.lang.IndexOutOfBoundsException:Index:0,Size:0

这是我的代码:

public static void main(String[] args) {

    setData();

}

public static void setData() {

    try {

        Product[] products = null;

        int size = getSize();

        for(int i = 0; i < size; i++) {

            products = new Product[i];

            if(products.length > 1) {
                products[i].setId(i);
            }

        }

    } catch(Exception e) {
        System.out.println(e.getMessage());
    }
}

那么,怎么了?

0 个答案:

没有答案
相关问题