-3

I've created Keyboard class for working with Robot. But when I starting using methods from this class I have error in logs.

Here's my Keyboard.java with method pressEscape():

public class Keyboard {

    private static Robot robot;
    private static int time = 1000;

    public Keyboard(){
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

   public void pressEscape() throws TestException {
        if (!getSession().CanRun())
            throw new TestException(InvalidStateMessage);
        robot.delay(time);
        robot.keyPress(KeyEvent.VK_ESCAPE);
        robot.delay(time);
        robot.keyRelease(KeyEvent.VK_ESCAPE);
        robot.delay(time);
    }

}

And here's error from log:

[2015/08/13 15:43:29] [ID:7F4315A] [Selenium]: java.lang.NullPointerException
[2015/08/13 15:43:29] [ID:7F4315A] [Selenium]: at Kodak.AutoTest.Framework.Keyboard.pressEscape(Keyboard.java:128)

Change a little method:

public void pressEscape() throws TestException {
        if (!getSession().CanRun())
            throw new TestException(InvalidStateMessage);
        try {
            new Robot();
            robot.setAutoDelay(time);
            robot.keyPress(KeyEvent.VK_ESCAPE);     
            robot.setAutoDelay(time);
            robot.keyRelease(KeyEvent.VK_ESCAPE);
        } catch (AWTException e) {
            e.printStackTrace();
        }

    }

but still has the same error on line:

robot.keyPress(KeyEvent.VK_ESCAPE); 
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Jason P.
  • 1
  • 3

3 Answers3

0

It looks as though the only place you can realistically get a null pointer exception in that method is in this line:

if (!getSession().CanRun())

which will throw a NPE if getSession() returns null. Actually the other option is if you failed to initialise robot, but then you'd be seeing an AWTException in the logs.

But you should confirm this by looking at the line number that the logs gave you. If this is right, I'd suggest changing

if (!getSession().CanRun())
            throw new TestException(InvalidStateMessage);

to

Session session = getSession();
if ((session==null) || !session.CanRun())
            throw new TestException(InvalidStateMessage);

That will harden that part, and mean you'll get a TestException if the session is null. (I am guessing the type that getSession() returns.)

Additionally, although you are initialising robot in your constructor, you should be aware that it's a static field. That means that if you have lots of Keyboard instances, every time you create an instance, it'll overwrite the previous instance of robot with a new one, because they all share the same one.

You should either change it to an instance field (remove the static modifier), or initialise it only once (check if it's null, and initialise it only if so).

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
0

For this to happen your robot variable should be null at line 128. This can happen only new Robot() threw an exception. Check your logs to see whether it is the case. And also see comments to your questions from MadProgrammer and act on them.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • was trying this: public void pressEscape() throws TestException { if (!getSession().CanRun()) throw new TestException(InvalidStateMessage); try { new Robot(); robot.setAutoDelay(time); robot.keyPress(KeyEvent.VK_ESCAPE); robot.setAutoDelay(time); robot.keyRelease(KeyEvent.VK_ESCAPE); } catch (AWTException e) { e.printStackTrace(); } } and has the same error on line: robot.keyPress(KeyEvent.VK_ESCAPE); – Jason P. Aug 14 '15 at 07:46
  • You should have added as code to the original question. Atleast in the pasted code, I don't see `new Robot()` being assigned to `robot`. – Dakshinamurthy Karra Aug 14 '15 at 07:48
0

It's working now, here's full code:

public class Keyboard {

    private static Robot robot;

   public void pressEscape() throws TestException {
    if (!getSession().CanRun())
        throw new TestException(InvalidStateMessage);
    try {
        robot = new Robot();
        robot.setAutoDelay(time);
        robot.keyPress(KeyEvent.VK_ESCAPE);     
        robot.setAutoDelay(time);
        robot.keyRelease(KeyEvent.VK_ESCAPE);
    } catch (AWTException e) {
        e.printStackTrace();
    }

}

}
Jason P.
  • 1
  • 3