【问题标题】:can't added my object to to ArrayList无法将我的对象添加到 ArrayList
【发布时间】:2015-05-24 12:30:52
【问题描述】:

我试图向 ArrayList 添加一个对象? 但我不能whyyyyyy?

ArrayList<Patient> emer; 

Patient p = new Patient(); 

p.setName(jTextField1.getText());
p.setSurname(jTextField2.getText());
p.setGender(jRadioButton1.getText()); } p.setBooldType(jComboBox1.getSelectedItem() + ""); p.setEntarnceDay(jComboBox2.getSelectedItem() + "/" + jComboBox3.getSelectedItem() + "/" + jComboBox4.getSelectedItem()); p.setFee(Integer.parseInt(jTextField8.getText())); 
emer.add(p);

【问题讨论】:

  • 中间的这个花括号是什么??
  • 你得到什么错误?

标签: java swing object arraylist


【解决方案1】:

您尚未初始化您的 emer,可能会收到“局部变量未初始化”错误。

ArrayList<Patient> emer;

你需要用一个新的数组列表实例初始化emer。

ArrayList<Patient> emer = new ArrayList<Patient>();

或者更好:

List<Patient> emer = new ArrayList<Patient>();

【讨论】:

  • 谢谢你的回答我已经初始化了 ArrayList,网络 bean 也没有给我一个错误,但是 arraylist 没有接受任何对象 arrayList 的大小始终是 1,这是我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 2012-04-14
  • 2011-12-24
  • 2016-09-26
  • 1970-01-01
相关资源
最近更新 更多