首页 > 解决方案 > 尝试访问类中定义的 ArrayList 时出现 NoSuchFieldError

问题描述

我正在尝试访问类来宾中定义的 ArrayList。但是,尝试这样做时会出错。

import java.util.ArrayList;

static class guess{
    ArrayList<Integer> box;
    ArrayList<Integer> index;
    int num;
    ArrayList <Boolean> valid;
}
...
guess attempt = new guess();
attempt.box = new ArrayList<Integer>();
attempt.index = new ArrayList<Integer>();
attempt.valid = new ArrayList<Boolean>();

尝试“attempt.box.add(某个值)”时出现错误

任何建议将不胜感激。

标签: javaarraylist

解决方案


这应该工作

import java.util.ArrayList;

    public class Main {
        static class guess{
            ArrayList<Integer> box;
            ArrayList<Integer> index;
            int num;
            ArrayList <Boolean> valid;
        }

        public static void main(String[] args) {
            guess attempt = new guess();
            attempt.box = new ArrayList<Integer>();
            attempt.index = new ArrayList<Integer>();
            attempt.valid = new ArrayList<Boolean>();
            attempt.box.add(22);
        }
    }

推荐阅读