我不断收到一个运行时错误,说我在线程“AWT-EventQueue-0”java.lang.NullPointerException 中有一个异常,它说的是 [第 65 行],但对我来说,它看起来就像一个基本的 if 语句。我可以提供一些背景。该帧是从一系列帧中调用的,一旦选择了状态,就会将其发送到包含州 AC 城市的新帧。此当前框架仅在阿拉巴马州不是所选州时才有效。(我没有为其他状态创建其他框架,因此为什么它们被注释掉了)我可以将代码放到另一个框架,如果有人需要它应该调用它,但我认为这是很多代码开始和。提前感谢大家的帮助。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class WetBulbByLocationFrameStates3 extends JFrame
{
public JLabel stateLabel;
public JPanel statePanel = new JPanel();
public JComboBox stateBox;
public String [] unitedStates = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "South Pacific Islands", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};
public String [] canadianProvinces = {"Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland and Labrador", "Northwest Territories", "Nova Scotia", "Nunavut", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan", "Yukon Territory"};
public JButton nextButton = new JButton("Next");
public JPanel buttonPanel = new JPanel();
public String state;
public String countryGIVEN;
public WetBulbByLocationFrameStates3(String country)
{
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimensions = toolkit.getScreenSize();
int x = (dimensions.width - 315)/2;
int y = (dimensions.height - 250)/2;
setBounds(x, y, 315, 250);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
countryGIVEN = country;
nextButton.addActionListener(new ButtonListener3());
if (country.equals("United States of America"))
{
stateBox = new JComboBox(unitedStates);
setTitle("Select State");
stateLabel = new JLabel("State");
}
else if (country.equals("Canada"))
{
stateBox = new JComboBox(canadianProvinces);
setTitle("Select Province");
stateLabel = new JLabel("Province");
}
stateBox.addActionListener(new ComboBoxListener2());
statePanel.add(stateLabel);
statePanel.add(stateBox);
add(statePanel, BorderLayout.CENTER);
buttonPanel.add(nextButton);
add(buttonPanel, BorderLayout.SOUTH);
}
private class ButtonListener3 implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (countryGIVEN.equals("United States of America"))
{
if (state.equals("Alabama"))
{
JFrame wbfs1 = new WetBulbByLocationFrameStateGroupA_C(state);
}
else if (state.equals("Alaska"))
{
JFrame wbfs2 = new WetBulbByLocationFrameStateGroupA_C(state);
}
else if (state.equals("Arizona"))
{
JFrame wbfs3 = new WetBulbByLocationFrameStateGroupA_C(state);
}
else if (state.equals("Arkansas"))
{
JFrame wbfs4 = new WetBulbByLocationFrameStateGroupA_C(state);
}
else if (state.equals("California"))
{
JFrame wbfs5 = new WetBulbByLocationFrameStateGroupA_C(state);
}
else if (state.equals("Colorado"))
{
JFrame wbfs6 = new WetBulbByLocationFrameStateGroupA_C(state);
}
else if (state.equals("Connecticut"))
{
JFrame wbfs7 = new WetBulbByLocationFrameStateGroupA_C(state);
}
}
}
}
private class ComboBoxListener2 implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
state = (String)stateBox.getSelectedItem();
}
}
}
PS这里是选择阿拉巴马州时的错误。
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at WetBulbByLocationFrameStates3$ButtonListener3.actionPerformed(WetBulbByLocationFrameStates3.java:65)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)