2015-12-17 22 views
0

我試圖在Platform.runLater()包裹ObservableList,他們卻給我一個錯誤:新任務導致java.lang.IllegalStateException:不上,即使Platform.runLater FX應用程序線程()包裹

local variables referenced from an inner class must be final or effectively final

我想不出有什麼解決這個錯誤的。這裏是我的代碼:

Task task = new Task<Void>() { 
      @Override public Void call() {     
       try { 

        for (int i = 0; i < obLstDataset.size(); i++){ 
         // **************************       
         //doing other stuff here 
         // ************************** 
         if (pageVLEDEResult.contains("something")) {        
          Platform.runLater(new Runnable() { 
          @Override 
          public void run() { 
            obLstInvalid.add((String)obLstDataset.get(i));  
           } 
          }); 
         } 

         else if (pageVLEDEResult.contains("something else")){ 
          if (i!=0){ 
           i--; 
          } 
         } 

         updateProgress (i+1, obLstDataset.size()); 

        } 

       } catch (IOException | FailingHttpStatusCodeException ex) { 
        Logger.getLogger(VehicleLicenceExpiryDateEnquirer.class.getName()).log(Level.SEVERE, null, ex); 
       } 
       return null; 
      } 
     }; 
+0

什麼是阻止你宣佈場最後? – mwe

+0

我以爲它想obLstInvalid或obLstDataset是最終的(他們已經是)。事實上,這是需要最終決定的varialbe「i」,完​​全錯過了它。 – ChickenFeet

回答

0

我認爲你需要使runLater最終的變量被訪問。另外,在for循環中修改for循環變量(在本例中爲i)是非常糟糕的做法。所以我已經將它重新寫入while循環。

Task task = new Task<Void>() { 
     @Override public Void call() {     
      try { 
       int i=0; 
       do { 
        // **************************       
        //doing other stuff here 
        // ************************** 
        if (pageVLEDEResult.contains("something")) {       
         final List <not sure what class this is> fobLstInvalid = obLstInvalid; 
         final List <not sure what class this is> fobLstDataset = obLstDataset; 
         final int fi = i; 

         Platform.runLater(new Runnable() { 
         @Override 
         public void run() { 
           obLstInvalid.add((String)obLstDataset.get(fi));  
          } 
         }); 
        } 

        else if (pageVLEDEResult.contains("something else")){ 
         if (i!=0){ 
          i--; 
         } 
        } 

        updateProgress (i+1, obLstDataset.size()); 
        i++; 
       } while (i < obLstDataset.size()) 

      } catch (IOException | FailingHttpStatusCodeException ex) { 
       Logger.getLogger(VehicleLicenceExpiryDateEnquirer.class.getName()).log(Level.SEVERE, null, ex); 
      } 
      return null; 
     } 
    }; 
+0

謝謝:) 你能解釋一下爲什麼改變變量i被認爲是不好的做法嗎? 哇,我想通了,特別是我的變量,需要是最終的。我的ObersableList變量已經是最終的,這就是爲什麼我很困惑 - 我沒有想到我! 感謝您的修復。 – ChickenFeet

+0

關於for循環中的i變量的註釋不是關於final的。這是關於for循環的含義。 for循環的定義是循環變量在for循環頂部的循環條件中進行控制。 –

+0

如果我可以正確格式化我的評論,你會發現我的問題之間有一段落空間,這個問題是關於不良做法和我需要最終刪除我得到的錯誤的var的認識。感謝for循環提示,我想可讀性使事情變得更容易,但從技術角度來說,我不明白爲什麼更改for-loop var會是一個壞主意 - 我會牢記它:) – ChickenFeet

相關問題