2013-09-26 26 views
1

我正在從TableModel接口實現我自己的模型。我的問題在於我的模型的建造者。我傳遞一個Object [] []類似數據庫的數據庫和Object []類似的頭文件。setModel構建器中的錯誤

在tableModel接口中,我有一個ArrayList,我保留從數據庫中獲取的數據。

如何將我的Object [] [] [] [] [] [] [] [] [] [[]]複製到我的ArrayList中。

在下面的行中,我顯示了我的代碼,你可以看到我在做什麼。

public class MiModelo implements TableModel{ 

private ArrayList<TableModelListener> listeners=new ArrayList<TableModelListener>(); 

private ArrayList<Object[]> datos=new ArrayList<Object[]>(); 

private ArrayList<String> titulos=new ArrayList<String>(); 

public MiModelo(Object[][] obj, Object[] head){ 
    Object[] registro; 
    for (int i=0;i<obj.length;i++){ 
     registro=new Object[obj[i].length]; 
     for (int k=0;k<obj[i].length;k++){ 
      registro[k]=obj[i][k]; 
     } 
     System.out.println(registro[0]+", "+registro[1]+", "+registro[2]+", "+registro[3]); 
     datos.add(registro); 
    } 


    for (int i1=0;i1<head.length;i1++) 
     titulos.add((String) head[i1]); 
} 

對於某些瑞森而言,此構建器不起作用。在Eclipse它顯示了以下錯誤:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at javax.swing.JTable.prepareRenderer(Unknown Source) 
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source) 
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source) 
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) 
at javax.swing.plaf.ComponentUI.update(Unknown Source) 
at javax.swing.JComponent.paintComponent(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JViewport.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JLayeredPane.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
at javax.swing.RepaintManager.paint(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) 
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 
at java.awt.Container.paint(Unknown Source) 
at java.awt.Window.paint(Unknown Source) 
at javax.swing.RepaintManager$3.run(Unknown Source) 
at javax.swing.RepaintManager$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.access$1000(Unknown Source) 
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) 
at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$200(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$1.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) 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at javax.swing.JTable.prepareRenderer(Unknown Source) 
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source) 
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source) 
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) 
at javax.swing.plaf.ComponentUI.update(Unknown Source) 
at javax.swing.JComponent.paintComponent(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JViewport.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at javax.swing.JLayeredPane.paint(Unknown Source) 
at javax.swing.JComponent.paintChildren(Unknown Source) 
at javax.swing.JComponent.paintToOffscreen(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) 
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) 
at javax.swing.RepaintManager.paint(Unknown Source) 
at javax.swing.JComponent.paint(Unknown Source) 
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source) 
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source) 
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source) 
at java.awt.Container.paint(Unknown Source) 
at java.awt.Window.paint(Unknown Source) 
at javax.swing.RepaintManager$3.run(Unknown Source) 
at javax.swing.RepaintManager$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source) 
at javax.swing.RepaintManager.access$1000(Unknown Source) 
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source) 
at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$200(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$1.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) 

在代碼中,你可以看到,我已經writen一個的System.out.println,看它是否是從數據庫中讀取,並且aparently它是確定。

預先感謝您。

+2

你可以寫你的異常的堆棧跟蹤? – Ketan

+0

我剛剛做到了。 – galeonweb

回答

1

TableModel的實施方案沒有爲JTable添加自己作爲其模型的偵聽器,如TableModel接口所​​要求的。相反,請擴展AbstractTableModel,它提供了必要的實現。 EnvTableTest是一個基本的例子。

如果這是而不是問題,請編輯您的問題,以包括一個sscce和堆棧跟蹤,展現您描述的問題。

+0

好吧,我已經解決了從AbstractTableModel擴展的問題,它的工作原理,但我想使用TableModel接口來看看它是如何工作的。無論如何,謝謝 – galeonweb

+0

很高興幫助;以「AbstractTableModel」的來源爲例。一段時間後,您可以通過點擊[空對號](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)來接受此答案。左邊。 – trashgod