【问题标题】:Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String OS Kernel Simulation线程“AWT-EventQueue-0”中的异常 java.lang.NumberFormatException:空字符串 OS 内核模拟
【发布时间】:2015-05-27 20:53:28
【问题描述】:

我正在使用 GUI 在 Java 中进行 OS 内核模拟项目。 我创建了一个 Jtable,在其中添加流程(任务)。 现在我正在尝试使用 FIFO(先进先出)算法执行进程。 我正在尝试实现 Button 功能以使用 fifo 算法执行进程。在这里,按下按钮后出现以下异常。

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

我可能在下面的函数中做错了

private void fifo_BtnActionPerformed(java.awt.event.ActionEvent evt) {
    //DefaultTableModel model = (DefaultTableModel) processTable.getModel();

    int totalNoOfProcess = p.totalProcess();

    for (int i = 0; i < totalNoOfProcess; i++) {
        Process cp = p.getPCB(i);

        double burstTime = cp.getBurst_time();

        while (burstTime > 0) {
            burstTime = burstTime - cpuTime;
            cp.setStatus("running");

            DefaultTableModel newRow = new DefaultTableModel();
            newRow.equals(model);

            newRow.setValueAt(new Object[]{
                (Double.parseDouble(processID_TF.getText())), processNameTF.getText(), Double.parseDouble(burstTimeTF.getText()), Double.parseDouble(priorityTF.getText()), statusTF.getText().trim()
            },i, i);

            //newRow.addRow(new Object[] {(Double.parseDouble(processID_TF.getText())), processNameTF.getText(), Double.parseDouble(burstTimeTF.getText()), Double.parseDouble(priorityTF.getText()), statusTF.getText().trim()});
            model.removeRow(i);
            //model.setValueAt(newRow, i, i);

            for (int a = 0; a < 100; a++) {
                model.fireTableDataChanged();
            }
        }
        cp.setBurst_time(0);
        cp.setStatus("Completed");
        DefaultTableModel anotherRow = new DefaultTableModel();
        anotherRow.equals(model);

        anotherRow.addRow(new Object[] {
            (Double.parseDouble(processID_TF.getText())), processNameTF.getText(), Double.parseDouble(burstTimeTF.getText()), Double.parseDouble(priorityTF.getText()), statusTF.getText().trim()
        });
        model.removeRow(i);
        model.setValueAt(anotherRow, i, i);
        model.fireTableDataChanged();
    }
}

这是我的 add_processes 函数

private void add_processBtnActionPerformed(java.awt.event.ActionEvent evt)     
{   
    p.createPCB((Double.parseDouble(processID_TF.getText())), processNameTF.getText(), Double.parseDouble(burstTimeTF.getText()), Double.parseDouble(priorityTF.getText()), statusTF.getText().trim());

    Process process = p.getPCB(count);

    process.setId(Double.parseDouble(processID_TF.getText()));
    process.setName(processNameTF.getText().trim());
    process.setBurst_time(Double.parseDouble(burstTimeTF.getText()));
    process.setPriority(Double.parseDouble(priorityTF.getText()));
    process.setStatus(statusTF.getText());

    model.addRow(new Object[]{process.getId(), process.getName(), process.getBurst_time(), process.getPriority(), process.getStatus()});

    //model.fireTableDataChanged();
    processID_TF.setText("");
    processNameTF.setText("");
    burstTimeTF.setText("");
    priorityTF.setText("");

    count++;
}     

这是按下 fifo 按钮后引发的整个异常错误。添加进程功能工作正常

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
    at java.lang.Double.parseDouble(Double.java:510)
    at ahmed.riphah.OS.ProcessManagementGUI.fifo_BtnActionPerformed(ProcessManagementGUI.java:251)
    at ahmed.riphah.OS.ProcessManagementGUI.access$100(ProcessManagementGUI.java:14)
    at ahmed.riphah.OS.ProcessManagementGUI$2.actionPerformed(ProcessManagementGUI.java:96)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6297)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6062)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4660)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:627)
    at java.awt.EventQueue$2.run(EventQueue.java:625)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:641)
    at java.awt.EventQueue$3.run(EventQueue.java:639)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

【问题讨论】:

    标签: java algorithm swing fifo


    【解决方案1】:

    当您单击按钮时,您的 GUI 中的文本字段之一为空,导致:

    Double.parseDouble( aTextField.getText() );
    

    失败(这是异常告诉你的)。

    那么,两种情况:

    • 必须填写该文本字段,在这种情况下,您可以在单击按钮时添加测试,以验证所有字段均已正确设置。

    例如:

    if( processID_TF.getText().match("\\d+") ){
       // proceed with normal behaviour
    }else{
       JOptionPane.showMessageDialog(null, "You must indicate a correct process ID.");
    }
    
    • 或者该字段是可选的,在这种情况下,您可以在上面的代码行周围使用 try/catch 块,并在 catch 块中设置默认值。

    例如:

    double value;
    try{
        value = Double.parseDouble(processID_TF.getText());
    }catch(Exception e){
        value = 0.; // Default value
    }
    

    【讨论】:

      猜你喜欢
      • 2013-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多