我正在使用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)
答案 0 :(得分: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.");
}
例如:
double value;
try{
value = Double.parseDouble(processID_TF.getText());
}catch(Exception e){
value = 0.; // Default value
}