title: Appium Java TestNG Framework>Launch Appium programatically via Mac null point exception with appium main.js file
body: <p>Mac book air</p>
  
  <p>Java 1.8</p>
  
  <p>Appium Desktop version 1.8.1</p>
  
  <p>Hi Guys, </p>
  
  <p>I am building a TestNG framework and and I want to start my appium Desktop server programmatically for my tests.  So what I decided to do was to create a java  test class to invoke appium for me, I thought I'd coded it ok but when I ran it just to check. Got a  <code>'null point exception'</code></p>
  
  <p><strong>1. What did I do wrong?</strong></p>
  
  <p><strong>2. How do I fix this?</strong></p>
  
  <p>Here is the launch appium programmatically code: </p>
  
  <pre><code>package aappiumLaunchServer;
  
  import java.io.File;
  
  import io.appium.java_client.service.local.AppiumServiceBuilder;
  
  public class LaunchAppium {
  
      private static AppiumServiceBuilder service;
  
      public static void main(String[] args) {
          //text
          stopAppium();
          startAppium();
          stopAppium();
      }
  
      public static void startAppium() {
          System.out.println("Start Appium Server");
          service = new AppiumServiceBuilder().usingDriverExecutable(new File("/usr/local/bin/node"))
          .withAppiumJS(new File ("/Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js "));
          service.build().start();
          System.out.println("You can now use Appium Server");
      }
  
      public static void stopAppium(){
          System.out.println("Appium Server is shutting down....");
          try {
              service.build().stop();
          } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
              System.out.println("Your current appium session is terminated... ");
          }
          System.out.println(" Appium Server has shut down. Thank you for using Appium Server");
      }
  
  }
  </code></pre>
  
  <p>Here is the message from eclipse</p>
  
  <pre><code>appium Server is shutting down....
  java.lang.NullPointerExceptionYour current appium session is terminated... 
   Appium Server has shut down. Thank you for using Appium Server
  Start Appium Server
  
      at aappiumLaunchServer.LaunchAppium.stopAppium(LaunchAppium.java:44)
      at aappiumLaunchServer.LaunchAppium.main(LaunchAppium.java:16)
  Exception in thread "main" io.appium.java_client.service.local.InvalidServerInstanceException: Invalid server instance exception has occured: The invalid appium node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js  has been defined
      at io.appium.java_client.service.local.AppiumServiceBuilder.validateNodeStructure(AppiumServiceBuilder.java:102)
      at io.appium.java_client.service.local.AppiumServiceBuilder.checkAppiumJS(AppiumServiceBuilder.java:294)
      at io.appium.java_client.service.local.AppiumServiceBuilder.createArgs(AppiumServiceBuilder.java:389)
      at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:342)
      at aappiumLaunchServer.LaunchAppium.startAppium(LaunchAppium.java:29)
      at aappiumLaunchServer.LaunchAppium.main(LaunchAppium.java:17)
  Caused by: java.io.IOException: The node /Applications/Appium.app/Contents/Resources/app/node_modules/appium/build/lib/main.js doesn't exist
      at io.appium.java_client.service.local.AppiumServiceBuilder.validateNodeStructure(AppiumServiceBuilder.java:104)
      ... 5 more
  </code></pre>