我很抱歉,如果这看起来显而易见,我对Java以及一般的编程都是相当新的。 对于我的程序,我希望用户在JTable中输入一组10个数字,然后我将提取并获取统计数据。 提取代码如下:
public double [] getTableContents()
{
Double [] table = new Double[10];
double [] realTable = new double[10];
Object [] objValue = new Object[10];
for(int i =0; i<inputTable.getRowCount();i++)
{
table[i] = new Double(Double.valueOf(inputTable.getValueAt(i, 0).toString())); //error occurs here
realTable[i] = table[i].doubleValue();
}
return realTable;
}
奇怪的是,这偶尔会起作用,然后偶尔会给我一个空指针异常。我无法确定何时可行的模式。 之前我扩展了代码并发现问题出现在我试图将值转换为字符串的地方(.toString()方法)。 经过大约一个半小时的研究后,我无法解决问题,如果发布了类似的问题,我很抱歉,但我不知道将我所见的应用到我的代码中 - 所有其他的例子都是更复杂。 在此先感谢您的帮助!
EDIT 我在这里定义表:
public class GUI extends JFrame
{
JTable inputTable;
JButton computeButton;
JLabel modeLabel;
JLabel outputModeLabel;
JLabel averageLabel;
JLabel outputAverageLabel;
JLabel minLabel;
JLabel maxLabel;
JLabel outputMaxLabel;
JLabel outputMinLabel;
public GUI()
{
computeButton = new JButton("Compute");
modeLabel = new JLabel("Mode: ");
outputModeLabel = new JLabel("None");
inputTable = new JTable(10, 1);
averageLabel = new JLabel("Average: ");
outputAverageLabel = new JLabel("None");
minLabel = new JLabel("Min:");
maxLabel = new JLabel("Max");
outputMinLabel = new JLabel("None");
outputMaxLabel = new JLabel("None");
GridBagLayout layout = new GridBagLayout();
getContentPane().setLayout(layout);
addComponent(inputTable, 0, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.1, 0.1);
addComponent(computeButton, 0, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.1, 0.1);
addComponent(modeLabel, 1, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.CENTER, 0.4, 0.1);
addComponent(outputModeLabel, 2, 0, 1, 1, GridBagConstraints.NONE, GridBagConstraints.WEST, 0.4, 0.1);
addComponent(averageLabel, 1, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTH, 0.4, 0.4);
addComponent(outputAverageLabel,2, 1, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.4, 0.4);
addComponent(maxLabel, 1, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTH, 0.4, 0.4);
addComponent(outputMaxLabel, 2, 2, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.4, 0.4);
addComponent(minLabel, 1, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTH, 0.4, 0.4);
addComponent(outputMinLabel, 2, 3, 1, 1, GridBagConstraints.NONE, GridBagConstraints.NORTHWEST, 0.4, 0.4);
computeButton.addActionListener(new ComputeButtonListener(this));
}
private void addComponent(Component comp, int conGridX, int conGridY, int conGridWid, int conGridHei, int conFill, int conAnchor, double conWeightX, double conWeightY)
{
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = conGridX;
constraints.gridy = conGridY;
constraints.gridwidth = conGridWid;
constraints.gridheight = conGridHei;
constraints.fill = conFill;
constraints.anchor = conAnchor;
constraints.weightx = conWeightX;
constraints.weighty = conWeightY;
getContentPane().add(comp,constraints);
}
错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Eight5.GUI.getTableContents(GUI.java:76)
at Eight5.ComputeButtonListener.calcMode(ComputeButtonListener.java:26)
at Eight5.ComputeButtonListener.actionPerformed(ComputeButtonListener.java:18)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
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:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
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:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
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)