java - Why is doAction() getting to done() early? -


i have of strange problem. have doaction() swingworker for loop remove rows jtable , data base. if for loop large done() method fire off before loop completed, stopping execution midway.

my code:

 public void doaction() throws exception {     int selectedrows[];     int modelrows[];     java.util.arraylist<customrecord> condemned;     customtablemodel datasource;     boolean databasedelete = false;     object uniqueid;     int rowcount = 0;      datasource = (customtablemodel)table.getmodel();     selectedrows = table.getselectedrows();      condemned = new java.util.arraylist<customrecord>();     modelrows = new int[selectedrows.length];      for(int i=0;i<selectedrows.length;i++)         modelrows[i] = table.convertrowindextomodel(selectedrows[i]);      selectedrows = null;      for(int i= 0; < modelrows.length; i++){         uniqueid = datasource.getuniqueid(modelrows[i]);          system.out.println("debug: spot 1, rowcount = " + rowcount + ", = " + + ", length = " + modelrows.length);          if(uniqueid == null)             datasource.removerow(modelrows[i]);         else if(adminprivileges){             condemned.add(//record being looked at);             databasedelete = true;              system.out.println("debug: spot 2, rowcount = " + rowcount + ", = " + i);              datasource.removerow(modelrows[i]);             if(condemned.size() >= 100){                 database.cleardata(condemned);                 condemned.clear();             }              system.out.println("debug: spot 3, rowcount = " + rowcount + ", = " + i);          }          system.out.println("debug: spot 4, rowcount = " + rowcount + ", = " + + ", uniqueid = " + uniqueid.tostring() + "\n");          if(++rowcount >= 1000){             system.out.println("rowcount = " + rowcount + ", in break");             break;         }     }      if(databasedelete){         if(condemned.size() > 0)             database.cleardata(condemned);         loaddata(table,database,filterparams);     } }  public void done() {      system.out.println("debug: in done()");      table.setvisible(true);     //display warning message if number of rows reached limit     if(rowcount >= 1000){         // display limit reached warning;     } } 

my output here looks like:

debug: spot 1, rowcount = 0, = 0, length 1006 debug: spot 2, rowcount = 0, = 0 debug: spot 3, rowcount = 0, = 0 debug: spot 4, rowcount = 0, = 0, uniqueid = 2608 . . . debug: spot 1, rowcount = 505, = 505, length = 1006 debug: spot 2, rowcount = 505, = 505 debug: spot 3, rowcount = 505, = 505 debug: spot 4, rowcount = 505, = 505, uniqueid = 3073  debug: spot 1, rowcount = 506, = 506, length = 1006 debug: in done() 

if big for loop set go large small for(i = modelrows.length-1; >= 0; i--) little further:

debug: spot 1, rowcount = 0, = 1005, length 1006 debug: spot 2, rowcount = 0, = 1005 debug: spot 3, rowcount = 0, = 1005 debug: spot 4, rowcount = 0, = 1005, uniqueid = 3073 . . . debug: spot 1, rowcount = 899, = 106, length = 1006 debug: spot 2, rowcount = 899, = 106 debug: spot 3, rowcount = 899, = 106 debug: spot 4, rowcount = 899, = 106, uniqueid = 2174  debug: in done() 

how can make/allow doaction() method complete properly? there maximum time swingworker execute before done() method called?

edit

i believe have confused myself(and else) in saying doaction() swingworker. doaction() method called doinbackground() in class extends swingworker.

code:

protected interface lengthyaction{      public customactions getaction();      public java.awt.component getcomponent();      public void doaction() throws exception;      public void done();  }  private class dataswingworker extends javax.swing.swingworker{      private lengthyaction action;      public dataswingworker(lengthyaction targetaction){         action = targetaction;         setcursor(action.getcomponent(),java.awt.cursor.wait_cursor);         if(listener != null)             listener.actionperformed(new java.awt.event.actionevent(**stuff**));     }     .     .     .     @override     protected object doinbackground() throws exception {         action.doaction();         return null;                 }      @override     protected void done() {         if(listener != null)             listener.actionperformed(new java.awt.event.actionevent(**stuff**));                         action.done();     }  } 

i found answer problem, , had nothing initial thoughts. taking out the:

if(condemned.size() >= 100){     database.cleardata(condemned);     condemned.clear(); } 

in big doaction() loop makes work. seems causing uniqueid's skip forward 100 places, , hitting end of array before end of loop, giving nullpointerexception.

thanks help, did end putting me on correct path.

i'll try answer questions far seem answerable based on data you've posted:

i have doaction() swingworker loop remove rows jtable , data base ...... no, doaction() method on object called doinbackground() method.

this not good. if calling doaction() within swingworker's doinbackground() method means code flagrantly violating swing threading rules making swing calls, calls mutate state of swing components, within background thread. may in fact cause or @ least major contributor intermittent problems running of background thread.

is there maximum time swingworker execute before done() method called?

no. called when doinbackground() method has completed actions , no exceptions have been found. key issue is: checking exceptions thrown during swingworker's run? tested calling get() on swingworker within try/catch block , after has completed actions.

how can make/allow doaction() method complete properly?

unfortunately it's hard tell based on current code since can't compile or run it. if still stuck, create , post minimal example program or sscce.

my main recommendations:

  1. first , foremost, re-write code strictly follows swing threading rules. has greatest chance solving problem. understand swingworkers automatically allow propertychangelistener support, , may way communicate between background thread , swing event thread.
  2. next sure refactor code classes small independently testable units, , test each of them max trying make them fail in way can think of.

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -