Robot - java.lang.NullPointerException - java

I'm trying to create an object of the Robot Class. However, I'm getting ' java.lang.NullPointerException' when trying to do the same.
static Robot robot = null;
try {
robot = new Robot();
if(keyAction.equals("TABPRESS")){
robot.keyPress(KeyEvent.VK_TAB);
}else if(keyAction.equals("TABRELEASE")){
robot.keyRelease(KeyEvent.VK_TAB);
}
}catch(AWTException e){
e.printStackTrace();
}
Can you please suggest how to handle this? This code was functioning properly earlier.

Although you have not sent the code that creates keyAction this is the only thing that can cause NPE in this code fragment. Check it first.
Moreover the better practice to call equals() method is to call it on constant and pass other object as a parameter. This is null-safe:
if("TABPRESS".equals(keyAction)){
robot.keyPress(KeyEvent.VK_TAB);
}else if("TABRELEASE".equals(keyAction)){
robot.keyRelease(KeyEvent.VK_TAB);
}

I have figured out the solution. It was not because of any programming issue. Actually, there was a clash of Jre Versions on my system. I uninstalled/deleted all the jdks and installed a new one and everything started working fine.
Thanks, Shreyas.

Related

setMediaTime not setting specified time on realized player

I've a situation where I'm creating an instance of javax.Media.player (to play audio in wav foramt) using javax.Media.Manager and I've code which looks like:-
Player player = null;
MediaLocator locator = new MediaLocator("file path to wav file");
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER, new Boolean(true));
try {
player = Manager.createRealizedPlayer(locator);
}
catch (CannotRealizeException e) {
e.printStackTrace();
}
player.addControllerListener(this);
player.start();
player.setMediaTime();
And after having an instance of player I'm invoking setMediaTime on it, the problem is that sometimes the player is updated with the time provided and sometime not.
Can anyone please suggest me that what mistake I'm making.
Finally, I got this working by tweaking my code, I obtained the player instance by
Manager.createPlayer(localtor)
and got my player realized by using busy waiting method, as soon as the player gets realized I invoke the setMediaTime method to set the audio start off set.
The reason why I was facing this problem was that, BasicPlayer from JMF API spwan a new thread if the fresh player instance is created and once the player is started and realized calling setMediaTime won't have any effect on that, basically this was more a threading issue which I overlooked.

How to press "ALT+S" in Selenium WebDriver using Java

I need to Send ALT+S Key event using Selenium Web Driver for an ``EditBox. Cursor Position is already set to EditBox I am using following code
driver.switchTo().activeElement().sendKeys(Keys.chord(Keys.ALT+"S"))
but it's not giving me desired result. It's Typing character 'S' in the Edit Box.
I have tried another code but got the same result.
Actions action =new Actions(driver);
action.keyDown(Keys.ALT).sendKeys(String.valueOf('\u0053')).perform();
Thanks in Advance
I want to Add one more thing here. The code is working Properly in Firefox 12 but its not working properly in IE9
Cross-browser issues are rather hard to investigate as they are specific to particular driver and not WebDriver API.
Another variant that might work.
driver.findElement(By.xpath("your editbox's XPath")).sendKeys(Keys.chord(Keys.ALT, "s"));
As workaround I might recommend to take a look to AutoIT (Official site) or Robot (Java Doc)
Try this. It might work, I haven't tried though
driver.findElement(By.xpath("your editbox's XPath"))
.sendKeys(Keys.chord(Keys.ALT + Keys.S));
You can achieve this by using Robot class of java
try{
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
}
catch(Exception ex){
System.out.println(ex.getMessage());
}

In order to Macify a Java app, to catch the about event, do I have to implement a menu?

The macify Notepad example is the only example of 'macification' I could find, and it's all fine and dandy I guess, apart from the menu which completely screws up my layout.
I'm very, very new at Java so I follow examples like these to the letter, but this menu really has to go.
Is there a way to catch the 'about' stuff without a menu? After all, this about thing on Mac OSes seems to be there even without one. Standard procedure, etc.
I don't have a Mac to test the code, so trial and error is severely limited...
How is this done?
Bit of a necro-post but it's code I use all the time. It's complicated and uses reflection to avoid throwing errors on non-Mac systems, however.
In the initialization of your app or as a static code block:
if (System.getProperty("os.name").contains("Mac")) {
try {
Object app = Class.forName("com.apple.eawt.Application")
.getMethod("getApplication")
.invoke(null);
Object al = Proxy.newProxyInstance(
Class.forName("com.apple.eawt.AboutHandler").getClassLoader(),
new Class[]{Class.forName("com.apple.eawt.AboutHandler")},
new AboutListener()
);
app.getClass()
.getMethod("setAboutHandler", Class.forName("com.apple.eawt.AboutHandler"))
.invoke(app, al);
}
catch (Exception e) {
//fail quietly
}
}
At the bottom of the source file after the last curly brace
public class AboutListener implements InvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) {
//Show About Dialog
return null;
}
}
When releasing a full application in java things like this make nice small touches. This should be mostly copy-and-paste-able but you will need to add a line or two to display an about box. If you need to test really badly use web-start, dropbox public links, and a neighborhood Apple Store.

java: how to send accelerator key

I am using Junit4 under eclipse.
I would like to write a test which can be able to send the action : ctrl+shift+P
I tried this using JTable as I don't know for which component I could use the sendAcceleratorKey :
myTable.sendAcceleratorKey(InputEvent.CTRL, InputEvent.SHIFT_DOWN_MASK)
but I can't add a third argument to say KeyEvent.P.
How can I send this action which changes the menu?
Thanks!
I guess you can use the Robot class.
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_P);
Thread.sleep(1000); // Time for your code to react to the event
assert(...);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_P);
I can't find any reference to sendAcceleratorKey(). But if it really exists and it does what you want, it looks logic to me to use the the method this way, using key modifiers:
myTable.sendAcceleratorKey(InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK,
KeyEvent.VK_P);
Otherwise, try to swap the parameters, depending on the methods signature.
myTable.sendAcceleratorKey(KeyEvent.VK_P,
InputEvent.CTRL | InputEvent.SHIFT_DOWN_MASK);

Control a Windows apps using Java

I would like to know if there is any way I can control a Windows application using Java code. I have already googled it, and found that it can be done using JNI or a library called NewJawin.
I want to control Windows Media Player using Java code, e.g. play, pause, and change songs, but could find no relevant example to get me started so far. Do you guys have any suggestion?
As no one has answered this question, I thought I would.
public void firePlay() {
//CTRL + P
//import java.awt.Robot
//import java.awt.KeyEvent
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_CONTROL);
} catch (AWTException ex) {
Logger.getLogger(atest.class.getName()).log(Level.SEVERE, null, ex);
}
}
This would play/pause the video. You can see other shortcuts here(http://windows.microsoft.com/en-AU/windows-vista/Windows-Media-Player-keyboard-shortcuts)

Categories

Resources