I have a form on which 300 textfield and a button

 `public static int i,j,k,l,m,n,z,f; public static JLabel[] lbl = new JLabel[21]; public static JLabel[] lbl1 = new JLabel[16]; public static JTextField[][] tf1 = new JTextField[20][15]; public static int [][] qw = new int[20][15]; public static Random rand = new Random(); public static void main(String[] args) { JFrame fr1 = new JFrame("Массив"); JPanel pn1 = new JPanel(); fr1.setSize(600,400); fr1.setExtendedState(MAXIMIZED_BOTH); fr1.setDefaultCloseOperation(EXIT_ON_CLOSE); pn1.setLayout(null); fr1.add(pn1); //fr1.setContentPane(pn1); // for(i=0;i<20;i++){ for(j=0;j<15;j++){ tf1[i][j]=new JTextField(); tf1[i][j].setBounds(30+i*35,30+j*35,30,25); pn1.add(tf1[i][j]); } } for (k=1;k<21;k++){ lbl[k]=new JLabel(""+k); lbl[k].setBounds(10+k*35,5,30,20); pn1.add(lbl[k]); } for(l=1;l<16;l++){ lbl1[l] = new JLabel(""+l); lbl1[l].setBounds(5,5+l*35,30,20); pn1.add(lbl1[l]); } JButton btn1 = new JButton("Заполнить"); pn1.add(btn1); btn1.setBounds(856,250,100,50); JTextField tf3 = new JTextField(); pn1.add(tf3);tf3.setBounds(850,190,40,40); JTextField tf4 = new JTextField(); pn1.add(tf4); tf4.setBounds(910,190,40,40); btn1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (m = 0; m < 20; m++) { for (n = 0; n < 15; n++) { qw[m][n] = rand.nextInt(10) + 1; } } tf1[i][j].setText("" + qw[m][n]); } });` 

It turns out, when I click on the button to fill the array and textfield to the textfield , knocks an error, please ActionListner me what was wrong in the ActionListner I wrote? Thank you in advance!

Closed due to the fact that off-topic by user194374, aleksandr barakin , Denis , Denis Bubnov , rjhdby 19 December '16 at 8:09 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - aleksandr barakin, Denis, Denis Bubnov, rjhdby
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    About the error we have to guess? - user194374 4:05 pm
  • Struck with you, help with us - Flippy
  • one
    @kff - yes, you have to guess. Are you a professional, or are you not a professional? - Igor
  • one
    @Igor Where am I ... Normal bydlokoder-dropout ... ;-( - user194374

1 answer 1

The main error is that you are trying in this line of code

 tf1[i][j].setText("" + qw[m][n]); 

assign something to an element with incomprehensible indexes, and taking it also from an array with incomprehensible indexes. Need actionPerformed to change as follows:

 for (m = 0; m < 20; m++) { for (n = 0; n < 15; n++) { qw[m][n] = rand.nextInt(10) + 1; tf1[m][n].setText("" + qw[m][n]); } } 

You got into such a situation for two reasons. The first is that you did not even try to understand what you have written. The second is global variables. If you did not declare a whole bunch of variables for all needs, but created them as needed, this error would not have occurred and you would receive an error already from the compiler, and not at the execution stage. You need to do this:

 for (int m = 0; m < 20; m++) { for (int n = 0; n < 15; n++) { qw[m][n] = rand.nextInt(10) + 1; tf1[m][n].setText("" + qw[m][n]); } } 

Cycle counters will exist only inside the loop and will become unavailable upon exiting the loop.

PS However, judging by the style of the code and the question, my maxims will be ignored, because the vehicle just needs to pass the lab and forget "these stupid puzzles on the Toad!".