java - Local variables referred from inner class must be final or effectively final -


please have @ below code

private void displayinitialrevenue_method() {     //get dates combo     string selectedcouple = revenueyearcombo.getselecteditem().tostring();     if (selectedcouple.equals("select year")) {         return;     }     string[] split = selectedcouple.split("/");     //related db     double totalamount = 0.0;     //get data database     dbconnector = new dbhandler();     dbconnector.makeconnection();     defaulttablemodel model = (defaulttablemodel) initialrevenuetable.getmodel();     model.setrowcount(0);     resultset selectalldetails = dbconnector.selectalldetails("sql code ");     try {         if (selectalldetails.isbeforefirst() == false) {             joptionpane.showmessagedialog(null, "this table empty");         } else {             while (selectalldetails.next()) {                 string clientname = selectalldetails.getstring("client name");                 string providername = selectalldetails.getstring("provider name");                 double amountinvested = selectalldetails.getdouble("invest_amount");                 //update table                 swingutilities.invokelater(new runnable() {                     public void run() {                         object[] row = {clientname, providername, amountinvested};                         model.addrow(row);                     }                 });                 //get total                 totalamount = totalamount + amountinvested;             }             //add sum             swingutilities.invokelater(new runnable() {                 public void run() {                     object[] blankrow = {null};                     model.addrow(blankrow);                     object[] row = {totalamount};                     model.addrow(row);                  }             });         }     } catch (sqlexception sql) {         joptionpane.showmessagedialog(null, sql.getlocalizedmessage());     } } 

this entire method running inside thread. ui updates running inside swingutilities.invokelater(). pls put attention below.

object[]row = {totalamount}; model.addrow(row); 

the variable totalamount not final , cannot final because need calculated in above while loop. since not final unable use inside swingutilities.invokelater() error says local variables referred inner class must final or final

you can make additional variable is final , copy final result of calculation it.

while (selectalldetails.next()) {             string clientname = selectalldetails.getstring("client name");             string providername = selectalldetails.getstring("provider name");             double amountinvested = selectalldetails.getdouble("invest_amount");             //update table             swingutilities.invokelater(new runnable() {                 public void run() {                     object[] row = {clientname, providername, amountinvested};                     model.addrow(row);                 }             });             //get total             totalamount = totalamount + amountinvested;         } final double totalamountinvested = totalamount; // , use 1 in inner 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -