0

I have an output class receiving an array from an input class. The array is then changed into labels in the output class. I have an error in the main method of my output class. It might have something to deal with the connection between the input class. What should I have put in my main method of the output class to fix the error?

Code of input:

int[]output = new int[4];
    output[0] = addObj.getSumA();
    output[1] = addObj.getSumB();
    output[2] = addObj.getSumC();
    output[3] = addObj.getSumD();

    Output outputObj = new Output(output);

Code of Output Class:

public class Output extends JFrame implements ActionListener
{
    private JLabel numberA;
    private JLabel numberB;
    private JLabel numberC;
    private JLabel numberD;
    private Box numberBox;
    private Box numberBox2;

public Output(int output[])
{
    super("Output Frame");
    this.setBounds(430,300,600,450);
    this.getContentPane().setBackground(Color.PINK);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setLayout(new BorderLayout());

    this.numberA = new JLabel(Integer.toString(output[0]));
    this.numberB = new JLabel(Integer.toString(output[1]));
    this.numberC = new JLabel(Integer.toString(output[2]));
    this.numberD = new JLabel(Integer.toString(output[3]));

    numberBox = Box.createVerticalBox();
    numberBox.add(numberA);
    numberBox.add(numberC);

    numberBox2 = Box.createVerticalBox();
    numberBox2.add(numberB);
    numberBox2.add(numberD);

    this.setVisible(true);
}

public static void main (String[] args)
{
    Output outputObj = new Output(int[]);
}

Keep in mind this is gui. The error is in the line above. int[] isn't the correct thing to enter, but I don't know what is.

  • 1
    Post the error please – rhowell Mar 16 '20 at 18:28
  • 1
    Does this answer your question? [How do I declare and initialize an array in Java?](https://stackoverflow.com/questions/1200621/how-do-i-declare-and-initialize-an-array-in-java) – azurefrog Mar 16 '20 at 18:30

1 Answers1

0

You need to actually declare and initialize an array to pass as an argument.

So create an int array first like this.

this creates an array of size 10 (doesn't have values assigned though)

int[] intArr = new int[10]

you can also create an array and populate the values in one line like this

int[] intArr = new int[]{ 1,2,3,4,5,6,7,8,9,10 }; 
// now you can call your method and pass the array you created
Output outputObj = new Output(intArr);
rhowell
  • 830
  • 5
  • 19
  • So even though that has nothing to do with my class, I would do that and keep it in my main method? – Leila MOZAFFAR Mar 16 '20 at 18:43
  • @LeilaMOZAFFAR Think about it this way. Look at the code inside of your Output class. You see where there is code like: `output[0]`? This is accessing the array you pass to it. So if you don't create an array and pass it, there is nothing for it to access and you will get an error. – rhowell Mar 16 '20 at 18:45