0

I have been working on a class project and have one program that I cannot figure out. It is supposed to convert the temperature from F to C and visa versa. When I try to change the temp. format from F to C (F is the default) in the comboBox, the program locks up. Anyone point me in the right direction?

// Create and format Temperature Calculator Tab 
private void TempCalcTab(){

    // Format panel
    JPanel tempCalcPanel = new JPanel();
    tempCalcPanel.setLayout(null);
    this.tabbedPane.addTab("Temp Calc", tempCalcPanel);

    //Create, format and add components to panel
    JLabel tempLabel = new JLabel("Enter Temperature:");
    tempLabel.setSize(115, 20);
    tempLabel.setLocation(10, 40);
    tempCalcPanel.add(tempLabel);
    tempText = new JTextField();
    tempText.setSize(120, 20);
    tempText.setLocation(140, 40);
    tempText.setText("0");
    tempCalcPanel.add(tempText);
    //******************************************************************
    JLabel resultLabel = new JLabel("Result:");
    resultLabel.setSize(45, 20);
    resultLabel.setLocation(10, 80);
    tempCalcPanel.add(resultLabel);
    resultLabel = new JLabel("F");
    resultLabel.setSize(15, 20);
    resultLabel.setLocation(280, 80);
    tempCalcPanel.add(resultLabel);
    //******************************************************************
    resultText = new JTextField();
    resultText.setSize(120, 20);
    resultText.setLocation(140, 80);
    resultText.setEditable(false);
    resultText.setText("32");
    tempCalcPanel.add(resultText);
    //******************************************************************
    comboBox = new JComboBox(new String[] {"C", "F"});
    comboBox.setSize(90, 25);
    comboBox.setLocation(280, 40);
    comboBox.setEditable(false);
    comboBox.addItemListener(new ItemListener(){ 
        public void itemStateChanged(ItemEvent e){ 
            comboBoxState(); 
            }
        });
    tempCalcPanel.add(comboBox);
    //******************************************************************
    JButton convertButton = new JButton("Convert");
    convertButton.setSize(150, 25);
    convertButton.setLocation(35, 120);
    tempCalcPanel.add(convertButton);
    convertButton.setMnemonic('C');
    convertButton.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
            convertTemperature(); 
            }
        });
    //******************************************************************
    JButton exitButton = new JButton("Exit");
    exitButton.setSize(100, 25);
    exitButton.setLocation(190, 120);
    tempCalcPanel.add(exitButton);
    exitButton.setMnemonic('X');
    exitButton.addActionListener(new ActionListener(){ 
        public void actionPerformed(ActionEvent e){ 
            closeProgram(); 
            }
        });
}// End TempCalcTab method

// Calculating and Formatting Temperature Calculator

// Formatting comboBox for F or C
private void comboBoxState(){
    if(comboBox.getSelectedItem().toString().equals("C")){
        resultLabel.setText("F");
    }
    else{
        resultLabel.setText("C");
    }
}// End comboBoxState method

// Formatting and calculating temperature conversions 
private void convertTemperature(){

    // Declare variables
    double temperature = 0.0;
    double result = 0.0;

    // Validating input
    if(tempText.getText().length() < 1){
        tempText.setText("0");
    }

    try{
        temperature = Double.parseDouble(tempText.getText());
    } 

    catch(Exception ex){
        temperature = 0.0;
    }

    // Converting to celsius or fahrenheit 
    if(comboBox.getSelectedItem().toString().equals("C")){
        result = (temperature  *  9/5) + 32;
    }

    else{
        result = (temperature  -  32)  *  5/9;
    }

    // Format and display results
    DecimalFormat decimalFormat = new DecimalFormat("##.##");
    resultText.setText(decimalFormat.format(result));
}// End convert temperature method
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jim
  • 51
  • 6
  • Is there any exception trace? – Justin Jasmann Oct 19 '13 at 01:15
  • Well I got it to stop locking up. Now I get the error message "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" – Jim Oct 19 '13 at 01:27
  • That's what I thought. Could you include the full trace in your post? – Justin Jasmann Oct 19 '13 at 01:39
  • Exception in thread "AWT-EventQueue-0"java.lang.NullPointerException at engineeringSpecificationInterface.EngineeringSpecificationInterface.comboBoxState(EngineeringSpecificationInterface. java:783) at engineeringSpecificationInterface.EngineeringSpecificationInterface.access$3(EngineeringSpecificationInterface.java: 778) at engineeringSpecificationInterface.EngineeringSpecificationInterface$6.itemStateChanged(EngineeringSpecificationInter face.java:280) – Jim Oct 19 '13 at 01:45
  • @ Justin Jasmann There is much more than that, just hate to fill the page up with it. Trying to work thru it. Seems to be with the comboBoxState method. Not sure where my null it, unless I am just brain dead. Which could be at this point. – Jim Oct 19 '13 at 01:49
  • 1
    My guess would be that `comboBox.getSelectedItem()` is returning null. Thus, calling `toString()` on it would throw a `NullPointerException`. You're getting the error at `EngineeringSpecificationInterface.java` at line 783, so if you debug around there you should find the exact problem. – Justin Jasmann Oct 19 '13 at 01:56
  • Line 783 is `resultLabel.setText("C");` I guess I am just burnt. Not understanding why it is throwing that error. – Jim Oct 19 '13 at 02:19
  • you must be calling comboBoxState() method before initializing the resultTable label – kdureidy Oct 19 '13 at 04:42
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Oct 19 '13 at 05:57

2 Answers2

0

I don't have enough rep to comment so I will take a shot at this. I see you have:

comboBox = new JComboBox(new String[] {"C", "F"});

But where is it originally initialized? Is it in the same class since it is private? For instance, I do not see something like:

JComboBox comboBox = new JComboBox(new String[] {"C", "F"});

In your code. Might be the reason why comboBoxState() can't access it? I would like to see more of EngineeringSpecificationInterface.java. Is that were this snip it of code is from?

Ducksauce88
  • 640
  • 3
  • 12
  • 26
  • `EngineeringSpecificationInterface.java` is about 1000 lines. The global variables for this method are `private JTextField tempText, resultText; private JComboBox comboBox; private JLabel resultLabel;` And in the constructor it calls `TempCalcTab` – Jim Oct 19 '13 at 02:31
  • Maybe try setting `comboBox.getSelectedItem().toString()` to a string somewhere else in your code, then calling the `variable.equals("C")` to see if you get errors in both locations. Maybe then you can try to pinpoint it down even more to see if it IS really what is getting you caught. What are the exact lines at `783`, `778` and `280`? – Ducksauce88 Oct 19 '13 at 02:37
0

Do you have a resultLabel stored as a class field in addition to the one you're initializing in your example? If so, your local resultLabel in your constructor is masking it - the constructor declares its own variable with that name then initializes it (and re-initializes it to add the second label), so the class-level label is still null when you get to comboBoxState(). You'll want to rename the first resultLabel variable to something else and keep the second label initialization as is.

Josh
  • 1,563
  • 11
  • 16
  • I tried renaming the field, same outcome. That is what is frustrating, I have done everything I have read. Out of the book and on all of the boards. I know it will be something stupid. – Jim Oct 19 '13 at 02:43
  • Did you rename using an IDE's refactoring capability? That would probably rename both, recreating your problem but using a different variable name. If the line in the stack trace is pointing to the `setText()` method, your label's most definitely the thing that's `null`. – Josh Oct 19 '13 at 02:47
  • Oh, re-read your comment - if you renamed the class field, you'll need to look at your constructor and rename the second `resultLabel` initializer to match it - starting at `resultLabel = new JLabel("F");` – Josh Oct 19 '13 at 02:48