“AWT-EventQueue-0”java.lang.NullPointerException

时间:2013-02-25 19:11:52

标签: java swing

我的代码中出现了''线程异常'AWT-EventQueue-0“java.lang.NullPointerException''。我不知道为什么以及发生此异常的地方。编译工作正常但有时我在运行时遇到此错误。我可以在这里帮忙吗?完整的错误消息是:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2115)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295)
at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:770)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我的部分代码:

class UserAgentGui extends JFrame implements ActionListener, ListSelectionListener, CinemaVocabulary {

private JTable movieTable;

public UserAgentGui(UserAgent a) {
// -----------------------------------   Constructor

    Object obj[][] = new Object[0][columnNames.length];
    TableModel model = new TableDataModel(obj, columnNames);
    movieTable = new JTable(model);
movieTable.setFillsViewportHeight(true);
    movieTable.setPreferredScrollableViewportSize(new Dimension(320,100));
    movieTable.setFont(new Font("Arial", Font.PLAIN, 11));
}

void displayMovies(java.util.leap.List list) {
// -------------------------------------------
    String s = "";

    if (menu.getSelectedItem().equals(actions[1])){//  <-- This is a list of movies
        s = "Movie-databse is presented";
        data = new Object[list.size()][columnNames.length];
        for (int i = 0; i < list.size(); i++) {
            Movie movie = (Movie)list.get(i);
            data[i][0] = movie.getTitle();
            data[i][1] = movie.getYear();
            data[i][2] = movie.getKind();
            data[i][3] = 0.0;
        }
    TableDataModel model = (TableDataModel)movieTable.getModel();
    model.setData(data);
    movieTable.setModel(model);
    movieTable.updateUI();
    }
    else {
        s = "Score Movies";
    }
    msg_text.setText(s);    
}

// =========================== External class ============================//

/* TableDataModel:
*  --------------
*  External class for the definition of the tables data model, used to
*  control the display of data within the different tables
**/
class TableDataModel extends AbstractTableModel {
// ----------------------------------------------

   private String[] columns;
   private Object[][] data;

   public TableDataModel(Object[][] data, String[] columns) {
   // ----------------------------------------------------------  Constructor
      this.data = data;
      this.columns = columns;
   }

   public int getColumnCount() {
   // -----------------------------  Return the number of columns in the table
      return columns.length;
   }

   public int getRowCount() {
   // --------------------------  Return the number of rows in the table
      return data.length;
   }

   public String getColumnName(int col) {
   // --------------------------------------  Return the name of a column
      return columns[col];
   }

  public boolean isCellEditable(int row, int col) {
   // -------------------------------------------------
            if (col == 3) {
                return true;
           } else {
               return false;
            }
   }

   public Object getValueAt(int row, int col) {
   // --------------------------------------------  Return the value at a specific
//                                               row and column
      if ( data.length == 0 ) return null;
         return data[row][col];
    }

   public Class getColumnClass(int col) {
   // --------------------------------------  Return the class of the values held
   //                                         by a column
      Object o = getValueAt(0, col);
      if (o == null) return columns[col].getClass();
         return getValueAt(0, col).getClass();
   } 

   public void setValueAt(Object value, int row, int col){
    // -------------------------------------------------------  Set the value at a        specific
   //                                                          row and column
      data[row][col] = value;
  }

   public void setData(Object[][] data){
   // -------------------------------------  Update the entire data in the table
       this.data = data;
   }

   Object[][] getData(){
   // ---------------------  Return the entire data of the table
       return data;
   }
}// end TableDataModel
}

3 个答案:

答案 0 :(得分:2)

Object obj[][] = new Object[0][columnNames.length];
您已将obj的第一个尺寸大小设置为0 它应该是Object obj[][]= new Object[1][columnNames.length]

答案 1 :(得分:1)

您可以从堆栈跟踪中看到它正在尝试绘制您的屏幕内容。也许在该屏幕或其中一个组件上没有正确初始化某些内容。

答案 2 :(得分:1)

  

我在线程中遇到''例外'AWT-EventQueue-0“   我的代码中的java.lang.NullPointerException''。我不知道为什么和   发生此异常的地方。编译工作正常,但有时   我在运行时收到此错误。我可以在这里帮忙吗?完整的错误   消息是这样的:

  • 这个问题根本无法解决,为了更好地帮助发布SSCCE,简短,可运行,可编译,需要将所有自定义类硬编码到本地变量

  • 你混合了两个XxxTableModels,没理由从TableModel中取出TableDataModel模型

  • TableModel从此代码

  • 中未知
  • TableDataModel未添加到可见的JTable,

  • void displayMovies(java.util.leap.List list){is not standard java import

  • movieTable.updateUI();将在你将创建自己的BasicTableUI的情况下工作,但这部分没有XxxTableModel和代码发布在这里,

  • updateUI()是UIManager和Look And Feel的方法,不是用于将共享模型中的数据显示到屏幕