So I'm working on a small calculator applet and whenever a button is pressed (generating an action event) i get this: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException. Something about a missing source? The variable m is an instance of my myFunctions class which houses all the necessary formulas. I believe the problem lies in how the program is trying to access the class, but really have no idea how to remedy it. Here's my code:
package Module1;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ScientificCalculator extends JFrame implements ActionListener
{
JTextField tfield;
double a;
char ch;
JButton csc, sec, cot, abs, fac, prime, pi, det, ncr, npr, arccos, rToD, dToR, pow, log, e, ln, root, sin, cos, tan;
Container cont;
JPanel textPanel, buttonpanel;
myFunctions m;
ScientificCalculator()
{
cont = getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel = new JPanel();
tfield = new JTextField(25);
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent keyevent)
{
char c = keyevent.getKeyChar();
if (c >= '0' && c <= '9')
{
}
else
{
keyevent.consume();
}
}
});
textpanel.add(tfield);
buttonpanel = new JPanel();
buttonpanel.setLayout(new GridLayout(8, 4, 2, 2));
root = new JButton("n^(1/K)");
buttonpanel.add(root);
root.addActionListener(this);
log = new JButton("log(x)");
buttonpanel.add(log);
log.addActionListener(this);
abs = new JButton("abs(x)");
buttonpanel.add(abs);
abs.addActionListener(this);
prime = new JButton("Primes");
buttonpanel.add(prime);
prime.addActionListener(this);
pi = new JButton("\u03C0");
buttonpanel.add(pi);
pi.addActionListener(this);
det = new JButton("Det");
buttonpanel.add(det);
det.addActionListener(this);
npr = new JButton("nPr");
buttonpanel.add(npr);
npr.addActionListener(this);
ncr = new JButton("nCr");
buttonpanel.add(ncr);
ncr.addActionListener(this);
ln = new JButton("ln(x)");
buttonpanel.add(ln);
ln.addActionListener(this);
e = new JButton("e^x");
buttonpanel.add(e);
e.addActionListener(this);
pow = new JButton("n^k");
buttonpanel.add(pow);
pow.addActionListener(this);
arccos = new JButton("arccos(x)");
buttonpanel.add(arccos);
arccos.addActionListener(this);
rToD = new JButton("Rad-Deg");
buttonpanel.add(rToD);
rToD.addActionListener(this);
dToR = new JButton("Deg-Rad");
buttonpanel.add(dToR);
dToR.addActionListener(this);
sin = new JButton("sin(x)");
buttonpanel.add(sin);
sin.addActionListener(this);
cos = new JButton("cos(x)");
buttonpanel.add(cos);
cos.addActionListener(this);
tan = new JButton("tan(x)");
buttonpanel.add(tan);
tan.addActionListener(this);
csc = new JButton("csc(x)");
buttonpanel.add(csc);
csc.addActionListener(this);
sec = new JButton("sec(x)");
buttonpanel.add(sec);
sec.addActionListener(this);
cot = new JButton("cot(x)");
buttonpanel.add(cot);
cot.addActionListener(this);
fac = new JButton("n!");
fac.addActionListener(this);
buttonpanel.add(fac);
cont.add("Center", buttonpanel);
cont.add("North", textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("log(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.log(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("e^x"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.e(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("n^(1/3)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.sqrt(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("sin(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.sin(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("cos(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.cos(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("tan(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.tang(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("csc(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = 1 / m.sin(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("sec(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = 1 / m.cos(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("cot(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = 1 / m.tang(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("abs(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.abs(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("arccos(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.acos(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("ln(x)"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("\u03C0"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.pi();
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("det"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("nPr"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("nCr"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = Math.tan(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("Rad-Deg"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.rToD(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("Deg-Rad"))
{
if (tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a = m.dToR(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
if (s.equals("n!"))
{
if (tfield.getText().equals(""))
{
tfield.setText("Enter Number");
}
else
{
a = m.factorial((double)Integer.parseInt(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText() + a);
}
}
tfield.requestFocus();
}
public static void main(String args[])
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e)
{
}
ScientificCalculator f = new ScientificCalculator();
f.setTitle("ScientificCalculator");
f.pack();
f.setVisible(true);
}
}