[英]ArrayList<String[]> won't add more than one item
I'm trying to create an ArrayList of String[], but can't get it to add more than one item.我正在尝试创建一个 String[] 的 ArrayList,但无法让它添加多个项目。 Ultimately I want to extract the items from the ArrayList and send them to a JTable.
最终我想从 ArrayList 中提取项目并将它们发送到 JTable。 The program is 5 separate classes, but here's the applicable code for this issue:
该程序是 5 个独立的类,但这里是此问题的适用代码:
static JComboBox<String> foodChoice;
DefaultTableModel foodList;
static String[] newFood;
static List<String[]> foodData;
JTextField newFoodText, portionText, carbsText;
public Main() {
void createFood() {
String[] foodProperties = new String[3];
foodProperties[0] = newFoodText.getText();
foodProperties[1] = portionText.getText();
foodProperties[2] = carbsText.getText();
Main.createFood(foodProperties);
}
static void createFood(String[] foodArray) {
foodData = new ArrayList<String[]>();
foodData.add(foodArray);
foodChoice.addItem(foodArray[0]);
}
void addFoodToTable() {
String[] s = new String[3];
s = (String[]) foodData.get(foodChoice.getSelectedIndex());
System.out.println(foodData.get(0));
System.out.println(foodData.get(1));
}
addFoodToTable
gets called with a button click.单击按钮即可调用
addFoodToTable
。 So the issue I'm having is that (based on the sysouts) I will get a pointer address to the first entry in the ArrayList, but then a Null Pointer Exception stating that it is out of bounds for Length 0 when it tries to print the second one to console.所以我遇到的问题是(基于 sysouts)我将获得指向 ArrayList 中第一个条目的指针地址,但随后出现一个空指针异常,指出它在尝试打印时超出 Length 0 的范围第二个安慰。 This is obviously after calling
createFood()
3 or four times in order to populate foodData
.这显然是在调用
createFood()
3 或 4 次以填充foodData
。 I can provide additional code if required, it's just too much to place in whole into this post.如果需要,我可以提供额外的代码,将其全部放入这篇文章中太多了。 Thanks!
谢谢!
you clear out foodData
every time you call createFood
remove this line:你清除
foodData
每次通话时间createFood
删除此行:
foodData = new ArrayList();
食物数据 = 新的 ArrayList();
and move the initialization to a static level , like this:并将初始化移动到静态级别,如下所示:
static JComboBox<String> foodChoice;
DefaultTableModel foodList;
static String[] newFood;
static List<String[]> foodData = new ArrayList<String[]>();
JTextField newFoodText, portionText, carbsText;
public Main() {
void createFood() {
String[] foodProperties = new String[3];
foodProperties[0] = newFoodText.getText();
foodProperties[1] = portionText.getText();
foodProperties[2] = carbsText.getText();
Main.createFood(foodProperties);
}
static void createFood(String[] foodArray) {
foodData.add(foodArray);
foodChoice.addItem(foodArray[0]);
}
void addFoodToTable() {
String[] s = new String[3];
s = (String[]) foodData.get(foodChoice.getSelectedIndex());
System.out.println(foodData.get(0));
System.out.println(foodData.get(1));
}
Every time you call createFood(String[] foodArray) you create a new List instead of just adding the incoming item to the existing list.每次调用 createFood(String[] foodArray) 时,您都会创建一个新列表,而不仅仅是将传入的项目添加到现有列表中。
Create the ArrayList in a different place and remove the line from the createFood method and it should work fine.在不同的地方创建 ArrayList 并从 createFood 方法中删除该行,它应该可以正常工作。
Worked like a charm.像魅力一样工作。 Man I don't know how I missed that... I guess when you look at the same problem for too long you miss the obvious.
伙计,我不知道我是怎么错过的......我想当你看同一个问题太久时,你会错过明显的。 Thanks guys!
谢谢你们!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.