title: Fill JTable with 1d-to-2d list, and restrict to property of Object
body: <p>I have a list of elements that in turn are lists of elements:</p>
  
  <pre><code>{{entity1, entity2}, {}, {entity3}, {entity4}, {entity5, entity6, entity7}, {}, ...}
  </code></pre>
  
  <p>I want to convert this list to a JTable. The list is 36 long, which should translate to a table of 6 by 6. Let's assume that the above example is a table of 3x2, the result'd be:</p>
  
  <pre><code>_______________________________
  | entity1 |         | entity3 |
  | entity2 |         |         |
  |_________|_________|_________|
  | entity4 | entity5 |         |
  |         | entity6 |         |
  |         | entity7 |         |
  |_________|_________|_________|
  </code></pre>
  
  <p>Side note, each entity is an object itself (e.g. employee object), and I'd want to output only a specific property of the entities (e.g. <code>.firstName</code> or something similar). Maybe this can be done with <code>.map</code>? But unfortunately I'm not all too familiar with that.</p>
  
  <p>What I got so far, is a loop for the initial list. Let's assume the outer list is called location (i.e. 36 locations) and the inner elements are employees on specific locations. (In practice the entities are in an arraylist, and the locations in an array.) This is untested, but it seems easy enough that it should work? (Please tell me if it's not.)</p>
  
  <pre><code>Location[] locations; // 36 locations
  Location[][] locations2d; // new array 6x6
  for(int x = 0; x &lt; 6; x++) {
    for(int y = 0; y &lt; 6; y++) {
      locations2d[x][y] = locations[6*x+y];
    }
  }
  </code></pre>
  
  <p>But how do I now easily get this <code>2dlocations</code> into my 6x6 JTable, considering I only want the <code>.firstName</code> of the entities in that list?</p>
  
  <hr>
  
  <p>Trying some stuff out, I came up with this:</p>
  
  <pre><code>public Overview() {
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setBounds(100, 100, 450, 300);
          contentPane = new JPanel();
          contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
          setContentPane(contentPane);
          contentPane.setLayout(null);
  
          JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
          tabbedPane.setBounds(10, 11, 414, 239);
          contentPane.add(tabbedPane);
  
          // Create new scrollpane
          JScrollPane scrollPaneEntityOverview = new JScrollPane();
          scrollPaneEntityOverview.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  
          // Set entity location data to table
          Object[][] data = locationToTable();
          JTable table = new JTable(data, null); // 1
  
          // Create new scrollpane, initialize with table
          JScrollPane scrollPaneMap = new JScrollPane(table);
          scrollPaneMap.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  
          tabbedPane.addTab("Entity overview", null, scrollPaneEntityOverview, null);
          tabbedPane.addTab("Map", null, scrollPaneMap, null);
      }
  
      public Object[][] locationToTable() {
          Object[][] data = new Object[6][6];
          Location[] locations = WorldMap.getInstance().getLocations();
          for (int x = 0; x &lt; 6; x++) {
              for(int y = 0; y &lt; 6; y++) {
                  data[x][y] = locations[6*x+y];
              }
          }
          return data;
      }
  </code></pre>
  
  <p>As you can see, I'm trying to add the table to a scrollpane, which is part of a tabbed window. However, when trying this out the tab is empty: nothing shows. Furthermore, I also get the following NullPointerException, which links to <code>//1</code> in the snippet above:</p>
  
  <pre><code>java.lang.NullPointerException
      at javax.swing.JTable$1.getColumnCount(Unknown Source)
      at javax.swing.JTable.createDefaultColumnsFromModel(Unknown Source)
      at javax.swing.JTable.tableChanged(Unknown Source)
      at javax.swing.JTable.setModel(Unknown Source)
      at javax.swing.JTable.&lt;init&gt;(Unknown Source)
      at javax.swing.JTable.&lt;init&gt;(Unknown Source)
      at javax.swing.JTable.&lt;init&gt;(Unknown Source)
      at game.Overview.&lt;init&gt;(Overview.java:59)
      at game.Overview$1.run(Overview.java:29)
      at java.awt.event.InvocationEvent.dispatch(Unknown Source)
      at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
      at java.awt.EventQueue.access$500(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.awt.EventQueue$3.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>