0

I have a varargs method and I want to call the method using 1 or 0 arguments. The following code compiles, but does not run correctly:

final List<MyMessage> allMessages = new ArrayList<MyMessage>();
MyMessage message = null;
if (checkSomeCondition()) {
  message = new MyMessage(someParam);
  allMessages.add(message);
}

int size = allMessages.size();
MyMessage[] msgArrayType = new MyMessage[size]; // ERROR
MyMessage[] msgArray = allMessages.toArray(msgArrayType);

callFunc(msgArray);
...........
public void callFunc(MyMessage... messages) {

}

When I debug the code, after the line that I marked with //ERROR, the value of the array msgArrayType is com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array.

This is also wrong:

MyMessage[] msgArray = (MyMessage[]) allMessages.toArray();
// -> java.lang.Object cannot be cast to .......MyMessage

I really don't understand it, because allMessages is a List of MyMessages!

The only option I see is to use something like

if (message == null) {
  callFunc();
} else {
  callFunc(message);
}

but I was wondering how the code would look like if I want to write callFunc only once. Any suggestions?

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
user1414745
  • 1,317
  • 6
  • 25
  • 45
  • I think you are misunderstanding varargs. In the case you put, you would not pass the array, but pass 2 parameters: `callfunc(message1, message2)` if there are 2 messages where message1 and message2 are both of type MyMessage. With the varargs you can call `callfunc()` or `callfunc(message1)` or `callfunc(message1, message2)`, etc. I have no idea what you are trying to accomplish with `msgArrayType` variable. – David Fleeman Dec 13 '13 at 16:35
  • I see your updated code where you have if/else construct. That is exactly how you should call the function if you are using varargs... If you want it to be cleaner than that, then perhaps you don't want varargs at all but have method signature like: `public void callFunc(List messages);` instead and just call `callFunc(allMessages)`. – David Fleeman Dec 13 '13 at 16:38
  • 2
    The first Error you get is actually just a [debugging glitch](http://stackoverflow.com/questions/1367730/how-do-i-deal-with-a-classnotloadedexception-while-debugging). The second problem is just a misuse of the API - `toArray` actually returns an Object[], that`s what the other method is for. You can pass an empty array to the varargs method, so you do not need the switch. – Sebastian Dec 13 '13 at 16:44

1 Answers1

-2

If you want to use varargs, which excepts an array - you should also make sure that the elements passed are non null.

So if you just want to use one function call of callfunc() and don't want to surround with if-else block you can use the following function call.

callfunc(message == null ? (Object)null : message);

This will either pass in the message object wrapped in array, or it will pass an array with single null element.

SpartanElite
  • 624
  • 4
  • 13