Null Pointer Exception on hotkey press

Feature Requests, Comments, Bugs, and Suggestions on the JIntellitype Java API.

Null Pointer Exception on hotkey press

Postby T3h_H4ck3r » Thu Aug 14, 2014 10:04 pm

Hi, I'm new to JIntellitype and can't seem to get it to work. Just trying a simple thing in order to give it a test, but I'm getting a large error trace and I can't seem to locate the issue. Here's the code for my simple test program:

Code: Select all
package com.h4ck3r;

import java.awt.AWTException;
import java.awt.Event;
import java.awt.Robot;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

public class BotMain implements Runnable, HotkeyListener {

   public static Thread botThread;
   public static BotMain botMain;
   public static Robot robot;
   public static boolean shouldRun;
   
   public BotMain() {
      botThread = new Thread(botMain);
      try {
         robot = new Robot();
      } catch (AWTException e) {
         e.printStackTrace();
      }
      shouldRun= false;
      JIntellitype.getInstance().registerHotKey(100, JIntellitype.MOD_CONTROL + JIntellitype.MOD_ALT, 89);
      JIntellitype.getInstance().addHotKeyListener(botMain);
   }
   
   public static void main(String[] args) {
      botMain = new BotMain();
      botThread.start();
   }

   public void run() {
      while (true) {
         
         if (shouldRun){
            robot.mouseMove(0, 0);
         }
         
      }
   }

   public void onHotKey(int arg0) {
      
      System.out.println("Hotkey detected");
      if (arg0==100){
         if (shouldRun){
            shouldRun = false;
         }else{
            shouldRun = true;
         }
      }
   }

}


The program runs until I enter my hotkey, at which point I receive this pile of errors:

Code: Select all
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at com.melloware.jintellitype.JIntellitype$1.run(JIntellitype.java:408)
   at java.awt.event.InvocationEvent.dispatch(Unknown Source)
   at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
   at java.awt.EventQueue.access$200(Unknown Source)
   at java.awt.EventQueue$3.run(Unknown Source)
   at java.awt.EventQueue$3.run(Unknown Source)
   at java.security.AccessController.doPrivileged(Native Method)
   at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
   at java.awt.EventQueue.dispatchEvent(Unknown Source)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
   at java.awt.EventDispatchThread.run(Unknown Source)


Note that the error occurs before the onHotKey() method even gets called, if that matters.
Any other information you might need in order to help me trouble shoot can be provided, and hopefully someone can help me out. Thanks everyone!
T3h_H4ck3r
 
Posts: 2
Joined: Thu Aug 14, 2014 9:57 pm

Re: Null Pointer Exception on hotkey press

Postby Melloware » Sat Aug 23, 2014 9:55 am

Can you try this line instead to register your hotkey? Using this instead of your local botMain variable which could be NULL?

Code: Select all
JIntellitype.getInstance().addHotKeyListener(this);
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Melloware Inc.
http://www.melloware.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
User avatar
Melloware
Site Admin
 
Posts: 11044
Joined: Thu Dec 01, 2005 10:24 pm
Location: Philadelphia, Pennsylvania USA

Re: Null Pointer Exception on hotkey press

Postby T3h_H4ck3r » Sat Aug 23, 2014 11:00 am

Thanks for getting back to me.

I might take a look at adding the key listener that way in the future. But for the time being, I actually managed to get it working in another way.

I noticed that the code was sort of convoluted lol, certainly much more so than necessary. I rearranged some stuff and declared things in a more simplistic way, managed to get rid of the exception.

Thanks for your help though; now that you mention that, I'm sure it probably would have worked that way too.

Edit: On second thought, it might get upset about using "this" in a static context, in your case...
T3h_H4ck3r
 
Posts: 2
Joined: Thu Aug 14, 2014 9:57 pm

Re: Null Pointer Exception on hotkey press

Postby Melloware » Sun Aug 24, 2014 7:12 am

Ahh yes you cannot use this in a static Main method. You can only use this in the instance of the class.

Glad you got it working though! Hope it suits your needs!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Melloware Inc.
http://www.melloware.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-
User avatar
Melloware
Site Admin
 
Posts: 11044
Joined: Thu Dec 01, 2005 10:24 pm
Location: Philadelphia, Pennsylvania USA


Return to JIntellitype

Who is online

Users browsing this forum: No registered users and 1 guest

cron