2

What I am attempting to do is create 2 JComboBox's and 2 JTextField box's. I need to be able to write code that uses the temperature type (Fahrenheit, Celsius, and Kelvin) in the first JComboBox and converts that first temperature type into whichever temperature type has been selected in the 2nd JComboBox. This must be done by using whatever number has been entered into the first JTextField box (that will be the initial value of the selected temp type) and converted into the new temperature type in the 2nd JTextField box. Here is how far I've progressed...

I am getting a NullPointerException at line 40 when I run my test, and I don't know if I have formatted the double used in the if statement correctly to have the new value appear again as a String in the 2nd JTextField box. Before i go through with writing all of the other if statements to handle all of the other scenarios, I am looking for some pointers on if what I have done up till this point is correct.

package temperatureConverter;


import java.awt.FlowLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JTextField;

public class TempConverter extends JFrame
{
    private JComboBox firstComboBox;
    private JComboBox secondComboBox;
    private JTextField initialTemp;
    private JTextField convertedTemp;
    //private enum TempType { FAHRENHEIT, CELSIUS, KELVIN};
    private static final String[] tempType = { "Fahrenheit", "Celsius", "Kelvin" }; 

    public TempConverter()
    {
        super("Temperature Converter");
        setLayout(new FlowLayout());

        firstComboBox = new JComboBox(tempType);
        firstComboBox.setMaximumRowCount(3);
        firstComboBox.addItemListener(null);
        add(firstComboBox);
        secondComboBox = new JComboBox(tempType);
        secondComboBox.setMaximumRowCount(3);
        secondComboBox.addItemListener(null);
        add(secondComboBox);
        initialTemp = new JTextField ("", 10);
        initialTemp.addActionListener(null);
        add(initialTemp);
        convertedTemp = new JTextField ("", 10);
        convertedTemp.addActionListener(null);
        add(convertedTemp);
    }
    String theInitialTempType = (String) firstComboBox.getSelectedItem();
    String theTempTypeToConvertTo = (String) secondComboBox.getSelectedItem();
    String theChosenTemp = initialTemp.getSelectedText();
    String theNewTemp = convertedTemp.getSelectedText();

    private class textHandler implements ItemListener
    {
        public void itemStateChanged (ItemEvent event)
        {
            double convertedNumberForTheChosenTemp = Double.parseDouble(theChosenTemp);
            double convertedNumberForTheNewTemp = Double.parseDouble(theNewTemp);
            //String string1 = "";
            //String string2 = "";

            if ( theInitialTempType == tempType[0] && theTempTypeToConvertTo == tempType[1] )
            {
                 convertedNumberForTheNewTemp = (convertedNumberForTheChosenTemp   -  32)  *  5 / 9; 
                 String result = String.valueOf(convertedNumberForTheNewTemp);
            }
        }
    }
}
David Robinson
  • 77,383
  • 16
  • 167
  • 187
Jremy
  • 73
  • 1
  • 1
  • 8

2 Answers2

2
String theInitialTempType = (String) firstComboBox.getSelectedItem();

This code line is outside the constructor where the field is created. The attributes are used in other methods of the class, so the declaration String theAttribute needs to be outside the constructor.

On the other hand, the creation/initialization of the instance needs to be done after other fields are created, so at the end of the constructor, theAttribute = anotherAttribute.getSelectedText();

But even that is not correct. The fields are empty at that stage, so it makes no sense to try calculating results from them. Calculations should be controlled by the end user, and done on action. Look into ActionListener - it can be added to fields and will fire on Enter

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thompsonwhen I do that I get indicators in my if statement below saying the two arguments cant be resolved to variables. – Jremy Sep 01 '12 at 01:47
  • No i was saying sorry about not getting the @Andrew Thompson out correctly. When I do what you suggested the arguments in the if statement below highlight in red(eclipse IDE). Can you give me a hint on how I am supposed to get the value in my if statement to show up in the 2nd JTextfield box? – Jremy Sep 01 '12 at 02:08
  • For better help sooner, post an [SSCCE](http://sscce.org/). To make the current code into an SSCCE, add a `main` that shows the GUI in a `JOptionPane` or `JFrame`. – Andrew Thompson Sep 01 '12 at 02:15
  • 1
    Jremy: You'll have to move the initialization for all four [instance variables](http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html) into the constructor. And don't forget to `pack()`! – trashgod Sep 01 '12 at 02:18
  • pack()? what do you mean? new term. – Jremy Sep 01 '12 at 02:26
  • 1
    [`pack()`](http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack%28%29), shown [here](http://stackoverflow.com/a/12224343/230513). – trashgod Sep 01 '12 at 03:56
2

As you have two instances of JComboBox, this example shows how a selection in the first combo can change what's shown in the second combo. For example, selecting Fahrenheit in the first combo would change the second combo's model to display only Celsius or Kelvin. Etc.

Addendum: As @Andrew suggests, you'll have to move the initialization for all four instance variables into the constructor. Here's the main() I used to test your code:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            TempConverter f = new TempConverter();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);
        }
    });
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • See also [`CelsiusConverterGUI`](http://docs.oracle.com/javase/tutorial/uiswing/learn/). – trashgod Sep 01 '12 at 02:08
  • I don't need for that to happen. What I'm trying to have happen is the value that a person inputs to the 1st JTextfield box should represent the value in degrees of the temp type in the first JComboBox and in the 2nd JtextField box should then be converted to the new temperature that should correlate to whatever type of temperature scale has been selected in the 2nd JComboBox. – Jremy Sep 01 '12 at 02:11
  • 1
    OK, it's just a way to avoid the identity conversion. Updated to expand on @Andrew's suggestion. See also [*Initial Threads*](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod Sep 01 '12 at 02:24
  • thanks to the both of you for your help! I'm going to rearrange the code and see what new questions arise. I appreciate what you've done for me to this point. – Jremy Sep 01 '12 at 02:30