Benoit Goderre Benoit Goderre - 1 year ago 84
Java Question

Array List initialization displayed as boolean

I am trying to create an object with the following constructor

public PageBreak(String sheetName, ArrayList<Integer> rowPageBreaks, ArrayList<Integer> columnPageBreaks)
this.sheetName = sheetName;
this.rowPageBreaks = rowPageBreaks;
this.columnPageBreaks = columnPageBreaks;

In another method, I do the following call:

pageBreaks.add(new PageBreak(teamResultSheet.getName(),
new ArrayList<Integer>().add(teamResultSheet.getRowPageBreak()), null));

I then get the following error message: The constructor PageBreak (String, boolean, null) is undefined.

How can I create an
on the spot?

Answer Source

You're going to want to populate your list before you attempt to use it. Here's what's happening:

  • You instantiate a new ArrayList<Integer>.
  • You immediately call add on that list.
  • The result of add is boolean (and consequently always returns true).
  • The boolean result is what is interpreted, and not the list.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download