数组错误(java)

时间:2013-12-05 10:23:57

标签: java arrays swing nullpointerexception

我正在尝试执行一个计算器程序,我对存储给定数字的对象有一个问题。

我正在尝试做一个长数组的1维对象,但我不知道为什么,当我编译时,在累积这些valeur的行中存在一个问题。

如果我改变de Object,它不是一个数组,它可以很好地工作。

我很抱歉,如果有这样的回应,但我搜索了,但我没有找到它。试图用标题来解释这一点太难了。

错误是:

  

线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException     在calculadora.calc.actionPerformed(calc.java:95)at   javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)     在   javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2341)     在   javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)     在   javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)     在   javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)     在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)     在   java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)     在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 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)     在   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)     在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)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:705)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我的代码是:

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

抱歉,如果我的英语不是世界上最好的。我不是讲英语的人。

2 个答案:

答案 0 :(得分:2)

v[]从未初始化,因此当您尝试在此处访问时NullPointerException

u=v[i];

答案 1 :(得分:1)

在您的代码中,您只声明了数组long v[]但未初始化它。所以此行u=v[i];可能会抛出异常。 您需要将其初始化为long v[]=new long[size];

此外,如果您不想在初始化期间定义数组大小,那么您也可以使用ArrayList