The form has an input field, 2 buttons and a table. When you enter a number and press the first button, the entered number is saved in the array. When you click on the second button, the table should be filled with data from the array. But when I click on the element of the array by pressing the second button, it contains zeros. Why is the array reset? Maybe due to carelessness / ignorance I missed something, but I do not understand why this is happening.

Code:

public class JFrame extends javax.swing.JFrame { /** * Creates new form JFrame */ public JFrame() { initComponents(); // Массив, в который записываются введенные данные int array[][] = new int[255][13]; int codeSymbol = 0; jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { int codeSymbol = Integer.parseInt(jTextField1.getText()); array[codeSymbol][0] = codeSymbol; System.out.println("Байт 1: " + array[codeSymbol][0]); } }); jButton2.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { System.out.println("Столбец 1: " + array [codeSymbol][0] ); } }); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jButton2 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("Байт 1"); jButton1.setText("Сохранить"); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object [][] { {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null} }, new String [] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" } ) { boolean[] canEdit = new boolean [] { false, false, false, false, false, false, false, false, false, false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit [columnIndex]; } }); jScrollPane1.setViewportView(jTable1); if (jTable1.getColumnModel().getColumnCount() > 0) { jTable1.getColumnModel().getColumn(0).setResizable(false); jTable1.getColumnModel().getColumn(1).setResizable(false); jTable1.getColumnModel().getColumn(2).setResizable(false); jTable1.getColumnModel().getColumn(3).setResizable(false); jTable1.getColumnModel().getColumn(4).setResizable(false); jTable1.getColumnModel().getColumn(5).setResizable(false); jTable1.getColumnModel().getColumn(6).setResizable(false); jTable1.getColumnModel().getColumn(7).setResizable(false); jTable1.getColumnModel().getColumn(8).setResizable(false); jTable1.getColumnModel().getColumn(9).setResizable(false); jTable1.getColumnModel().getColumn(10).setResizable(false); jTable1.getColumnModel().getColumn(11).setResizable(false); } jButton2.setText("Ввод"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(80, 80, 80) .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(161, 161, 161) .addComponent(jButton2))) .addGap(0, 67, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(28, 28, 28) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jButton1)) .addGap(34, 34, 34) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE) .addComponent(jButton2) .addContainerGap()) ); pack(); }// </editor-fold>//GEN-END:initComponents /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(JFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new JFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; private javax.swing.JTextField jTextField1; // End of variables declaration//GEN-END:variables } 

    1 answer 1

    You declare a local variable.

      int codeSymbol = Integer.parseInt(jTextField1.getText()); ^^^^^^^^^^^^^^^ 

    which will cease to exist after exiting a method, instead of using a class field, such as

      codeSymbol = Integer.parseInt(jTextField1.getText()); ^^^^^^^^^^ 
    • I would add that the variable codeSymbol and the array array limited to the scope of the constructor, and you need to put them in the class fields - MrFylypenko
    • @MrFylypenko This is so clear. This is a local variable, not a class field. :) - Vlad from Moscow
    • @VladfromMoscow, thanks! Elementary things and did not notice. - Ivan
    • @Ivan Not at all. :) - Vlad from Moscow