We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › nullPointer thrown for every call to Robot object
Page Index Toggle Pages: 1
nullPointer thrown for every call to Robot object (Read 772 times)
nullPointer thrown for every call to Robot object
Aug 19th, 2006, 3:22pm
 
Hi - i'm kind of new to processing and Java though done some object oriented stuff in Actionscript 2.0.  I'm trying to take control of the mouse cursor in windows xp, using the Robot class in java (see:
http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Robot.html ).

I've written a test applet based on one of the example sketches, and i've created a new Robot object ok i think (i.e. no exceptions thrown), but whenever i make a method call to that object, no matter the method, i get a nullPointer exception.  Does anybody have an idea why this might be happening?
Many thanks,
Thomas.

Here is my code...

public class testSketchRobot extends PApplet {
 import java.awt.*;
 Robot myRobot;
 Color pixTester;
 
 void setup()
 {
   size(200, 200);

   rectMode(CENTER);
   noStroke();
   fill(0, 102, 153, 204);
   
   //create new robot - have to use try,catch,finally as Robot constructor can throw exceptions.
   try{
     Robot myRobot = new Robot();
   } catch (AWTException e) {
     System.err.println("awt error thrown");//don't see this
   } catch (SecurityException e){
     System.err.println("SECURITY error thrown");//don't see this either
   } finally {
     System.err.println("Robot ok i think");//i do see this
   }
   
 }

 void draw()
 {
   background(255);//from the example sketch - left in just so i can see something working
   rect(width-mouseX, height-mouseY, 50, 50);//from the example sketch - left in just so i can see something working
   rect(mouseX, mouseY, 50, 50);//from the example sketch - left in just so i can see something working
   
   /*trying robot methods - each one will throw a null pointer exception*/
   myRobot.mouseMove(300,300);
   //myRobot.setAutoDelay(500);
   //pixTester = myRobot.getPixelColor(mouseX, mouseY);
   //System.err.println(myRobot.toString());
 }
 
}
Re: nullPointer thrown for every call to Robot obj
Reply #1 - Aug 21st, 2006, 10:57am
 
toemass wrote on Aug 19th, 2006, 3:22pm:
 
Code:
    try {
     Robot myRobot = new Robot();
   } catch (AWTException e) {
     System.err.println("awt error thrown");//don't see this
   } catch (SecurityException e){
     System.err.println("SECURITY error thrown");//don't see this either
   } finally {
     System.err.println("Robot ok i think");//i do see this
   }



the error is caused by declaring myRobot again inside the try block.

Code:
    try {
    myRobot = new Robot();
   } catch (AWTException e) {
     System.err.println("awt error thrown");//don't see this
   } catch (SecurityException e){
     System.err.println("SECURITY error thrown");//don't see this either
   } finally {
     System.err.println("Robot ok i think");//i do see this
   }


your code should look like this. remember that a variable only lives within the curly brakets it s been defined in.
Re: nullPointer thrown for every call to Robot obj
Reply #2 - Aug 24th, 2006, 1:23am
 
Yowser!  Absolutely works now - i think i was jumping to conclusions that it must be a Big problem as i'm a bit unsure of what i'm doing.  Thanks!
Page Index Toggle Pages: 1