1

I just can't seem to return the variable data in this method:

public Object[] populateTable(ArrayList<Outlet> outletList, String selection){ 
    Object[] data;
    for(int i = 0; i<outletList.size(); i++){
        if(outletList.get(i).getCity().equalsIgnoreCase(selection)){
            if(outletList.get(i).getStatus().equals("ACTIVE")){

                String bar = outletList.get(i).getBarangay();
                String code = Integer.toString(outletList.get(i).getCode());
                String name = outletList.get(i).getName();

                data = {bar, code, name};                      
            }   
        }               
    }
    return data;
}

Netbeans is saying illegal start of expression. Is there a way to execute this method wherein data (coming from a mysql database transferred to a ArrayList<object> in an interface) is passed to an Object[]? Finding a way to populate JTable rows with data coming from a mysql database.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
beyonchayyy
  • 93
  • 2
  • 12

1 Answers1

3

The error arises here:

data = {bar, code, name};

Instead, construct a new array of Object to hold instances of String:

data = new Object[]{bar, code, name};

Then you can invoke addRow(data) on your DefaultTableModel. A complete example is shown here in TableAddTest#addRow().

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    See also [*Arrays: Creating, Initializing, and Accessing an Array*](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). – trashgod May 03 '16 at 09:29