Hi!

My purpose is to fill the JComboBox from the database. The code shown below is working - it fills the JComboBox. However, the problem is that when I click on JComboBox to select some item, the error occurs. As I understand, String is casted to Object, and this is exactly the reason of the error. But I cannot find the way, in which my code must be changed to solve the problem. Please help me with some advices. Thanks!

The code:

Vector docTypes = new Vector();
        docTypes = SystClasses.Form.tableModel.returnDataFromSelectQuery("select doc_type from documents");

        cmbFormType = new JComboBox(docTypes);

...

  public Vector returnDataFromSelectQuery(String q) {
    cache = new Vector();
    String record = new String();
    try {
      ResultSet rs = statement.executeQuery(q);

      while (rs.next()) { 
        record = rs.getString(1);
        cache.addElement(record);
      }
    } catch (Exception e) {
      cache = new Vector(); // blank it out and keep going.
      e.printStackTrace();
    }
    return cache;
  }

The error message:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
        at SystClasses.QueryTableModel.getValueAt(QueryTableModel.java:58)
        at SystClasses.QueryTableModel.getColumnClass(QueryTableModel.java:50)
        at javax.swing.JTable.getColumnClass(JTable.java:2666)
        at javax.swing.JTable.getCellRenderer(JTable.java:5670)
        at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2071)
        at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1974)
        at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1770)
        at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
        at javax.swing.JComponent.paintComponent(JComponent.java:752)
        at javax.swing.JComponent.paint(JComponent.java:1029)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JViewport.paint(JViewport.java:747)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
        at javax.swing.JComponent.paintChildren(JComponent.java:862)
        at javax.swing.JComponent.paint(JComponent.java:1038)
        at javax.swing.JComponent.paintToOffscreen(JComponent.java:5124)
        at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1224)
        at javax.swing.JComponent._paintImmediately(JComponent.java:5072)
        at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:808)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Dani AI

Generated

The stack trace points to a classic data-shape mismatch: your table model (QueryTableModel) is trying to treat each cached row as an array of Objects, but the vector you populated contains plain Strings. That causes the ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object; when the JTable asks the model for values during painting (this is why the error appears when you open/select the combo even though the combo was filled successfully).

Two practical fixes — pick one that fits your design:

  1. Keep the model’s expectation (rows as Object[]). Wrap each DB value in an array when building the cache:

    // when reading the ResultSet, store rows in the form the table expects
    cache.add(new Object[] { rs.getString(1) });
  2. Make the model tolerant of different row shapes. Change getValueAt so it checks the actual stored type instead of blindly casting:

    public Object getValueAt(int row, int col) {
    Object rowObj = cache.get(row);
    if (rowObj instanceof Object[]) {
     return ((Object[]) rowObj)[col];
    } else if (rowObj instanceof java.util.List) {
     return ((java.util.List<?>) rowObj).get(col);
    } else {
     // single-value rows (single-column table)
     return (col == 0) ? rowObj : null;
    }
    }

Other recommendations:

  • Don’t reuse the same raw Vector for a JTable and a JComboBox unless the element shape is identical. Create separate models (e.g., DefaultComboBoxModel<String> for the combo).
  • Use generics (List<String>) instead of raw Vector and modern try-with-resources for JDBC to avoid leaks.
  • Make getColumnClass defensive: scan rows for a non-null value and return its class, otherwise Object.class.
  • Run UI updates on the EDT (e.g., SwingUtilities.invokeLater) to avoid painting/model races.

’s request for getValueAt() was the right debugging step, and as noted the issue was the unexpected return type — aligning the stored data shape and the model’s expectations will fix it.

Recommended Answers

All 2 Replies

Can you post your getValueAt() code?

oh, you are right. I have an Object as a return value of getValueAt. Thank you very much. Just missed this point.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.