title: Why I am getting java.lang.NullPointerException?
body: <p>I am trying to test this script, to get print some demo text to my Zebra printer using Android, but I am getting a <strong>java.lang.NullPointerException</strong>. I have read is something about an initialization, but I can't identify what is the object to initializate.</p>
  
  <p><strong>this is the error:</strong> </p>
  
  <pre><code>E/AndroidRuntime(2098): 
  FATAL EXCEPTION: Thread-9874
  Process: com.stihn.sibmovil, PID: 2098
  java.lang.NullPointerException
  at com.stihn.sibmovil.sendfile.SibPrint.testSendFile(SibPrint.java:88)
  at com.stihn.sibmovil.sendfile.SibPrint.access$0(SibPrint.java:86)
  at com.stihn.sibmovil.sendfile.SibPrint$1.run(SibPrint.java:53)
  at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>Thanks for your help!</p>
  
  <pre><code>import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  
  import android.os.Looper;
  
  import com.stihn.sibmovil.util.SettingsHelper;
  import com.zebra.sdk.comm.BluetoothConnection;
  import com.zebra.sdk.comm.Connection;
  import com.zebra.sdk.comm.ConnectionException;
  import com.zebra.sdk.printer.ZebraPrinter;
  import com.zebra.sdk.printer.ZebraPrinterFactory;
  import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
  import org.apache.cordova.api.Plugin;
  import org.apache.cordova.api.PluginResult;
  import com.zebra.sdk.printer.PrinterLanguage;
  import org.json.JSONArray;
  import org.json.JSONException;
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Context;
  
  public class SibPrint extends Plugin {
  
      ProgressDialog loadingDialog;
      Activity activity;
      private Context context;
      String macAddress;
      String filepath;
  
      public PluginResult execute(String action, JSONArray args, String callbackId) {
          try {
              if (action.equals("sibPrintTicket")) {
                  String echo = args.getString(0);
                  filepath = args.getString(1);
                  macAddress = args.getString(2);
                  if (echo != null &amp;&amp; echo.length() &gt; 0) {
  
                      new Thread(new Runnable() {
                          public void run() {
                              Looper.prepare();
  
                              Connection connection = null;
                              connection = new BluetoothConnection(macAddress);
  
                              try {
                                  System.out.println("Imprimiendo ...");
                                  connection.open();
                                  ZebraPrinter printer = ZebraPrinterFactory
                                          .getInstance(connection);
                                  testSendFile(printer);
  
                                  connection.close();
                              } catch (ConnectionException e) {
                                  System.out.println("Error Try 1 "
                                          + e.getMessage());
                              } catch (ZebraPrinterLanguageUnknownException e) {
                                  System.out.println("Error Catch 1 "
                                          + e.getMessage());
                              } finally {
                                  if (activity != null &amp;&amp; loadingDialog != null) {
                                      loadingDialog.dismiss();
                                  }
                              }
                              Looper.loop();
                              Looper.myLooper().quit();
                          }
  
                      }).start();
  
                      //retornar mensaje de imprimiendo
                      return new PluginResult(PluginResult.Status.OK, "Imprimiendo..");
                  } else {
                      return new PluginResult(PluginResult.Status.ERROR);
                  }
              } else {
                  return new PluginResult(PluginResult.Status.INVALID_ACTION);
              }
          } catch (JSONException e) {
              return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
          }
      }
  
      private void testSendFile(ZebraPrinter printer) {
          try {
              File filepath = context.getFileStreamPath("TEST.LBL");
              createDemoFile(printer, "TEST.LBL");
              printer.sendFileContents(filepath.getAbsolutePath());
              this.saveBluetoothAddress(this, macAddress);
  
          } catch (ConnectionException e1) {
              System.out.println("Error sending file to printer" + e1.getMessage());
          } catch (IOException e) {
              System.out.println("Error creating file" + e.getMessage());
          }
      }
  
      private void saveBluetoothAddress(SibPrint sibPrint,
              String macAddress) {
          // TODO Auto-generated method stub
  
      }    
  
         private void createDemoFile(ZebraPrinter printer, String fileName) throws IOException {
  
              FileOutputStream os = context.openFileOutput(fileName, Context.MODE_PRIVATE);
  
              byte[] configLabel = null;
  
              PrinterLanguage pl = printer.getPrinterControlLanguage();
              if (pl == PrinterLanguage.ZPL) {
                  configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
              } else if (pl == PrinterLanguage.CPCL) {
                  String cpclConfigLabel = "! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n";
                  configLabel = cpclConfigLabel.getBytes();
              }
              os.write(configLabel);
              os.flush();
              os.close();
          }
  
  }
  </code></pre>