title: Java Robot - Class functions issue
body: <p>I am trying to make a separate class for the Java AWT Robot to use with projects but i am having trouble setting it up how i would like as all of the examples I have found online seem to pack the code into a single .java file instead.</p>
  
  <p>My code works fine however I am wondering if I could setup the functions in a nicer way.</p>
  
  <p>The code for the RobotLib.java class is as follows:</p>
  
  <pre><code>package com.z;
  
  import java.awt.*;
  import java.awt.event.*;
  import java.util.*;
  
      public class RobotLib {
  
      private static Robot robot;
  
      // Press Function
      public void Press(int key, int time){ 
          try {
          Robot robot = new Robot();
              robot.keyPress(key);
              robot.delay(time);
              robot.keyRelease(key);
  
          } catch (AWTException e) {
              e.printStackTrace();
          }
      } 
  
  }
  </code></pre>
  
  <p>And my Example.java code is:</p>
  
  <pre><code>package com.z;
  
  import java.awt.*;
  import java.awt.event.*;
  import java.util.*;
  
  public class Example {
  
      public static void main(String[] args) {
  
          RobotLib robot = new RobotLib();
  
          robot.Press(KeyEvent.VK_A,100); // type a
  
      }
  }
  </code></pre>
  
  <p>With the RobotLib.java class I was wondering if it's possible to have the functions without wrapping them with try/catch and new Robot() so instead of the above version it would be something like this instead:</p>
  
  <pre><code>public void Press(int key, int time){ 
      robot.keyPress(key);
      robot.delay(time);
      robot.keyRelease(key);
  }
  </code></pre>
  
  <p>The try/catch and new Robot() seem to be required however and if I take those away I get errors like this:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at com.z.RobotLib.Press(RobotLib.java:35)
      at com.z.Example.main(Example.java:14)
  </code></pre>
  
  <p>I am quite new to Java coding and might be setting up the class the wrong way, is there a way to fix that error or have the functions how I want?</p>