0

I am trying to delete a data from JTable and also from text file but got "AWT-EventQueue-0" java.lang.NullPointerException. Please help me, here is my code:

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);     
        }



        }
xlm
  • 6,854
  • 14
  • 53
  • 55
mayuresh
  • 7
  • 2

1 Answers1

0

My guess is that you are shadowing the myjtable variable. This means you defined it as an instance variable in your class and as a local variable somewhere in your code. So you probably have code something like:

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

You don't want a local variable so the code should be:

myjTable = new JTable(...);
camickr
  • 321,443
  • 19
  • 166
  • 288