NumberFormatExceptionが発生する原因がわかりません
- 追記
yonaさんの回答によりNumberFormatExceptionが発生していることがわかりました。自分で調べたのですが正しい形式ではない場合に発生するそうですがこのプログラムは単純に言うと入力した値をファイルに書き込むといったようなものです。ここに整数を入力してもこのエラーになるということはどこがうまくいっていないのでしょうか?
nowmoey.txtの中身は「3」です。
Javaバージョン:jdk1.8.0_91.jdk
(NetBeansのプロジェクトのプロパティの実行→「ランタイム・プラットフォーム」の「管理」にかいてあるバージョンを見ました)
JavaFXバージョン:8.0.91
(/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre/lib/javafx.properties)から参照した
java
1package money; 2 3import java.io.BufferedReader; 4import java.io.File; 5import java.io.FileNotFoundException; 6import java.io.FileReader; 7import java.io.FileWriter; 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; 16public class FXMLDocumentController implements Initializable { 17 @FXML 18 private Label nowmoney; 19 private static boolean checkBeforeReadfile(File file) { 20 if (file.exists()) { 21 if (file.isFile() && file.canRead()) { 22 return true; 23 } 24 } 25 return false; 26 } 27 @FXML 28 private TextField addmoney; 29 30 int aggregate=0; 31 32 33 34 35 36 37 @FXML 38 private void botan(ActionEvent event) { 39 nowmoney.setText(""); 40 String inputmoney = addmoney.getText(); 41 int i = Integer.parseInt(inputmoney); 42 43 try { 44 File file = new File("/Users/uron/Desktop/nowmoney.txt"); 45 46 if (checkBeforeReadfile(file)) { 47 BufferedReader br = new BufferedReader(new FileReader(file)); 48 49 String str; 50 51 while ((str = br.readLine()) != null) { 52 System.out.println(str); 53 nowmoney.setText(str); 54 int 読み込み = Integer.parseInt(str); 55 aggregate += 読み込み; 56 } 57 58 59 br.close(); 60 } else { 61 System.out.println("ファイルが見つからないか開けません"); 62 } 63 } catch (FileNotFoundException e) { 64 System.out.println(e); 65 } catch (IOException e) { 66 System.out.println(e); 67 } 68 69 aggregate += i; 70 71 addmoney.setText(""); 72 73 System.out.println(aggregate); 74 String 表示 = Integer.toString(aggregate); 75 nowmoney.setText(表示 + "円"); 76 try { 77 File file = new File("/Users/uron/Desktop/nowmoney.txt"); 78 79 if (checkBeforeWritefile(file)) { 80 FileWriter filewriter = new FileWriter(file, false); 81 82 filewriter.write(aggregate); 83 filewriter.close(); 84 } else { 85 System.out.println("ファイルに書き込めません"); 86 } 87 } catch (IOException e) { 88 System.out.println(e); 89 } 90 91 } 92 93 private static boolean checkBeforeWritefile(File file) { 94 if (file.exists()) { 95 if (file.isFile() && file.canWrite()) { 96 return true; 97 } 98 } 99 100 return false; 101 } 102 103 104 @Override 105 public void initialize(URL url, ResourceBundle rb) { 106 // TODO 107 108 try { 109 File file = new File("/Users/uron/Desktop/nowmoney.txt"); 110 111 if (checkBeforeReadfile(file)) { 112 BufferedReader br = new BufferedReader(new FileReader(file)); 113 114 String str; 115 while ((str = br.readLine()) != null) { 116 System.out.println(str); 117 nowmoney.setText(str + "円"); 118 } 119 120 br.close(); 121 } else { 122 System.out.println("ファイルが見つからないか開けません"); 123 } 124 } catch (FileNotFoundException e) { 125 System.out.println(e); 126 } catch (IOException e) { 127 System.out.println(e); 128 } 129 130 } 131 132} 133
エラーは
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Node.fireEvent(Node.java:8411) at javafx.scene.control.Button.fire(Button.java:185) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) at javafx.event.Event.fireEvent(Event.java:198) at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380) at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) at com.sun.glass.ui.View.handleMouseEvent(View.java:555) at com.sun.glass.ui.View.notifyMouse(View.java:937) Caused by: 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 sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) ... 45 more Caused by: java.lang.NumberFormatException: For input string: "3" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at money.FXMLDocumentController.botan(FXMLDocumentController.java:56) ... 55 more Deleting directory /Users/uron/NetBeansProjects/Money/dist/run731981009 jfxsa-run: ビルド成功(合計時間: 13秒)
回答3件
あなたの回答
tips
プレビュー