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);
        }
}

can you please provide the complete error message?
it tells you on which line the exception is thrown, so if you look at that line in your source code, it should be easier to find out where it's going wrong.

**at line 15 **you have taken the support of some class

myFunctions m;

you haven't instantiated this variable anywhere in your code but you called some methods by using that reference somewhere like line no 331, 344, 357

thats causing the exception check that once

create an object for that class is the solution for your problem

and let me know the status after the modification

It depends on whether the methods in myFunctions are static or not. (They probably should be static).
If they are static you call them with the class name, eg myFunctions.sin
If not, then see radhakrishna's post above.

ps Java naming comventions: class names should begin with a capital letter

this is the error i get after initializing m as a new instance of myFunctions():

Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Insets.<init>(Unknown Source)
    at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
    at sun.awt.windows.WToolkit.getScreenInsets(Unknown Source)
    at java.awt.Window.init(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Frame.<init>(Unknown Source)
    at java.awt.Frame.<init>(Unknown Source)
    at javax.swing.JFrame.<init>(Unknown Source)
    at Module1.ScientificCalculator.<init>(ScientificCalculator.java:17)
    at Module1.myFunctions.<init>(myFunctions.java:11)
    at Module1.ScientificCalculator.<init>(ScientificCalculator.java:15)
    at Module1.myFunctions.<init>(myFunctions.java:11)

And here's the original error (Although i use the factorial mehtod for an example, the same thing happens when trying to press any other button as well):

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Module1.ScientificCalculator.actionPerformed(ScientificCalculator.java:357)
    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$200(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)

can you post the MyFunctions.java file?
(for sort out your problem)

the StackOverFlow error may comes with recurcive calls of a function itself
to get more clarity on your problem i need to see the code (if you dont have any problem)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.