-2

I have an ArrayList that has many objects. For example:

ArrayList<Supplier> a = new ArrayList<>();

As you see it's an ArrayList from type Supplier which is a class that has 4 attributes (name, company, address, phone number)

public class Supplier {
    public String name;
    public String company;
    public String address;
    public String phone_no;
}

I want to write JOptionPane.showInputDialog statement that shows the list elements in a drop down list to choose one of them and after taking the choice I want to divide that choice into 4 attributes again from the same class.

This is my code, but it didn't work:

String []choices = null;
for(int i = 0; i < a.size(); i++) {
    choices[i] = a.get(i).toString();
}
JOptionPane.showInputDialog(null, "Choose supplier of the product !!", "Select Supplier", JOptionPane.QUESTION_MESSAGE,null, choices, "----");
Tom
  • 16,842
  • 17
  • 45
  • 54
MHM
  • 19
  • 10
  • can you please explain in a more clear way ? – Vishal Gajera Dec 08 '15 at 11:17
  • http://stackoverflow.com/questions/6555040/multiple-input-in-joptionpane-showinputdialog - Multiple fields – Ewald Dec 08 '15 at 11:17
  • Well again.... this array list take many objects of class `Supplier` .. well... i want to print object elements to be showed in drop down list ... for example like this: `John Pepsi London 9584841` and so on. – MHM Dec 08 '15 at 11:19

3 Answers3

1

The array you are using is not initialized causing NullPointerException.

Use the below code

String[] choices = a.toArray();
JOptionPane.showInputDialog(null, "Choose supplier of the product !!",
            "Select Supplier", JOptionPane.QUESTION_MESSAGE, null, choices,
            "----");

Also update the toString() method in Supplier class as below:

@Override
public String toString() {
    return "Supplier [name=" + name + ", company=" + company + ", address="
            + address + ", phone_no=" + phone_no + "]";
}

Keep the fields that you need to show in the dropdown.

Vivek Singh
  • 2,047
  • 11
  • 24
1

Try to use something like this:

  ArrayList<Supplier> a = new ArrayList<>();

 String[] choices = a.toArray();

  String input = (String) JOptionPane.showInputDialog(null, "Choose now...",
    "Choose supplier of the product !!", JOptionPane.QUESTION_MESSAGE, null,                                                                     
    choices, // Array of choices
    choices[1]); // Initial choice
Abdelhak
  • 8,299
  • 4
  • 22
  • 36
  • The best solution ... thanks :) – MHM Dec 08 '15 at 11:37
  • @MHM This "best solution" is neither usable nor compilable. – Tom Dec 08 '15 at 12:20
  • Btw: what I meant with "not usable or compilable": the latest change with `String[] choices = a.toArray();` won't work. – Tom Dec 08 '15 at 14:50
  • @Tom its edited in the post, before edit there were normal array of strings so i apply this idea on my code posted in the answer and its work. – MHM Dec 08 '15 at 15:20
  • @MHM Yes, the older version was working, but this is one is now broken :P. – Tom Dec 08 '15 at 20:20
0

Based on @Abdelhak answer i tried this and worked for me:

First assume that array list has already some values so we use iterator to loop faster in array list then i used for loop inside iterator to initialize array of strings.

        Iterator<Supplier> i = a.iterator();
        String []choices = new String[a.size()];
        while(i.hasNext())
        {
            for(int j = 0; j < a.size(); j++)
            {
                Supplier p = i.next();
                choices[j] = p.getName() + " " + p.getCompany() + " " + p.getAddress() + " " + p.getPhone_no();
            }
        }
        JOptionPane.showInputDialog(null, "Choose supplier of the product !!", "Select Supplier", JOptionPane.QUESTION_MESSAGE,null, choices, "----");
MHM
  • 19
  • 10
  • @Abdelhak please check this – MHM Dec 08 '15 at 11:45
  • does this code give you some issue – Abdelhak Dec 08 '15 at 12:59
  • I can't understand why are add these lines: Supplier p = i.next(); choices[j] = p.getName() + " " + p.getCompany() + " " + p.getAddress() + " " + p.getPhone_no(); – Abdelhak Dec 08 '15 at 13:31
  • @Abdelhak Because his `Supplier` class doesn't has a proper `toString` implementation. That is why he chose this way to get a String representation of his `Supplier` instance. – Tom Dec 08 '15 at 13:56
  • @Abdelhak well, iterator is a good thing to loop faster in array list .. it is more faster than normal for loop ... and we just use for loop to initialize the array of strings .... its working perfect with me :) ... the same idea of your solution. – MHM Dec 08 '15 at 15:13
  • @MHM There is no significant performance difference between these two approaches: [Which is more efficient, a for-each loop, or an iterator?](http://stackoverflow.com/q/2113216). Edit: oh you wrote "normal" for loop. Well, you're using an `ArrayList`, so there is also no difference here, but I would suggest to use the enhanced `for` loop instead. Easier to write and to read. – Tom Dec 08 '15 at 20:23