-1

Possible Duplicate:
Can anyone help me identify the Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException on Line 65?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at SelectUnitFrame.<init>(SelectUnitFrame.java:112)
    at CoolingTowerFrame$ButtonListener.actionPerformed(CoolingTowerFrame.java:184)
    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)

Code is long but i can edit this post and add if it would help

i think the code is not working because of this [line 112] tableButton.setRolloverEnabled(false); the reason i need the rollover disabled is so that when the button changes when a button is pushed it does not return to the original image. If there are any fixes for that, that may work too.

heres the code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class SelectUnitFrame extends JFrame
{
  public JLabel showNumbersLabel;
  public JPanel showPanel = new JPanel();
  public JButton tableButton;
  public JPanel buttonPanel = new JPanel();
  public JButton goBackButton = new JButton("Change Variables");
  public JButton exitButton = new JButton("Done");
  public JButton nextButton = new JButton(">");
  public JButton prevButton = new JButton("<");
  public int fRate;
  public int wbTemp;
  public int r;
  public int a; 
  public int rcTons;
  public int count;
  public String [] tablesFor80 = {"80F_FG2003.GIF", "80F_FG2004.GIF", "80F_FG2005.GIF", "80F_FG2007.GIF", "80F_FG2009.GIF", "80F_FG2011.GIF", "80F_FG2015.GIF"};
  public String [] tablesFor78 = {"78F_FG2003.GIF", "78F_FG2004.GIF", "78F_FG2005.GIF", "78F_FG2007.GIF", "78F_FG2009.GIF", "78F_FG2011.GIF", "78F_FG2015.GIF"};
  public String [] tablesFor76 = {"76F_FG2003.GIF", "76F_FG2004.GIF", "76F_FG2005.GIF", "76F_FG2007.GIF", "76F_FG2009.GIF", "76F_FG2011.GIF", "76F_FG2015.GIF"};
  public String [] tablesFor74 = {"74F_FG2003.GIF", "74F_FG2004.GIF", "74F_FG2005.GIF", "74F_FG2007.GIF", "74F_FG2009.GIF", "74F_FG2011.GIF", "74F_FG2015.GIF"};
  public String [] tablesFor72 = {"72F_FG2003.GIF", "72F_FG2004.GIF", "72F_FG2005.GIF", "72F_FG2007.GIF", "72F_FG2009.GIF", "72F_FG2011.GIF", "72F_FG2015.GIF"};
  public String [] tablesFor70 = {"70F_FG2003.GIF", "70F_FG2004.GIF", "70F_FG2005.GIF", "70F_FG2007.GIF", "70F_FG2009.GIF", "70F_FG2011.GIF", "70F_FG2015.GIF"};
  public String [] tablesFor68 = {"68F_FG2003.GIF", "68F_FG2004.GIF", "68F_FG2005.GIF", "68F_FG2007.GIF", "68F_FG2009.GIF", "68F_FG2011.GIF", "68F_FG2015.GIF"};
  public ImageIcon tablePicture;   


  public SelectUnitFrame(int flowRate, int wetBulbTemp, int roundedCoolingTons, int approach, int range)
  {
    super("Unit Selected");
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimensions = toolkit.getScreenSize();
    int x = (dimensions.width - 750)/2; 
    int y = (dimensions.height - 350)/2;
    setBounds(x, y, 750, 350); 

    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    goBackButton.addActionListener(new ButtonListener());
    exitButton.addActionListener(new ButtonListener());
    nextButton.addActionListener(new ButtonListener());
    prevButton.addActionListener(new ButtonListener());

    fRate = flowRate;
    wbTemp = wetBulbTemp;
    r = range;
    a = approach;
    rcTons = roundedCoolingTons;

    buttonPanel.add(goBackButton);
    buttonPanel.add(exitButton);

    add(buttonPanel, BorderLayout.SOUTH);

    showNumbersLabel = new JLabel("Wet Bulb: " + wbTemp + "(deg)F     " + "Range: " + r + "     Approach: " + a + "     Tons/GPM: " + rcTons + "/" + fRate);
    showPanel.add(showNumbersLabel);

    add(showPanel, BorderLayout.NORTH);

    if ((wbTemp >= 79) && (wbTemp < 82))
    {
      tempSelection80(flowRate, wetBulbTemp, roundedCoolingTons, approach, range);
    }
    else if ((wbTemp < 79 ) && (wbTemp >= 77))
    {
      tempSelection78(flowRate, wetBulbTemp, roundedCoolingTons, approach, range);
    }  
    else if ((wbTemp < 77) && (wbTemp >= 75))
    {
      tempSelection76(flowRate, wetBulbTemp, roundedCoolingTons, approach, range);
    } 
    else if ((wbTemp < 75) && (wbTemp >= 73))
    {
      tempSelection74(flowRate, wetBulbTemp, roundedCoolingTons, approach, range);
    }
    else if ((wbTemp < 73) && (wbTemp >= 71))
    {
      tempSelection72(flowRate, wetBulbTemp, roundedCoolingTons, approach, range);
    }
    else if ((wbTemp < 71) && (wbTemp >= 69))
    {
      tempSelection70(flowRate, wetBulbTemp, roundedCoolingTons, approach, range);
    }
    else if ((wbTemp < 69) && (wbTemp >= 67))
    {
      tempSelection68(flowRate, wetBulbTemp, roundedCoolingTons, approach, range);
    }
    else
    {
      tableButton = new JButton("Cooling Tower Cannot Handle Temperatures In This Wet Bulb");
    }
    tableButton.setRolloverEnabled(false);
    add(nextButton, BorderLayout.EAST);
    add(prevButton, BorderLayout.WEST);
    add(tableButton, BorderLayout.CENTER);
  }
  private class ButtonListener implements ActionListener
  {
    @Override
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == goBackButton)
      {
        dispose();
      }
      else if (e.getSource() == exitButton)
      {
        System.exit(0);
      }
      else if (e.getSource() == nextButton)
      {
        String t = tableButton.getText();
        tableButton.updateUI();
        String s = ((ImageIcon)tableButton.getIcon()).toString();
        if ((wbTemp >= 79) && (wbTemp < 82))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor80[i]))
              {
                if (i == 6)
                {
                  count = 0;
                }
                else
                {
                  count = (i+1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor80[count], tablesFor80[count]));
        }
        else if ((wbTemp < 79 ) && (wbTemp >= 77))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor78[i]))
              {
                if (i == 6)
                {
                  count = 0;
                }
                else
                {
                  count = (i+1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor78[count], tablesFor78[count]));
        }
        else if ((wbTemp < 77) && (wbTemp >= 75))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor76[i]))
              {
                if (i == 6)
                {
                  count = 0;
                }
                else
                {
                  count = (i+1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor76[count], tablesFor76[count]));
        }
        else if ((wbTemp < 75) && (wbTemp >= 73))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor74[i]))
              {
                if (i == 6)
                {
                  count = 0;
                }
                else
                {
                  count = (i+1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor74[count], tablesFor74[count]));
        }
        else if ((wbTemp < 73) && (wbTemp >= 71))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor72[i]))
              {
                if (i == 6)
                {
                  count = 0;
                }
                else
                {
                  count = (i+1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor72[count], tablesFor72[count]));
        }
        else if ((wbTemp < 71) && (wbTemp >= 69))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor70[i]))
              {
                if (i == 6)
                {
                  count = 0;
                }
                else
                {
                  count = (i+1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor70[count], tablesFor70[count]));
        }
        else if ((wbTemp < 69) && (wbTemp >= 67))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor68[i]))
              {
                if (i == 6)
                {
                  count = 0;
                }
                else
                {
                  count = (i+1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor68[count], tablesFor68[count]));
        }
        tableButton.setRolloverEnabled(false);
        add(tableButton, BorderLayout.CENTER);
      }
      else if (e.getSource() == prevButton)
      {
        String t = tableButton.getText();
        tableButton.updateUI();
        String s = ((ImageIcon)tableButton.getIcon()).toString();
        if ((wbTemp >= 79) && (wbTemp < 82))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor80[i]))
              {
                if (i == 0)
                {
                  count = 6;
                }
                else
                {
                  count = (i-1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor80[count], tablesFor80[count]));
        }
        else if ((wbTemp < 79 ) && (wbTemp >= 77))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor78[i]))
              {
                if (i == 0)
                {
                  count = 6;
                }
                else
                {
                  count = (i-1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor78[count], tablesFor78[count]));
        }
        else if ((wbTemp < 77) && (wbTemp >= 75))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor76[i]))
              {
                if (i == 0)
                {
                  count = 6;
                }
                else
                {
                  count = (i-1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor76[count], tablesFor76[count]));
        }
        else if ((wbTemp < 75) && (wbTemp >= 73))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor74[i]))
              {
                if (i == 0)
                {
                  count = 6;
                }
                else
                {
                  count = (i-1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor74[count], tablesFor74[count]));
        }
        else if ((wbTemp < 73) && (wbTemp >= 71))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor72[i]))
              {
                if (i == 0)
                {
                  count = 6;
                }
                else
                {
                  count = (i-1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor72[count], tablesFor72[count]));
        }
        else if ((wbTemp < 71) && (wbTemp >= 69))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
            for (int i = 0; i <= 6; i++)
            {
              if (s.equals(tablesFor70[i]))
              {
                if (i == 0)
                {
                  count = 6;
                }
                else
                {
                  count = (i-1);
                }
              }
            }
          }
          tableButton = new JButton(new ImageIcon(tablesFor70[count], tablesFor70[count]));
        }
        else if ((wbTemp < 69) && (wbTemp >= 67))
        {
          if (t.equals("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables."))
          {
            count = 0;
          }
          else
          {
          for (int i = 0; i <= 6; i++)
          {
            if (s.equals(tablesFor68[i]))
            {
              if (i == 0)
              {
                count = 6;
              }
              else
              {
                count = (i-1);
              }
            }
          }
          }
          tableButton = new JButton(new ImageIcon(tablesFor68[count], tablesFor68[count]));
        }
        tableButton.setRolloverEnabled(false);
        add(tableButton, BorderLayout.CENTER);
      }
    }
  }

the issue is with this statement in a method that is called the body of it is too long for me to add it

else
      {
        tableButton = new JButton("Cannot Find Model for Variables Given. Adjust or Scroll to Look Through Tables.");
      }

}
Community
  • 1
  • 1
user1469139
  • 13
  • 1
  • 9
  • Please include the code on line 112 of the `SelectUnitFrame` class. – user1329572 Jun 29 '12 at 15:08
  • 2
    You already asked this here: http://stackoverflow.com/questions/11141425/can-anyone-help-me-identify-the-exception-in-thread-awt-eventqueue-0-java-lang – Evan Mulawski Jun 29 '12 at 15:09
  • It's true that the tableButton referenced in line 112 is the source of your null pointer, but it's impossible to tell you how to fix it without seeing WHY it is null in your code. – Rob Wagner Jun 29 '12 at 15:09
  • i did `tableButton.setRolloverEnabled(false);` – user1469139 Jun 29 '12 at 15:09
  • We need to see the code around 112, to see why your tableButton is null. – Rob Wagner Jun 29 '12 at 15:10
  • @EvanMulawski This is another NullPointerException, but not the same problem. – Rob Wagner Jun 29 '12 at 15:11
  • 1
    When you receive a NullPointerException, it means something is null. On the line you quote, either `tableButton` is null or (unlikely) something in the `setRolloverEnabled` method is null. – assylias Jun 29 '12 at 15:13
  • @Recursed Then the OP needs to learn how to debug on his own. – Evan Mulawski Jun 29 '12 at 15:13
  • 2
    OP, you won't get much help because you don't know how to allow us to help you. Forget about GUI, your problem is basic Java. You have a property whose value is `null` at the time you want to invoke a method on it. – Marko Topolnik Jun 29 '12 at 15:15
  • @assylias Good point. One point probably wrong, the other redundant after edit. Comment deleted. – Andrew Thompson Jun 29 '12 at 15:24

1 Answers1

0

tableButton is null unless the else part of your long list of if / else if / else is run.

In other words, if wbTemp is between 69 and 82, you will get a NullPointerException.

Conclusion: you need to have a statement like tableButton = new JButton(...) that is run if wbTemp is between 69 and 82.

EDIT

To address your comment, I have added a code example below, where:

  • If condition1 is true, only Block 1 is run
  • If condition1 is false AND condition2 is true, only Block 2 is run
  • If condition1 is false AND condition2 is false, only Block 3 is run

I would suggest you read about the basics of the language, for example the Java Tutorial

if(condition1) {
    //(Block 1)
} else if(condition2) {
    //(Block 2)
} else {
    //(Block 3)
}
assylias
  • 321,522
  • 82
  • 660
  • 783
  • there is a tableButton set for that wbTemp range... its the last else statement in the constructor of this frame it will show up as `tableButton = new JButton("Cooling Tower Cannot Handle Temperatures In This Wet Bulb");` – user1469139 Jun 29 '12 at 15:40