FXML一直告诉我有一个空字符串

时间:2017-04-21 21:36:19

标签: java netbeans javafx

我有一个更复杂的程序,但经过多次查看代码后,我认为我已经能够解决问题了,我希望能帮助我们检查一下这个代码。
这是其他代码的一个非常基本的版本,但它复制了错误,所以我希望如果我能在这个简化版本中修复它我可以在更大的版本中修复它。

import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.DoubleStream;
import java.util.stream.Stream;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;




    public class FXMLDocumentController implements Initializable {

        //SPECIAL CLASSES NEEDED
        public class TemperatureReading {
            private final double[] values;
            private final LocalDateTime timeStamp;

            public TemperatureReading(LocalDateTime timeStamp, double... values) {
                this.timeStamp = timeStamp;
                this.values = new double[values.length];
                System.arraycopy(values, 0, this.values, 0, values.length);
            }

            public TemperatureReading(double... values){
                this(LocalDateTime.now(), values);
            }

            public double getValue(int channel) {
                return values[channel];
            }

            public int getNumberOfChannels() {
                return values.length;
            }

            public LocalDateTime getTimestamp(){
                return timeStamp;
            }

            public boolean noneExceed(double max) {
                return DoubleStream.of(values).allMatch(v -> v <= max);
            }
        }
        //ANOTHER SPECIAL CLASS TO PARSE THE DATA
        public class TemperatureParser {
            public TemperatureReading parse (String text) {
                System.out.println("String received: " + text);
                if (text != null && text.length() > 0) //CHECK THAT THE STRING RECEIVED IS NOT EMPTY AND NOT NULL
                {
                return new TemperatureReading(Stream.of(text.split("\\s")).mapToDouble(Double::parseDouble).toArray());
                }
                else
                {
                System.out.println("Null reading");
                return null;
                }
            }
        }
        //SIMPLE GUI FXML COMPONENTS
        @FXML
        private Label label;
        @FXML
        private TextArea displayLog;
        @FXML
        private TextField input; //INPUT IS USUALLY NUMBERS EX 24.458  24.435  24.355  24.433  24.511  24.554  24.507  24.515  24.413




        @FXML
        private void handleButtonAction(ActionEvent event) {
            System.out.println("You clicked me!");
            System.out.println(readings.size());
            displayLog.setText("Getting ready to print! Beep Boop!");

                    for (int t = 0; t < readings.size(); t++)
            {
                displayLog.setText("Time: " + readings.get(t).getTimestamp() + " CH1: " + readings.get(t).getValue(0) + " CH2: " + readings.get(t).getValue(1) + " CH3: " + readings.get(t).getValue(2) + " CH4: " + readings.get(t).getValue(3) + " CH5: " + readings.get(t).getValue(4) + " CH6: " + readings.get(t).getValue(5) + " CH7: " + readings.get(t).getValue(6) + " CH8: " + readings.get(t).getValue(7) + " CH9: " + readings.get(t).getValue(8));
            }
        }
        public List<TemperatureReading> readings = new ArrayList<>();
        @FXML
        private void doPass(ActionEvent event)
        {
            TemperatureReading reading;
            TemperatureParser parser = new TemperatureParser();

            reading = parser.parse(input.getText());
            displayLog.appendText("Value Parsed " + input.getText());
            readings.add(reading);
            displayLog.appendText("Value Passed!");
            input.clear();
        }
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        }    

    }

我在那里添加了一条评论,所以你得到一个例子,但在TextField中我放置了以下字符串(复制粘贴):
24.458 24.435 24.355 24.433 24.511 24.554 24.507 24.515 24.413 每个数字之间有2个空格(“\ s”) 从理论上讲,根据我对TemperatureReading Lists中的元素的理解,我应该得到以下内容:

Time: 2017-04-20T15:28:19.118 CH1: 24.024 CH2: 23.961 CH3: 23.889 CH4: 24.163 CH5: 24.085 CH6: 24.107 CH7: 24.017 CH8: 24.042 CH9: 24.473

但是我一直收到这个错误:

String received: 24.458  24.435  24.355  24.433  24.511  24.554  24.507  24.515  24.413
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:8413)
    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:381)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
    at com.sun.glass.ui.View.notifyMouse(View.java:937)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
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)
    ... 48 more
Caused by: java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at java.util.stream.ReferencePipeline$6$1.accept(ReferencePipeline.java:244)
    at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:545)
    at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:260)
    at java.util.stream.DoublePipeline.toArray(DoublePipeline.java:506)
    at trytheclass.FXMLDocumentController$TemperatureParser.parse(FXMLDocumentController.java:67)
    at trytheclass.FXMLDocumentController.doPass(FXMLDocumentController.java:105)
    ... 58 more

它说“由空字符串引起”但是你也可以看到解析器在我实际发送它之前不应该使用空字符串(如果语句)运行并且我在日志文件中输出它并且它确实显示完整字符串为我打字了。

1 个答案:

答案 0 :(得分:1)

此问题的原因与JavaFX和FXML无关。

事实上,让我们暂时不考虑这些问题。您如何看待以下照片?

String text = "24.458  24.435  24.355  24.433  24.511  24.554  24.507  24.515  24.413";
System.out.println(Arrays.toString(text.split("\\s")));

实际输出是:

[24.458, , 24.435, , 24.355, , 24.433, , 24.511, , 24.554, , 24.507, , 24.515, , 24.413]

问题是你在一个空间上分裂,但你的数字之间有两个空格。因此,每对空格之间都是一个空字符串,这就是您在返回数组中的每个其他索引处获得的内容。

修复很简单:通过添加+来调整正则表达式,以便在任意数量的空格上进行拆分而不只是一个:

String text = "24.458  24.435  24.355  24.433  24.511  24.554  24.507  24.515  24.413";
System.out.println(Arrays.toString(text.split("\\s+")));

这给了我输出

[24.458, 24.435, 24.355, 24.433, 24.511, 24.554, 24.507, 24.515, 24.413]