Foros del Web » Programación para mayores de 30 ;) » Java »

que significa Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsE

Estas en el tema de que significa Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsE en el foro de Java en Foros del Web. hola tengo un problema un una calculadora que estoy haciendo tengo que hacer una calculadora que sume, reste y multiplique como haciendolo en papel a ...
  #1 (permalink)  
Antiguo 09/06/2012, 17:37
 
Fecha de Ingreso: marzo-2012
Mensajes: 75
Antigüedad: 12 años, 1 mes
Puntos: 3
Pregunta que significa Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsE

hola tengo un problema un una calculadora que estoy haciendo
tengo que hacer una calculadora que sume, reste y multiplique como haciendolo en papel a punta de arreglos, y apenas la empecé, y lo que estoy haciendo es pasar los números que me ingresan a a los respectivos arreglos para empezar a operar

miren este es el código por si alguien lo quiere probar y ayudarme a saber el error donde esta

Código Javascript:
Ver original
  1. public class Ventana extends javax.swing.JFrame implements ActionListener
  2. {
  3.     /*
  4.      * interface objects
  5.      * objetos de la interfaz  
  6.      */
  7.     private JTextField jTextField1;
  8.     private JTextField jTextField2;
  9.     private JLabel jLabelNumero2;
  10.     private JButton jButtonCalcular;
  11.     private JLabel jLabelResta;
  12.     private JTextField jTextFieldMultiplicacion;
  13.     private JLabel jLabelMultiplicacion;
  14.     private JTextField jTextFieldResta;
  15.     private JLabel jLabelSuma;
  16.     private JTextField jTextFieldSuma;
  17.     private JLabel jLabel1;
  18.  
  19.     /**
  20.     * Auto-generated main method to display this JFrame
  21.     * Generado automáticamente el método principal para mostrar el JFrame
  22.     */
  23.     public static void main(String[] args) {
  24.         SwingUtilities.invokeLater(new Runnable() {
  25.             public void run() {
  26.                 Ventana inst = new Ventana();
  27.                 inst.setLocationRelativeTo(null);
  28.                 inst.setVisible(true);
  29.             }
  30.         });
  31.     }
  32.    
  33.     public Ventana() {
  34.         super();
  35.         initGUI();
  36.     }
  37.     /*
  38.      * method with the objects´s events (ActionListener)
  39.      * Método con los eventos de los objetos (ActionListener)
  40.      */
  41.     private void initGUI() {
  42.         try {
  43.             setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  44.             getContentPane().setLayout(null);
  45.             {
  46.                 jTextField1 = new JTextField();
  47.                 getContentPane().add(jTextField1);
  48.                 jTextField1.setBounds(110, 28, 155, 21);
  49.             }
  50.             {
  51.                 jLabel1 = new JLabel();
  52.                 getContentPane().add(jLabel1);
  53.                 jLabel1.setText("Numero 1:");
  54.                 jLabel1.setBounds(30, 31, 80, 14);
  55.             }
  56.             {
  57.                 jButtonCalcular = new JButton();
  58.                 getContentPane().add(jButtonCalcular);
  59.                 jButtonCalcular.setText("Calcular");
  60.                 jButtonCalcular.setBounds(289, 45, 92, 21);
  61.                 jButtonCalcular.addActionListener(this);
  62.             }
  63.             {
  64.                 jLabelNumero2 = new JLabel();
  65.                 getContentPane().add(jLabelNumero2);
  66.                 jLabelNumero2.setText("Numero 2:");
  67.                 jLabelNumero2.setBounds(30, 57, 62, 14);
  68.             }
  69.             {
  70.                 jTextField2 = new JTextField();
  71.                 getContentPane().add(jTextField2);
  72.                 jTextField2.setBounds(110, 54, 155, 21);
  73.             }
  74.             {
  75.                 jTextFieldSuma = new JTextField();
  76.                 getContentPane().add(jTextFieldSuma);
  77.                 jTextFieldSuma.setBounds(30, 117, 80, 21);
  78.             }
  79.             {
  80.                 jLabelSuma = new JLabel();
  81.                 getContentPane().add(jLabelSuma);
  82.                 jLabelSuma.setText("Suma");
  83.                 jLabelSuma.setBounds(49, 97, 41, 14);
  84.             }
  85.             {
  86.                 jTextFieldResta = new JTextField();
  87.                 getContentPane().add(jTextFieldResta);
  88.                 jTextFieldResta.setBounds(248, 117, 89, 21);
  89.             }
  90.             {
  91.                 jLabelMultiplicacion = new JLabel();
  92.                 getContentPane().add(jLabelMultiplicacion);
  93.                 jLabelMultiplicacion.setText("Multiplicacion");
  94.                 jLabelMultiplicacion.setBounds(145, 97, 72, 14);
  95.             }
  96.             {
  97.                 jTextFieldMultiplicacion = new JTextField();
  98.                 getContentPane().add(jTextFieldMultiplicacion);
  99.                 jTextFieldMultiplicacion.setBounds(136, 117, 91, 21);
  100.             }
  101.             {
  102.                 jLabelResta = new JLabel();
  103.                 getContentPane().add(jLabelResta);
  104.                 jLabelResta.setText("Resta");
  105.                 jLabelResta.setBounds(273, 97, 40, 14);
  106.             }
  107.             pack();
  108.             this.setSize(417, 300);
  109.         } catch (Exception e) {
  110.            
  111.             e.printStackTrace();
  112.         }
  113.     }
  114.     /*
  115.      * The method below has the interaction and the logic
  116.      * due to that Triviño did not taught us to use methods with out
  117.      * the static rule
  118.      *
  119.      * El método siguiente tiene la interacción y la lógica,
  120.      * debido a que Triviño no nos enseñó a usar métodos sin la regla estática
  121.      */
  122.     @Override
  123.     public void actionPerformed(ActionEvent e)
  124.     {
  125.         if(e.getSource() == jButtonCalcular)
  126.         {
  127.             int numero1 = Integer.parseInt(jTextField1.getText());
  128.             int numero2 = Integer.parseInt(jTextField2.getText());
  129.             int tamaño = calcularDigitos(numero1, numero2);
  130.            
  131.             int num1[] = separarDigitos(numero1, tamaño);
  132.             int num2[] = separarDigitos(numero2, tamaño);
  133.            
  134.            
  135.            
  136.            
  137.             jTextFieldSuma.setText(""+numero1);
  138.             jTextFieldResta.setText(""+numero2);
  139.             jTextFieldMultiplicacion.setText("");
  140.         }
  141.     }
  142.    
  143.      public int calcularDigitos(int numero1, int numero2){
  144.          int numDigitos=1;
  145.          if(numero1>numero2){
  146.                 int Det=numero1;
  147.                 for(; Det>0; numDigitos++){
  148.                 Det/=10;
  149.                 }  
  150.             }else{
  151.                 int Det=numero2;
  152.                 for(; Det>0; numDigitos++){
  153.                 Det/=10;
  154.                
  155.             }
  156.                
  157.         }
  158.          return numDigitos;
  159.      }
  160.       public int[] separarDigitos(int numero, int tamaño){
  161.             int Det=numero;
  162.             int numeroArg[];
  163.             numeroArg= new int[tamaño];
  164.                 for(int i=numeroArg.length-1; Det>0; Det=numero/10){
  165.                 numeroArg[i]=Det%10;
  166.                 i--;
  167.                 }
  168.                 System.out.print(numeroArg);
  169.                 return numeroArg;
  170.             }
  171. }

miren este método me dice el tamaño del número mas grande para así al numero mas pequeño agregarle ceros a la izquierda en el arreglo
Código Javascript:
Ver original
  1. public int calcularDigitos(int numero1, int numero2){
  2.          int numDigitos=1;
  3.          if(numero1>numero2){
  4.                 int Det=numero1;
  5.                 for(; Det>0; numDigitos++){
  6.                 Det/=10;
  7.                 }  
  8.             }else{
  9.                 int Det=numero2;
  10.                 for(; Det>0; numDigitos++){
  11.                 Det/=10;
  12.                
  13.             }
  14.                
  15.         }
  16.          return numDigitos;
  17.      }

luego ese valor lo guardo en una variable "tamaño" para luego colocarla de parámetro para el método que me mete el número dentro del arreglo

Código Javascript:
Ver original
  1. public int[] separarDigitos(int numero, int tamaño){
  2.             int Det=numero;
  3.             int numeroArg[];
  4.             numeroArg= new int[tamaño];
  5.                 for(int i=numeroArg.length-1; Det>0; Det=numero/10){
  6.                 numeroArg[i]=Det%10;
  7.                 i--;
  8.                 }
  9.                 System.out.print(numeroArg);
  10.                 return numeroArg;
  11.             }

luego en el main los llamo así
Código Javascript:
Ver original
  1. int numero1 = Integer.parseInt(jTextField1.getText());
  2.             int numero2 = Integer.parseInt(jTextField2.getText());
  3.             int tamaño = calcularDigitos(numero1, numero2);
  4.            
  5.             int num1[] = separarDigitos(numero1, tamaño);
  6.             int num2[] = separarDigitos(numero2, tamaño);

y los muestro en estos text fiels así
Código Javascript:
Ver original
  1. jTextFieldSuma.setText(""+numero1);
  2.             jTextFieldResta.setText(""+numero2);

para saber si esta bueno o malo, pero no me aparece nada y me aparece este error


Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at Ventana.separarDigitos(Ventana.java:182)
at Ventana.actionPerformed(Ventana.java:148)
at javax.swing.AbstractButton.fireActionPerformed(Unk nown 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.mouseRe leased(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(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn 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$000(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.doIntersectionPri vilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(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.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
  #2 (permalink)  
Antiguo 09/06/2012, 17:51
 
Fecha de Ingreso: marzo-2012
Mensajes: 75
Antigüedad: 12 años, 1 mes
Puntos: 3
Respuesta: que significa Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexO

a nooooo mucho tio que me cago en cabra de leche joder jaja ya se el error es que en el método separarDigitos yo tenia la condición del for mala, por eso me sacaba ese error yo tenia

Código Javascript:
Ver original
  1. for(int i=numeroArg.length-1; Det>0; Det=numero/10){
  2.                 numeroArg[i]=Det%10;
  3.                 i--;
  4.                 }

solo tenía que cambiar la condicion del for por
Código Javascript:
Ver original
  1. i>=0

y listo me funciona ya, ahora puedo seguir trabajando en lo demás pero si quisiera saber que significa ese error tan largo gracias...
  #3 (permalink)  
Antiguo 09/06/2012, 18:08
 
Fecha de Ingreso: diciembre-2011
Mensajes: 152
Antigüedad: 12 años, 4 meses
Puntos: 34
Respuesta: que significa Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexO

La causa es que tratas de acceder a un indice que no esta dentro del rango del arreglo, por ejemplo si tu arreglo es de tamaño 5 y tratas de acceder a la posicion 10.

Etiquetas: arreglos, calculadora
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 05:22.