我想把JTabbedPane页面输入的信息存放到一个对象里,再加进arraylist里,页面有一个添加的按钮,我给按钮加了个监听器,当点下按钮时,获取文本域的信息,存放到对象里。第一次点按钮时,信息添加成功,修改下信息后第二次点添加,就会报错。代码如下:
- Java code
private void jbClassActionPerformed(java.awt.event.ActionEvent evt) { Classes newcla = new Classes(); newcla.grades = Integer.parseInt(classText.getText()); //grades是int型的,初步调试发现该处出错 newcla.major = majorText.getText(); newcla.classNum = Integer.parseInt(classNumText.getText();); newcla.numOfStu = Integer.parseInt(stuNumText.getText()); if (newcla.grades == 0 || newcla.major == null || newcla.classNum == 0 || newcla.numOfStu == 0) { JOptionPane.showMessageDialog(null, "输入的信息有误,请重新输入"); } else { newcla.stu = new Student[newcla.numOfStu]; for (int i = 0, j = 0; i < newcla.numOfStu; i++) { newcla.stu[i] = new Student(); newcla.stu[i].name = (String) classTable.getValueAt(i, j); newcla.stu[i].id = (String) classTable.getValueAt(i, j + 1); j = 0; } cla.add(newcla); } classText.setText(" "); majorText.setText(" "); classNumText.setText(" "); stuNumText.setText(" "); }
错误信息如下:Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 4"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:497)
at testgrade.TestGrade.jbClassActionPerformed(TestGrade.java:387)
at testgrade.TestGrade.access$100(TestGrade.java:14)
at testgrade.TestGrade$3.actionPerformed(TestGrade.java:134)
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:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.Window.dispatchEventImpl(Window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
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)
经过我的调试,我发现是把String转化为int时出错,我如果把grades等变量改成String型的就不会出错,为什么?
------解决方案--------------------
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: " 4"