Results 1 to 10 of 10
  1. #1
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default Problems with java.lang.reflect

    I am working on an IRCbot at the moment. To make command parsing a little easier, I decided to implement the reflect API (In this case, calling methods by their name, which is passed as a string).
    However, I am getting an InvokationTargetException whenever I call the method.

    Code:
    *snip*
    Alright, spoiler tags failed. Here is the pastebin link:
    [Java] package ircbotgui; import java.io.FileNotFoundException; import java.io.IOEx - Pastebin.com


    Java Code:
    public void checkForSendableCommands(String[] args, boolean priv) throws IOException{
     
            isPrivate = priv;
            int index = 1;
     
            if (priv) {
                index += 1;
            }
     
            cmdArgs = args[1].split(" ", index);                        //0 = recipient, 1 = command 2+ = args. If not priv, recipient does not appear, all indices -1
            String cmd = cmdArgs[index - 1];
     
            boolean inList = false;
            for (int i = 0; i < sendable.length; i++) {
     
                if (sendable[i].equalsIgnoreCase(cmd)) {
     
                    inList = true;
                }
            }
     
            Method[] m = BorBot.class.getMethods();
     
            for (int i = 0; i < m.length; i++) {
     
                if (m[i].getName().equalsIgnoreCase(cmd) && inList) {
                    try {
                        gui.appendParseDebug("Invoking command: " + cmd);
     
                        m[i].invoke(this);
                       
                        System.out.println("Invoking succesful");
                    } catch (IllegalAccessException ex) {
                        System.out.println("Got exception Access");
                        gui.logError(ex);
                    } catch (InvocationTargetException ex) {
                        System.out.println("Got exception Invocation Target");
                        System.out.println(ex.getTargetException());
                        gui.logError(ex);
                    } catch (IllegalArgumentException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    Error: InvocationTargetException
    TargetException: NullPointerException
    Stacktrace:
    Java Code:
    [31/07 22:27:56]	null
    	[31/07 22:27:56]	sun.reflect.NativeMethodAccessorImpl.invoke0 (Line -2)
    	[31/07 22:27:56]	sun.reflect.NativeMethodAccessorImpl.invoke (Line 39)
    	[31/07 22:27:56]	sun.reflect.DelegatingMethodAccessorImpl.invoke (Line 25)
    	[31/07 22:27:56]	java.lang.reflect.Method.invoke (Line 597)
    	[31/07 22:27:56]	ircbotgui.BorBot.checkForSendableCommands (Line 235)
    	[31/07 22:27:56]	ircbotgui.BorBot.read (Line 102)
    	[31/07 22:27:56]	ircbotgui.BaseBot.run (Line 117)
    Any help would be greatly appreciated,
    Last edited by Boreeas; 08-07-2011 at 09:32 PM.

  2. #2
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Anyone, please?

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default

    What i noticed (but it has nothing to do with invoke) is, that you call rand.add(line); in your BorBot constructor, but you have not initialized the list (rand) ! So rand is null --> NullPointerException ! Please post the stacktrace from printStacktrace and not your own :D
    Last edited by eRaaaa; 08-07-2011 at 10:52 PM.

  4. #4
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Ah, thanks, fixed that.
    Java Code:
    java.lang.reflect.InvocationTargetException
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:601)
    	at ircbotgui.BorBot.checkForSendableCommands(BorBot.java:235)
    	at ircbotgui.BorBot.read(BorBot.java:103)
    	at ircbotgui.BaseBot.run(BaseBot.java:117)
    Caused by: java.lang.IllegalArgumentException: n must be positive

  5. #5
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default

    IllegalArgumentException: n must be positive

    that sounds like the random api! Do you call the nextInt method somewhere in your code with an integer < 1 ?

    Maybe here : :D
    String message = (String)rand.get(random.nextInt(rand.size()));

    rand.size() == 0 ? --> Exception !

  6. #6
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    I'll check that, but why would that already throw an exception during invoke? Wouldn't it first jump to the random() method, and then back?

  7. #7
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default

    Mhm? Thats the stacktrace! run calls read, read calls checkForSendableCommands, checkForSendableCommands calls invoke.
    invoke calls random ?
    Actually in your posted stacktrace after the line Caused by: java.lang.IllegalArgumentException: n must be positive there must be more lines, such as
    at java.util.Random.nextInt(Random.java:250)
    at ircbotgui.BorBot.random(BorBot.java:xxxx)
    ?? Thats the stacktrace for this Exception!

  8. #8
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    No, that was all. But apparently you were right - the bot was ereasing the entire file of random sentences on startup. Adding a check for rand-length solved it. Thanks!

  9. #9
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    11

    Default

    "the bot was ereasing the entire file of random sentences on startup"

    For me it looks more like a design issue :D
    Do you start the thread in the BaseBot constructor? Thats a common mistake! You start the thread and thus call the run/read method(because of inheritance), but your object isnt fully initialized/created (your while loop and the filling of your list)
    But this is only a presumption, sorry :P

  10. #10
    Boreeas's Avatar
    Boreeas is offline Member
    Join Date
    Apr 2011
    Posts
    20
    Rep Power
    0

    Default

    Nah, the ereasing of the file was an reader/writer issue (If you declare a new writer without an append flag, it clears the file, apparently)

Similar Threads

  1. Trouble using java.lang.reflect.Field class
    By wirdsmoth in forum New To Java
    Replies: 11
    Last Post: 10-09-2010, 03:58 PM
  2. Replies: 13
    Last Post: 07-09-2010, 05:50 AM
  3. Replies: 0
    Last Post: 01-06-2010, 01:19 PM
  4. java.lang.Error: Unresolved compilation problems
    By jon80 in forum New To Java
    Replies: 0
    Last Post: 06-07-2009, 10:04 PM
  5. Replies: 1
    Last Post: 05-19-2009, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •