2016-04-01 14 views
1

Sorunun tam olarak ne olduğunu anlamaya çalışıyorum, ancak yaptığım her şey işe yaramıyor gibi görünüyor. Numaraları ile birlikte, kolon tarafından ayrılmış şekilde listelenen bir metin dosyası var. Bunun bir örneği aşağıda verilmiştir:JavaFX Dosyadan okuma "InvocationTargetException" atar?

Betty Ross: 52

Angie Scotts: 29

Michael Rosen: 72

liste çok kapsamlı ve 10.000 hatları üzerinden içerir.

public class PeopleIds { 
    public static int UNDEFINED_ID = -1; 
    private static HashMap<String, Integer> people; 

    public static void initialize() { 
     people = new HashMap<String, Integer>(); 
     System.out.println(new File("res/ids/people_ids.txt").exists()); 
     try { 
      Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> { 
       people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", ""))); 
      }); 
     } catch (IOException e) { 
      System.out.println("Unable to read specified file."); 
      e.printStackTrace(); 
     } 
    } 

    public static int getId(final String name) { 
     final Integer id = people.get(name); 
     return id != null ? id : UNDEFINED_ID; 
    } 
} 

Ben GUIController sınıftan initialize yöntemini çağırın: Başvuru başlangıcında bir istisna olduğunu söyleyip, bir istisnası atılır, PeopleIds.initialize() ile bu sınıftan diyoruz

public class GUIController implements Initializable { 
    @FXML 
    private TableView<PersonData> personTable; 
    @FXML 
    private TableColumn<PersonData, String> name; 
    @FXML 
    private TableColumn<PersonData, Integer> limit; 
    @FXML 
    private TextField searchInput; 
    @FXML 
    private ImageView personIcon; 

    private Image undefinedIcon; 
    private PersonIcon icon; 
    private ObservableList<PersonData> data; 

    @Override 
    public void initialize(URL location, ResourceBundle resources) { 
     PeopleIds.initialize(); 
     undefinedIcon = new Image(getClass().getResourceAsStream("/ids/no.png")); 
     name.setCellValueFactory(new PropertyValueFactory<PersonData, String>("name")); 
     limit.setCellValueFactory(new PropertyValueFactory<PersonData, Integer>("limit")); 
     data = PriceData.getData(); 
     personTable.setPeople(data); 
     searchInput.textProperty().addListener((ov, oldValue, newValue) -> { 
      final String input = searchInput.getText(); 
      if (input.length() == 0) return; 
      searchInput.setText(input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase()); 
      filterSearch(); 
     }); 
    } 
} 

yöntem. İşte

bütünüyle ne günlüğe kaydedildiği edilir:

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: javafx.fxml.LoadException: 
/C:/Confidential/bin/base/PersonGUI.fxml 

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
    at javafx.fxml.FXMLLoader.load(Unknown Source) 
    at base.PersonGUI.start(PersonGUI.java:13) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source) 
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source) 
    ... 1 more 
Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1 
    at java.io.BufferedReader$1.hasNext(Unknown Source) 
    at java.util.Iterator.forEachRemaining(Unknown Source) 
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source) 
    at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source) 
    at base.PeopleIds.initialize(PeopleIds.java:17) 
    at base.GUIController.initialize(GUIController.java:36) 
    ... 18 more 
Caused by: java.nio.charset.MalformedInputException: Input length = 1 
    at java.nio.charset.CoderResult.throwException(Unknown Source) 
    at sun.nio.cs.StreamDecoder.implRead(Unknown Source) 
    at sun.nio.cs.StreamDecoder.read(Unknown Source) 
    at java.io.InputStreamReader.read(Unknown Source) 
    at java.io.BufferedReader.fill(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    at java.io.BufferedReader.readLine(Unknown Source) 
    ... 24 more 
Exception running application base.PersonGUI 

Burada neler olduğunu emin değilim? Ben ona baktım ve insanlar fxml dosyası (içeriği biçimlendirmek için kullanılan ve GUIController ana sınıfla aynı pakete bağlı olanı taşımak için söylediler, ancak zaten var.

I Bu sorunla günlerce hiçbir sorun yaşamadan güreştik.İlginizdeki tecrübeleriniz var mı? Eğer öyleyse, nasıl çözdünüz? Çok teşekkürler ..

+0

Hata ayıklamaya çalıştınız mı? istisna gerçekten PeopleIds.initialize(); 'dan geliyor mu, yoksa değişkenlerinizden hangisinin altındaki satırlarda boş olabilir? –

cevap

1

Dosya Exception ise okunurken, dosyayı açarken değil, bir denetlenmemiş istisnaFiles.lines akış işlemi için atılır (Stream.forEach bir throws yan tümcesi).

Bu burada kolayca stacktrace görebileceğiniz

Files.lines(Paths.get("res/ids/people_ids.txt")).forEach(s -> { 
    people.put(s.replaceAll(":.*", "").trim(), Integer.parseInt(s.replaceAll(".*:", ""))); 
}); 

, olur:

Sen don (Files.readAllBytes vs Files.lines getting MalformedInputException bkz Bu kullanılmakta olan yanlış Charset kaynaklanır)

Caused by: java.io.UncheckedIOException: java.nio.charset.MalformedInputException: Input length = 1 

Bu tür bir özel durumu catch yan tümce ile yakala:

} catch (IOException e) { 

Çok kontrolsüz istisnaları yakalamak için

} catch (Exception e) { 

kullanmak gerekir.