如何处理" AWT-EventQueue-0"显示java.lang.NullPointerException?

时间:2014-04-09 10:56:29

标签: java swing nullpointerexception awt-eventqueue

我正在尝试从JTable以及文本文件中删除数据,但获得了"AWT-EventQueue-0" java.lang.NullPointerException。请帮帮我,这是我的代码:

private void btndeleteActionPerformed(java.awt.event.ActionEvent evt2){
    deleteRow();            }


    private void deleteRow() 
      {
        dtm = (DefaultTableModel)myjtable.getModel();`EXCEPTION AT THIS LINE `
        int r=myjtable.getSelectedRow();

        String str=myjtable.getValueAt(r,0).toString();


        int row=dtm.getRowCount();     

        try  
        { 
            RandomAccessFile r1=new RandomAccessFile("C:\\Myfile.txt","rw");
            RandomAccessFile r2=new RandomAccessFile("C:\\TempFile.txt","rw");
            FileWriter myfile = new FileWriter("C:\\TempFile.txt",true);
            PrintWriter outStream = new PrintWriter(myfile);
            FileWriter myfile1 = new FileWriter("C:\\Myfile.txt",true);
            PrintWriter outStream1 = new PrintWriter(myfile1);

            r2.setLength(0);


            String regexp = "[|]+";
            for(int i=0;i<row;i++)
            {   
             String str1=r1.readLine().trim();
             String[] line = str1.split(regexp);
            if(line[0].compareTo(str)!=0)
            {
                outStream.print(str1);
                outStream.print("\r\n");

            }
            }
            outStream.close();

            r2.seek(0);
            r1.setLength(0);
            for(int i=0;i<row-1;i++)
            {   

             String str1=r2.readLine().trim();


            {
                outStream1.print(str1);
                outStream1.print("\r\n");

            }
            }
            r1.close();
            r2.close();
            outStream1.close();
            readFile();
        }
        catch (IOException e) {     
            System.out.println(e);     
        }



        }

1 个答案:

答案 0 :(得分:0)

我的猜测是你正在影响myjtable变量。这意味着您将其定义为类中的实例变量,并将其定义为代码中的某个局部变量。所以你可能有类似的代码:

JTable myjtable;  // the instance variable
.
.
.
.
JTable myjtable = new JTable(...); // local variable

您不需要本地变量,因此代码应为:

myjTable = new JTable(...);