Home » Java » java – ArrayList<String[]> won't add more than one item-Exceptionshub

java – ArrayList<String[]> won't add more than one item-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m trying to create an ArrayList of String[], but can’t get it to add more than one item. Ultimately I want to extract the items from the ArrayList and send them to a JTable. The program is 5 separate classes, but here’s the applicable code for this issue:

    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. 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. This is obviously after calling createFood() 3 or four times in order to populate foodData. I can provide additional code if required, it’s just too much to place in whole into this post. Thanks!

How to&Answers:

Every time you call createFood(String[] foodArray) you create a new List instead of just adding the incoming item to the existing list.

Create the ArrayList in a different place and remove the line from the createFood method and it should work fine.

Answer:

you clear out foodData every time you call createFood remove this line:

foodData = new 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));
  }

Answer:

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!