-1

我的代码中出现“线程“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
}
4

3 回答 3

2

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

于 2013-02-25T19:35:19.440 回答
1

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

  • 这个问题根本无法回答,为了获得更好的帮助,请尽快发布 SSCCE,简短,可运行,可编译,需要将所有自定义类硬编码到局部变量

  • 您混合了两个 XxxTableModel,没有理由从 TableModel 中获取 TableDataModel 模型

  • 此代码中的 TableModel 未知

  • TableDataModel 未添加到可见 JTable,

  • void displayMovies(java.util.leap.List list) { 不是标准的 java 导入

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

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

于 2013-02-25T20:14:00.387 回答
1

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

于 2013-02-25T19:13:55.823 回答