NullPointerException on list.add

2022-09-01 13:36:15

我正在得到一个在的方法。我从调试中知道这不是.我真的很困惑,谢谢。NullPointerExceptionmodelData.add(i, es)esnull

public class EventTableModel extends AbstractTableModel {

    //private int rowCount = 0;
    protected List<EventSeat> modelData;
    private static final int COLUMN_COUNT = 3;
    private Event e;
    Event j = GUIpos.m;
    int i = 1;

public EventTableModel(Event e) {
    this.e = e;
    try {
        System.out.println(modelData);
        for (EventSeat es : e.getEventSeats()) {
            modelData.add(i, es);
            i++;
        }
    } catch (DataException ex) {
        Logger.getLogger(EventTableModel.class.getName()).log(Level.SEVERE, null, ex);
    }

}

答案 1

您需要初始化 List 才能不获取 .NullPointerException

protected List<EventSeat> modelData = new ArrayList<EventSeat>();

答案 2

尝试

protected List<EventSeat> modelData = new ArrayList<EventSeat>(); 

推荐