1

As you can see in public void form4() i want to make a JLabel array with the size depends on variable x inside public void receiver(String rlc). String rlc's value comes from my another class. If in case that is not possible please give me advise or other ways to do it. Thanks

package name1;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class NAME4 extends JFrame implements ActionListener
{
public int x;
String letter = "";
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
JLabel label1;
JTextField part;

String count,count2;
int value;

double lite;
int arraycount;
int array;
JLabel[] parted;
String[] characters;
JButton okbutton;
int int1;
public String ameer;
JLabel label56;
public NAME4()
{
    super ("My Game4");
    setSize(500,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(true);
    setLocationRelativeTo(null);
    form4();

}
public void form4()
{
    label1 = new JLabel(letter);
    setLayout(new FlowLayout(1,250,25));
    part = new JTextField(null,3);
    okbutton = new JButton("OK");
    okbutton.addActionListener(this);
    add(label1);
    add(part);
    add(okbutton);
    parted = new JLabel[x];
}

public void receiver(String rlc)
{
    x = Integer.parseInt(rlc);        
    lite = 26/x;
    arraycount = (int) lite;
    array = arraycount +1 ;
    for (int a = 1; a < alphabet.length()+1; a++)
    {
        letter = letter + alphabet.charAt(a-1);
        if ((a) % x == 0)
        {
            letter = letter + "|";
        }
    }
    label1.setText(letter);
    characters = new String [array];
    int inc = x;
    int y;
    int h=0,z;
    for (z = 0 ; z < array ; z++)
    {
        characters[z] = "";
        for (y = h ; y < inc ; y++)
        {
            characters[z] += letter.charAt(y);
        }
            h += (x+1);
            inc = inc + (x+1);
        if (z == array - 2)
        {
            switch(x)
                {
                    case 1: inc = 52;
                        break;
                    case 2: inc = 38;
                        break;
                    case 3: inc = 34;
                        break;
                    case 4: inc = 32;
                        break;
                    case 5: inc = 31;
                        break;
                    case 6: inc = 30;
                        break;
                    case 7: inc = 29;
                        break;
                    case 8: inc = 29;
                        break;
                    case 9: inc = 28;
                        break;
                }
        }
    }
}//void receiver
@Override
public void actionPerformed(ActionEvent e) {

}

}

ameer
  • 31
  • 2
  • 8
  • Possible duplicate of [Variable length (Dynamic) Arrays in Java](http://stackoverflow.com/questions/2426671/variable-length-dynamic-arrays-in-java) – Christoph S Oct 11 '16 at 09:04
  • @jcamillo asked this (still not enough rep to directly comment): Can you extract the point of your question into a simple and small piece of code please? What exactly is the problem? – Serge Ballesta Oct 11 '16 at 09:59

1 Answers1

0

Yes, you can access x in form4() method, because x is a class field and it's accessible by any method inside your class.
However, you are initializing x in receiver() method, which is executed after form4(). When form4() executes, x is not initialized (defaults to 0) so your array will have 0 size.

To fix it, you can move the last line from form4() to receiver() like this:

public void form4()
{
    ...
    add(okbutton);
}

public void receiver(String rlc)
{
    x = Integer.parseInt(rlc);        
    parted = new JLabel[x]; // <--- now x is initialized
    lite = 26/x;
    ...
walen
  • 7,103
  • 2
  • 37
  • 58
  • Yeah your right but if i declare the size of my JLabel on `public void receiver` and do the `add(parted[4]);` 4 is just example. i get an error message NullPointerException which means `parted = new JLabel[x];` inside public void receiver is not readable. – ameer Oct 11 '16 at 11:07
  • What `add(parted[4])` ? You're not using `parted` anywhere in the code you posted, we can't help you if you hide important parts of your code. Please edit your post to include all relevant code. – walen Oct 11 '16 at 11:37