0

I'm trying to do a calculator program and I have one problem with the object where i store the numbers given.

I'm trying to do a long array of 1 dimenson object, but i don't know why, when I compile, there is one problem in the line of going accumulate these valeur.

If i change de Object doing it not be one array, it works perfectly.

I'm sorry if there is something responded like that, but i searched and I didn't find it. It's too hard too to try to explain this with a title.

The error is:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at calculadora.calc.actionPerformed(calc.java:95) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

And my code is:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import javax.swing.*;


public class calc extends JFrame implements ActionListener {
    JButton nou = new JButton("9");
    JButton vuit = new JButton("8");
    JButton set = new JButton("7");
    JButton sis = new JButton("6");
    JButton cinc = new JButton("5");
    JButton quatre = new JButton("4");
    JButton tres = new JButton("3");
    JButton dos = new JButton("2");
    JButton un = new JButton("1");
    JButton zero = new JButton("0");
    JButton mes = new JButton("+");
    JButton menys = new JButton("-");
    JButton mem = new JButton("Ans");
    JButton borrar = new JButton("Borrar");
    JButton igual = new JButton("=");
    JTextField texte= new JTextField("0",10);
   long v[];
   long v2;
   int i=0;
   long u=0;
   String muestra;

   int memo;
    public calc (){
       super("Calculadora Carlos. Congrats!");
       setSize(400,400);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       JPanel num = new JPanel();
       JPanel op = new JPanel();
       JPanel tex = new JPanel();
       un.addActionListener(this);
       dos.addActionListener(this);
       tres.addActionListener(this);
       quatre.addActionListener(this);
       cinc.addActionListener(this);
       sis.addActionListener(this);
       set.addActionListener(this);
       vuit.addActionListener(this);
       nou.addActionListener(this);
       mes.addActionListener(this);
       menys.addActionListener(this);
       mem.addActionListener(this);
       borrar.addActionListener(this);
       igual.addActionListener(this);
       texte.setEditable(false);
       GridLayout numeros = new GridLayout(4,3,5,5);
       num.setLayout(numeros);
       num.add(nou);
       num.add(vuit);
       num.add(set);
       num.add(sis);
       num.add(cinc);
       num.add(quatre);
       num.add(tres);
       num.add(dos);
       num.add(un);
       num.add(zero);

     GridLayout operacions = new GridLayout(5,1,5,5);
        op.setLayout(operacions);
        op.add(mes);
        op.add(menys);
        op.add(mem);
        op.add(borrar);
        op.add(igual);

        GridLayout text = new GridLayout(3,4,5,5);
        tex.setLayout(text);
        tex.add(texte);



getContentPane().add(num, BorderLayout.CENTER); 
 getContentPane().add(tex, BorderLayout.NORTH);     
 getContentPane().add(op, BorderLayout.EAST);  

        setVisible(true);
    }
  public void actionPerformed(ActionEvent evt) {

      Object A=evt.getSource();
      while(i==0){
      if(A==un){
          u=v[i];
          v[i]=u*10+1;
          muestra=muestra+"1";
          texte.setText(muestra);
      }}
 }



    public static void main(String[] args) {
   calc carlos=new calc();
    }
}

Sorry If my english is not the best of the world. I'm not an english speaker.

2 Answers 2

2

v[] is never initialized, so it will NullPointerException when you try to access it here

u=v[i];
Sign up to request clarification or add additional context in comments.

6 Comments

But how I initalize if i don't want to decide the lenght of the array? is not possible?
No, arrays length needs to be set on initialization. If you want a variable length list you need to use one of the Collections like ArrayList
If you do not want to decide on the length of an array, you should go with an ArrayList<Long> instead of long[] for example.
@user3069682 what are you going to store in v[] ? ArrayList is the best option for this. To play with this you dont need to specify the length.
I may be wrong, but does while(i == 0) have a funny smell to it?
|
1

In your code you have only declared array long v[] but not initialized it.So this line u=v[i]; is might be throwing exception. You need to initialzed it as long v[]=new long[size];

Also if you don't want to define array size during initialization then you can use ArrayList also.

3 Comments

Thank you for all the responses! this webpage is amazing! I did the next changes long v[]= new long[3]; //line 28 v[i]=v[i]*10+1; //line 96 When i compile it works, but when i press one button, it freezes? any idea?
check @peeskillet comment on the end of my post, that may be why
@user3069682 I guess while(i==0) this might be causing infinite loop.you are not incrementing value of i.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.