0

The eclipse compiler shows Null pointer exception and large number of statements.Actually i am coding for a basic calulator in desktop application and i dont know what went wrong ` import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

  import javax.swing.*;
  class Calcc extends JFrame implements ActionListener
  {
  Calcc n;
  String l="b";
  int z=0;
  JFrame f;
  JTextField t1=new JTextField(30);   
  JTextField t2=new JTextField(10);  
  JButton b1=new JButton("1");
  JButton b2=new JButton("2");
  JButton b3=new JButton("3");
  JButton b4=new JButton("4");
  JButton b5=new JButton("5");
  JButton b6=new JButton("6");
  JButton b7=new JButton("7");
  JButton b8=new JButton("8");
  JButton b9=new JButton("9");
  JButton b10=new JButton("0");
  JButton b11=new JButton("+");
  JButton b12=new JButton("-");
  JButton b13=new JButton("*");
  JButton b14=new JButton("/");
  JButton b15=new JButton("=");
  Calcc()
  {
    f=new JFrame();
    f.add(t1);
    f.add(t2);
    f.add(b1);
    f.add(b2);
    f.add(b3);
    f.add(b4);
    f.add(b5);
    f.add(b6);
    f.add(b7);
    f.add(b8);
    f.add(b9);
    f.add(b10);
    f.add(b11);
    f.add(b12);
    f.add(b13);
    f.add(b14);
    f.add(b15);
    b1.addActionListener(this);
    b2.addActionListener(this);
    b3.addActionListener(this);
    b4.addActionListener(this);
    b5.addActionListener(this);
    b6.addActionListener(this);
    b7.addActionListener(this);
    b8.addActionListener(this);
    b9.addActionListener(this);
    b10.addActionListener(this);
    b11.addActionListener(this);
    b12.addActionListener(this);
    b13.addActionListener(this);
    b14.addActionListener(this);
    b15.addActionListener(this);
    f.setVisible(true);
    f.setSize(350,350);
    f.setLayout(new FlowLayout());
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public void actionPerformed( ActionEvent e)
  {

    String d;
    d=t1.getText();
    if(e.getSource()==b1)
    {
        t1.setText(d+"1");
    }
    if(e.getSource()==b2)
    {
        t1.setText(d+"2");
    }
    if(e.getSource()==b3)
    {
        t1.setText(d+"3");
    }
    if(e.getSource()==b4)
    {
        t1.setText(d+"4");
    }
    if(e.getSource()==b5)
    {
        t1.setText(d+"5");
    }
    if(e.getSource()==b6)
    {
        t1.setText(d+"6");
    }
    if(e.getSource()==b7)
    {
        t1.setText(d+"7");
    }
    if(e.getSource()==b8)
    {
        t1.setText(d+"8");
    }
    if(e.getSource()==b9)
    {
        t1.setText(d+"9");
    }
    if(e.getSource()==b10)
    {
        t1.setText(d+"0");
    }
    if(e.getSource()==b11)
    {
        t1.setText(d+"+");
    }
    if(e.getSource()==b12)
    {
        t1.setText(d+"-");
    }
    if(e.getSource()==b13)
    {
        t1.setText(d+"*");
    }
    if(e.getSource()==b14)
    {
        t1.setText(d+"/");
    }
    if(e.getSource()==b15)
    {
        while(l!="a")
        {
            int b=n.CalcA(d);
            if(l=="+")
                z=z+b;
            else if(l=="-")
                z=z-b;
            else if(l=="*")
                z=z*b;
            else if(l=="*")
                z=z*b;
        }
        t2.setText(String.valueOf(z));
    }

  }

  int CalcA(String d1)
  {

    l="a";
    int k = 0;
    while((d1!="+")&&(d1!="-")&&(d1!="*")&&(d1!="/"))
    {
    if(d1!="\n")
    break;
    k=k*10+Integer.valueOf(d1);
    }
    l=d1;
    return k;
    }
 }
  public class CalcD 
{

  public static void main(String[] args)
 {
   new Calcc();
 }

}

`

  • post your exception so that we could understand better – Blip Jun 27 '17 at 06:01
  • `n` is `null` and has no point. Replace `int b=n.CalcA(d);` with `int b=CalcA(d);` . Also, method names should start with a lower-case letter. – Arnaud Jun 27 '17 at 06:05
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Calcc.actionPerformed(CalcD.java:135) 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) – EzhilKumaran Jun 27 '17 at 06:05
  • `n` is `null`....in fact, there is no need to `n` at all – MadProgrammer Jun 27 '17 at 06:06
  • 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) – EzhilKumaran Jun 27 '17 at 06:06
  • 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$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) – EzhilKumaran Jun 27 '17 at 06:07
  • at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) – EzhilKumaran Jun 27 '17 at 06:07
  • 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) – EzhilKumaran Jun 27 '17 at 06:07
  • these are the staements that came as the error messages.....srry i am new to the community so dont know how to post larger comments.... – EzhilKumaran Jun 27 '17 at 06:08
  • You should edit your question and post the error stack trace in the question. Also where is **CalcD.java: Line 135** – Blip Jun 27 '17 at 06:09
  • I am using loop condition to split the numbers in the calculator but now its an infinite loop and can someone help me out with the statement of comparing single character of a string to another character in the loop.. – EzhilKumaran Jun 27 '17 at 06:14
  • If your current problem is solved, ask a new question. – Blip Jun 27 '17 at 06:18
  • k sure..thankyou Mr.Blip.. – EzhilKumaran Jun 27 '17 at 06:19

0 Answers0