我是Java编程的新手,并且不知道如何修复当我按下JFrame上的任何一个按钮时发生的错误。我环顾四周,找不到任何东西似乎可以帮助我解决这些问题。 这是我的两个按钮的JFrame代码:
private void btnCheckInActionPerformed(java.awt.event.ActionEvent evt) {
Patient p = null;
if (this.rbtnChild.isSelected()) {
p = new Child();
}
else if (this.rbtnAdult.isSelected()) {
p = new Adult();
}
else if (this.rbtnElder.isSelected()) {
p = new Elder();
}
p.name = txtName.getText();
if(txtName.getText().isEmpty())
{
lblValid.setText("Enter a Name.");
}
h.CheckIn(p);
lblResult.setText(p.name + " successfully checked in.");
txtName.setText("");
}
private void btnProcessActionPerformed(java.awt.event.ActionEvent evt) {
Doctor d = h.processNext();
lblResult.setText(d.name + " looked at your patient " + d.currentPatient.name);
if(q.isEmpty())
{
lblProValid.setText("No Patients in Queue.");
}
}
当我点击btnProcess时,它会给我这个错误:
Exception in thread "AWT-EventQueue-0" java.util.NoSuchElementException
at java.util.LinkedList.removeFirst(LinkedList.java:268)
at java.util.LinkedList.remove(LinkedList.java:683)
at assignmet.Hospital.processNext(Hospital.java:37)
at assignmet.NewJFrame.btnProcessActionPerformed(NewJFrame.java:231)
at assignmet.NewJFrame.access$400(NewJFrame.java:14)
at assignmet.NewJFrame$5.actionPerformed(NewJFrame.java:94)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:740)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:699)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:713)
at java.awt.EventQueue$4.run(EventQueue.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:710)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
当我点击btnCheckIn时,它给了我这个错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at assignmet.NewJFrame.btnCheckInActionPerformed(NewJFrame.java:220)
at assignmet.NewJFrame.access$300(NewJFrame.java:14)
at assignmet.NewJFrame$4.actionPerformed(NewJFrame.java:87)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:740)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:699)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:713)
at java.awt.EventQueue$4.run(EventQueue.java:711)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:710)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
这是提到的医院课程:
package assignmet;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class Hospital {
Queue<Patient> q;
ArrayList<Doctor> d;
Hospital() {
q = new LinkedList<>();
d = new ArrayList<>();
d.add(new Doctor("Smith", Qualification.CHILD));
d.add(new Doctor("Charles", Qualification.ADULT));
d.add(new Doctor("Jones", Qualification.ELDERLY));
}
void CheckIn(Patient p) {
q.add(p);
}
Doctor processNext() {
Patient p = q.remove();
for (Doctor doc: d) {
if (doc.qualification == Qualification.CHILD &&
p.getClass() == Child.class)
{
doc.currentPatient = p;
return doc;
}
else if (doc.qualification == Qualification.ADULT &&
p.getClass() == Adult.class)
{
doc.currentPatient = p;
return doc;
}
else if (doc.qualification == Qualification.ELDERLY &&
p.getClass() == Elder.class)
{
doc.currentPatient = p;
return doc;
}
}
return null;
}
}
答案 0 :(得分:0)
看起来NoSuchElementException
是由在空队列上调用remove方法引起的。确保仅当队列不为空时才启用“处理”按钮。
对于NullPointerException
,您可以查看NewJFrame
课程的第220行或分享该代码(包含行号),让我们一起来看看。
答案 1 :(得分:0)
NoSuchElementException
是因为当队列为空时调用q.remove()。
NullPointerException
因为p或txtName为空。
答案 2 :(得分:0)
由于您使用的是Netbeans,因此使用调试器应该很容易。你在NewJFrame.java:220有一个空指针异常?在那里设置一个断点,看看什么是NULL。如果它是合法地可能为NULL的情况,那么添加一些代码来有条件地执行操作。
如果是不应该为空的情况。然后,您必须深入研究代码,并了解使代码进入错误状态并修复它们的运行时条件。