执行SolveBasicMath()时是什么导致异常?

时间:2019-06-04 08:14:31

标签: java javafx

大家好(初学者在这里,请保持温柔),只是为了给您一个想法:我正在为Uni作业编写一个计算器类型的程序。使用场景生成器制作FXML文件。我有三个主要的FXML对象:一个TextField,一个ChoiceBox和一个Button。当您在选择框中选择一个选项时,需要在文本字段中输入特定的内容。最后,当您单击Button时,我们进入Java代码并基于ChoiceBox中选择的选项执行一个方法。我正在制作一种方法,尝试对Texfield中的给定字符串进行分析,并尝试查找第一个数字(负数或正数),并使用其值创建一个新的整数。 如果有人可以帮助我,我将提供任何进一步的信息。

String BasicMathProblem;

@FXML
private void SolveBasicMath() {
    textview.setText(null);
    BasicMathProblem = textedit.getText();
    System.out.println("The math problem is: " + BasicMathProblem);
    int i = 0;
    char num01 = BasicMathProblem.charAt(i);
    String Negativecheck = String.valueOf(num01);
    if (Negativecheck.equals("-")) {
        System.out.println("The first number is a negative number");
        // Don't forget, you need to add a '-' to the first number!
        i = i + 1;
    } else {
        System.out.println("The first number is a positive number");
    }
    int firstspaceindex = BasicMathProblem.indexOf(' ');
    int j = 0;
    int[] firstnumberdigitsArrayIntegers = new int[firstspaceindex];
    while (Character.isDigit(num01) && firstspaceindex > i) {
        System.out.println("The 'count' coefient i = " + i);
        firstnumberdigitsArrayIntegers[j] =
                Character.getNumericValue(BasicMathProblem.charAt(i));
        i++;
        j++;
        System.out.println("The digits of the first number [" + j + "] are as following:" + firstnumberdigitsArrayIntegers[j]);
    }
}

这是我再次要提到的地方,我是一个绝对的初学者。我希望从该方法的当前状态中摆脱出来的是:包含第一个数字中每个数字的整数值的数组,我实际上得到的是:

The user selected: Basic Math
The Calculate button was just pressed
The math problem is: 23456 
The first number is a positive number
The 'count' coefient i = 0
The digits of the first number [1] are as following: 0
The 'count' coefient i = 1
The digits of the first number [2] are as following: 0
The 'count' coefient i = 2
The digits of the first number [3] are as following: 0
The 'count' coefient i = 3
The digits of the first number [4] are as following: 0
The 'count' coefient i = 4
Exception in thread "JavaFX Application Thread"             
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at     

javafx.fxml / javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1787)     在javafx.fxml / javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1670)     在javafx.base / com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)     在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在javafx.base / com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在javafx.base / com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在javafx.base / com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)     在javafx.base / javafx.event.Event.fireEvent(Event.java:198)     在javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8890)     在javafx.controls / javafx.scene.control.Button.fire(Button.java:203)     在javafx.controls / com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)     在javafx.controls / com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)     在javafx.base / com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)     在javafx.base / com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)     在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)     在javafx.base / com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)     在javafx.base / com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)     在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)     在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在javafx.base / com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)     在javafx.base / com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)     在javafx.base / com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)     在javafx.base / com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)     在javafx.base / javafx.event.Event.fireEvent(Event.java:198)     在javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3862)     在javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)     在javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2590)     在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)     在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)     在java.base / java.security.AccessController.doPrivileged(AccessController.java:389)     在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)     在javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:411)     在javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)     在javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)     在javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)     在javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机方法)     在javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)     在java.base / java.lang.Thread.run(Thread.java:835) 造成原因:java.lang.reflect.InvocationTargetException     在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法)     在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:567)     在com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)     在jdk.internal.reflect.GeneratedMethodAccessor2.invoke(未知来源)     在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.base / java.lang.reflect.Method.invoke(Method.java:567)     在javafx.base / com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)     在javafx.fxml / com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)     在javafx.fxml / javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1782)     ...另外46个 由以下原因引起:java.lang.ArrayIndexOutOfBoundsException:索引5超出长度5的范围     在com.myproject.myvisualcalculator.Controller.SolveBasicMath(Controller.java:151)     在com.myproject.myvisualcalculator.Controller.PressCalculate(Controller.java:40)     ...还有57个

0 个答案:

没有答案