Question

I keep getting a Run Time error that says I am having an Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException it is saying a [line 65] but to me it just looks like a basic if statement. I can give some background. this frame is call from a sequence of frames that is then once the state is selected sent to a new frame that contains states A-C cities. This current frame will work only when Alabama is not the state selected. (I have not created the other frames for the other states hence why they are commented out) I can put up the code to the other frame it is supposed to call if anyone needs it but I figured that this was a lot of code to begin with. Thanks in advance for the help everybody.

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 here's the error when selecting Alabama.

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)
Was it helpful?

Solution

Line 65 is this one:

if (state.equals("Alabama"))

If you look through the previous code, nothing has initialized state so its value will be null.

OTHER TIPS

The state is null when you try to invoke equals on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top