My code compiles, but won't run; java.lang.NullPointerException?

My code compiles, its for a Fahrenheit-Celsius converter GUI, but it does not run. The following pops up ...

Exception in thread "main" java.lang.NullPointerException

at Temp1.<init>(Temp1.java:35)

at Temp1.main(Temp1.java:17)

Press any key to continue . . .

This is what my code looks like

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass Temp1extends JFrameimplements ActionListener{

private JTextField jtfNum, jtfCF, jtfResult, jtfResultCF;

private JButton jbtCelF, jbtFahC, jbtClear, jbtClearBoth, jbtClearNum, jbtClearCF, jbtClose;

privatefloat num1, num2, celfah, fahcel;

privatechar cf;

publicstaticvoid main(String [] args){

Temp1 temp =new Temp1();

temp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

temp.setSize(800,100);

temp.setTitle("Temperature C/F Converter");

temp.setVisible(true);

}

public Temp1(){

JPanel p1 =new JPanel();

p1.setLayout(new FlowLayout());

p1.add(new JLabel("Degrees : "));

p1.add(jtfNum =new JTextField(10));

p1.add(new JLabel("C/F : "));

p1.add(jtfCF =new JTextField(2));

p1.add(new JLabel("Result : "));

p1.add(jtfResult =new JTextField(10));

jtfResult.setEditable(false);

p1.add(jtfResult =new JTextField(2));

jtfResultCF.setEditable(false);

JPanel p2 =new JPanel();

p2.setLayout(new FlowLayout());

p2.add(jbtCelF =new JButton("Convert to Fahrenheit"));

p2.add(jbtFahC =new JButton("Convert to Celsius"));

p2.add(jbtClose =new JButton("Exit Program"));

p2.add(jbtClearNum =new JButton("Clear Degrees"));

p2.add(jbtClearCF =new JButton("Clear C/F"));

p2.add(jbtClearBoth =new JButton("Clear Degrees and C/F"));

p2.add(jbtClear =new JButton("Clear All"));

getContentPane().setLayout(new BorderLayout());

getContentPane().add(p1, BorderLayout.NORTH);

getContentPane().add(p2, BorderLayout.CENTER);

jbtCelF.addActionListener(this);

jbtFahC.addActionListener(this);

jbtClearNum.addActionListener(this);

jbtClearCF.addActionListener(this);

jbtClearBoth.addActionListener(this);

jbtClear.addActionListener(this);

jbtClose.addActionListener(this);

}

publicvoid actionPerformed(ActionEvent e){

String actionCommand = e.getActionCommand();

if (e.getSource()instanceof JButton){

if ("Convert to Fahrenheit".equals(actionCommand))

calculate1();

if ("Convert to Celsius".equals(actionCommand))

calculate2();

else

if ("Clear All".equals(actionCommand)){

jtfCF.setText(" ");

jtfNum.setText(" ");

jtfResult.setText(" ");

}

if ("Clear Degrees and C/F".equals(actionCommand)){

jtfCF.setText(" ");

jtfNum.setText(" ");

}

if ("Clear Degrees".equals(actionCommand)){

jtfNum.setText(" ");

}

if ("Clear C/F".equals(actionCommand)){

jtfCF.setText(" ");

}

}

else

if ("Exit Program".equals(actionCommand))

System.exit(0);

}

privatevoid calculate1(){

float celfah = ((5/9)*(num1 - 32));

float num1 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (jtfCF.getText().charAt(0));

if (cf == (' '))

jtfCF.setText("C");

if (cf =='F')

jtfCF.setText("F");

jtfResultCF.setText("F");

jtfResult.setText(" + num 1 + ");

if (cf =='C')

jtfCF.setText("C");

jtfResult.setText(" + celfah + ");

jtfResultCF.setText("F");

}

privatevoid calculate2(){

float fahcel = ((9/5)*(num2) + 32);

float num2 = (Float.parseFloat(jtfNum.getText().trim()));

char cf = (jtfCF.getText().charAt(0));

if (cf == (' '))

jtfCF.setText("C");

if (cf =='C')

jtfResultCF.setText("C");

jtfResult.setText(" + num2 + ");

if (cf =='F')

jtfResult.setText(" + fahcel + ");

jtfResultCF.setText("C");

}

}

[8340 byte] By [an23dya] at [2007-11-27 11:22:58]
# 1

Hi

i compiled and ran the code, i get slightly different runtime error (different line numbers)

from what i can see, in line 30

jtfResultCF.setEditable(false);

you attempt to call the setEditable() method on a non initialised reference. You do have the jtfResultCF reference declared in the class, but it is not pointing anywhere (no object assigned)

adding

jtfResultCF = new JTextField();

before line 30 will stop the nullpointer from occuring, but there still seem to be some problems in the code

good luck!

Vectorizeda at 2007-7-29 14:57:53 > top of Java-index,Java Essentials,New To Java...
# 2

This is probebly because you didnt allocated jtfResultCF variable memory (didnt use new ).

confused_in_javaa at 2007-7-29 14:57:53 > top of Java-index,Java Essentials,New To Java...
# 3

> This is probebly because you didnt allocated

> jtfResultCF variable memory (didnt use new ).

Stop babbling nonsense. "new" doesn't allocate variable memory:

Object o; // <-- variable created, 4 bytes used, no "new"

Declaring the variable does. "new" constructs a new instance of a class, invoking the c'tor specified.

CeciNEstPasUnProgrammeura at 2007-7-29 14:57:53 > top of Java-index,Java Essentials,New To Java...