Question

I have a small piece of Java code which first checks whether a parameter you've entered into IRC command exists or not before it selects which code segment to run. I am listening to commands through PircBotX and the command that is described below is a command which basically either lists all the people on a server, or all the people in a channel via the command :? list (all).

if ( argments[1].equalsIgnoreCase( "list" ) ) {
  CyniChat.printDebug( "Listing chosen..." );
  if ( argments[2] != null && argments[2].equalsIgnoreCase( "all" ) ) {
    CyniChat.printDebug( "You've either got 'all' as parameter..." );
    CyniChat.printDebug( event.getUser().getNick()+" : "+thisChan.getName() );
    ircResponses.listOutput( event.getUser(), event.getBot(), thisChan.getName(), true );
    return;
  } else {
    CyniChat.printDebug( "Or you don't...." );
    CyniChat.printDebug( event.getUser().getNick()+" : "+thisChan.getName() );
    ircResponses.listOutput( event.getUser(), event.getBot(), thisChan.getName(), false );
    return; 
  }
}

Now, the odd thing about this statement is that while the first debugging statement is executed so the console outputs "Listing chosen...", that is the only thing that it outputs. There is neither of the other debug statements executed whenever I run :? list in IRC. Yet when I run :? list all, everything seems to run fine, the statements being executed as per normal and stuff.

In all probability, I've probably just made a very small logical error somewhere that I am finding impossible to spot. If anyone has any ideas on how to resolve this situation, the help would be much appreciated.

Thanks.

Was it helpful?

Solution

If you type in :? list, that's 2 arguments. But if you check for the 3rd by doing argments[2], this will throw an index out of bounds exception. The reason you're not seeing the error message is you probably have a catch block that ignores it without printing an error. You'll need to find this catch and at the very least put this inside it:

catch (Exception e) {
    e.printStackTrace();
}

In the future, never leave a catch block empty. Always log something or else you won't be able to notice when errors occur.

OTHER TIPS

Replace

if ( argments[2] != null && argments[2].equalsIgnoreCase( "all" ) ) {
    ...
}

with

if (arguments.length > 2 && argments[2] != null && argments[2].equalsIgnoreCase( "all" ) ) {
    ...
}

This ensures that there is a third element in the array before you try to access it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top