ファイルから読み取った文字列をSystem.out.printlnで表示するのにはエラーが出ないのですが、ラベルにSetTextで表示させようとするとエラーがでます。
java
1 2package money; 3 4import java.io.BufferedReader; 5import java.io.File; 6import java.io.FileNotFoundException; 7import java.io.FileReader; 8import java.io.IOException; 9import java.net.URL; 10import java.util.ResourceBundle; 11import javafx.event.ActionEvent; 12import javafx.fxml.FXML; 13import javafx.fxml.Initializable; 14import javafx.scene.control.Label; 15import javafx.scene.control.TextField; 16 17 18 19public class FXMLDocumentController implements Initializable { 20 @FXML 21 private Label nowmoney; 22 { 23try{ 24 File file = new File("/Users/uron/Documents/test.txt"); 25 26 if (checkBeforeReadfile(file)){ 27 BufferedReader br = new BufferedReader(new FileReader(file)); 28 29 String str; 30 while((str = br.readLine()) != null){ 31 System.out.println(str); 32 nowmoney.setText(str); 33 } 34 35 br.close(); 36 }else{ 37 System.out.println("ファイルが見つからないか開けません"); 38 } 39 }catch(FileNotFoundException e){ 40 System.out.println(e); 41 }catch(IOException e){ 42 System.out.println(e); 43 } 44 } 45 46 private static boolean checkBeforeReadfile(File file){ 47 if (file.exists()){ 48 if (file.isFile() && file.canRead()){ 49 return true; 50 } 51 } 52 53 return false; 54 } 55 56 57 58 59 60 61 62 63 64 65 66// @FXML 67// private void handleButtonAction(ActionEvent event) { 68// System.out.println("You clicked me!"); 69// label.setText("Hello World!"); 70// } 71 72 @Override 73 public void initialize(URL url, ResourceBundle rb) { 74 // TODO 75 } 76 77} 78
エラーは
Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) at java.lang.Thread.run(Thread.java:745) Caused by: javafx.fxml.LoadException: file:/Users/uron/NetBeansProjects/Money/dist/run1859694132/Money.jar!/money/FXMLDocument.fxml:10 at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) at money.Money.start(Money.java:22) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) Caused by: java.lang.NullPointerException at money.FXMLDocumentController.<init>(FXMLDocumentController.java:34) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at java.lang.Class.newInstance(Class.java:442) at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51) at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927) at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971) at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220) at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744) at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707) at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527) ... 14 more Exception running application money.Money Java Result: 1 Deleting directory /Users/uron/NetBeansProjects/Money/dist/run1859694132 jfxsa-run: ビルド成功(合計時間: 1秒)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。