2015-06-16 25 views
6

Antwort: JavaFX append text to TextArea throws ExceptionJavaFX anhängen Text TextArea- Exception wirft

ich einen Thread haben, der die Größe eines Verzeichnisses berechnet.

Ich benutze WalkFileTree dafür.

Um einige Informationen zu erhalten, füge ich die aktuelle Datei an ein Textfeld an.

Aber wenn ich eine Menge von Dateien (. Ex> 300) habe ich bekam

Exception in thread "JavaFX Anwendung Thread" java.lang.ArrayIndexOutOfBoundsException

Hier ist der Code:

private void startScheduledExecutorService() { 

     Thread dt = new Thread(new Runnable() { 
      public void run() { 
       try { 
        taStatus.appendText("Dateien werden ermittelt\n"); 
        Files.walkFileTree(quellOrdner.toPath(), new SimpleFileVisitor<Path>() { 
         @Override 
         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { 
          size += attrs.size(); 
          files++; 
          taStatus.appendText(file.toString() + "\n"); 
          return FileVisitResult.CONTINUE; 
         } 

        }); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     }, "dt"); 
     dt.setDaemon(true); 
     dt.start(); 
    } 

Wenn ich eine Arraylist erstellen und jede Datei, um es hinzuzufügen und diese Array append (jeder Eintrag in einer Reihe) mit dem TextArea-, arbeiten wird.

Es sieht aus wie ein Problem mit der Thread-Geschwindigkeit?

Der vollständige Ausnahme

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException 
    at com.sun.javafx.text.PrismTextLayout.addTextRun(PrismTextLayout.java:755) 
    at com.sun.javafx.text.GlyphLayout.addTextRun(GlyphLayout.java:121) 
    at com.sun.javafx.text.GlyphLayout.breakRuns(GlyphLayout.java:191) 
    at com.sun.javafx.text.PrismTextLayout.buildRuns(PrismTextLayout.java:770) 
    at com.sun.javafx.text.PrismTextLayout.layout(PrismTextLayout.java:1021) 
    at com.sun.javafx.text.PrismTextLayout.ensureLayout(PrismTextLayout.java:223) 
    at com.sun.javafx.text.PrismTextLayout.getBounds(PrismTextLayout.java:246) 
    at javafx.scene.text.Text.getLogicalBounds(Text.java:358) 
    at javafx.scene.text.Text.impl_computeGeomBounds(Text.java:1168) 
    at javafx.scene.Node.updateGeomBounds(Node.java:3556) 
    at javafx.scene.Node.getGeomBounds(Node.java:3509) 
    at javafx.scene.Node.getLocalBounds(Node.java:3457) 
    at javafx.scene.Node.updateTxBounds(Node.java:3620) 
    at javafx.scene.Node.getTransformedBounds(Node.java:3403) 
    at javafx.scene.Node.updateBounds(Node.java:538) 
    at javafx.scene.Parent.updateBounds(Parent.java:1706) 
    at javafx.scene.Parent.updateBounds(Parent.java:1706) 
    at javafx.scene.Parent.updateBounds(Parent.java:1706) 
    at javafx.scene.Parent.updateBounds(Parent.java:1706) 
    at javafx.scene.Parent.updateBounds(Parent.java:1706) 
    at javafx.scene.Parent.updateBounds(Parent.java:1706) 
    at javafx.scene.Parent.updateBounds(Parent.java:1706) 
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2404) 
    at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:314) 
    at com.sun.javafx.tk.Toolkit$$Lambda$178/156290868.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:313) 
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:340) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:525) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505) 
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$400(QuantumToolkit.java:334) 
    at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$47/104706045.run(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139) 
    at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1086508417.run(Unknown Source) 
    at java.lang.Thread.run(Thread.java:745) 
+0

Sie erhalten diese Ausnahme aus welcher Zeile? – immibis

+0

Nirgendwo in meinem Code, ich habe die volle Exception – Garog

+0

@ user3710098 Wenn taStatus ist einige UI-Element, können Sie versuchen, 'Platform.runLater() -> taStatus.appendText());' – varren

Antwort

7

in gleichen Fehler Ran und wie Kleopatras Kommentar angegeben, FX TextArea- muss auf FX Thread zugegriffen werden.

Für einfache Fälle verwenden Platform.runLater mit Java 8 Lambda:

javafx.application.Platform.runLater(() -> taStatus.appendText(file.toString() + "\n")); 

Wenn Sie viele Log-Meldungen haben, statt die FX Thread von Überschwemmungen wäre es besser, sie und Update TextArea- weniger häufig zu puffern. Hier ist ein Beispiel: https://stackoverflow.com/a/31414801/3710098

+1

das funktioniert, wenn Sie nicht viel hinzufügen, werfen Sie einen Blick hier, es erklärt, warum RunLater nicht die beste Wahl ist http://Stackoverflow.com/a/31414801/3710098 – Garog

+0

@Garog Sie haben Ihre Forschung gut gemacht:) Ich cache auch meine Nachrichten, aber ich bevorzuge eine einfache Antwort. Das ist ein guter; Ich habe mich damit verbunden. – Sheepy