gpt4 book ai didi

java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException : empty String OS Kernel Simulation 中的异常

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:23:01 25 4
gpt4 key购买 nike

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

线程“AWT-EventQueue-0”java.lang.NumberFormatException 中的异常:空字符串

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

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)

最佳答案

当您单击该按钮时,您的 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 block ,并在 catch block 中设置默认值。

例如:

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

关于java - 线程 "AWT-EventQueue-0"java.lang.NumberFormatException : empty String OS Kernel Simulation 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30492919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com