title: NullPointerException when loading serviceAccountKey.json
body: <p>How do I load and parse the <code>JSON</code> file <code>serviceAccountKey.json</code>?</p>
  
  <pre><code>thufir@doge:~/NetBeansProjects/mbaasFirebase$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ gradle --stacktrace clean fatJar;java -jar build/libs/mbaasFirebase-all.jar 
  
  BUILD SUCCESSFUL in 14s
  4 actionable tasks: 3 executed, 1 up-to-date
  Jul 19, 2017 10:13:23 PM net.bounceme.dur.mbaas.firebase.Main main
  INFO: mbaasFirebase starting..
  Exception in thread "main" java.lang.NullPointerException
      at net.bounceme.dur.mbaas.firebase.FirebaseConnection.query(FirebaseConnection.java:46)
      at net.bounceme.dur.mbaas.firebase.FirebaseConnection.runQuery(FirebaseConnection.java:37)
      at net.bounceme.dur.mbaas.firebase.Main.run(Main.java:22)
      at net.bounceme.dur.mbaas.firebase.Main.main(Main.java:16)
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cat src/
  main/ test/ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cat src/
  main/ test/ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cat src/main/
  java/      resources/ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cat src/main/
  java/      resources/ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cat src/main/java/net/bounceme/dur/mbaas/firebase/
  FirebaseConnection.java  PropertiesHelper.java    User.java                
  Main.java                UserHelper.java          
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cat src/main/java/net/bounceme/dur/mbaas/firebase/
  FirebaseConnection.java  PropertiesHelper.java    User.java                
  Main.java                UserHelper.java          
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cat src/main/java/net/bounceme/dur/mbaas/firebase/FirebaseConnection.java 
  
  
  package net.bounceme.dur.mbaas.firebase;
  
  import com.google.firebase.FirebaseApp;
  import com.google.firebase.FirebaseOptions;
  import com.google.firebase.auth.FirebaseCredentials;
  import com.google.firebase.database.DataSnapshot;
  import com.google.firebase.database.DatabaseError;
  import com.google.firebase.database.DatabaseReference;
  import com.google.firebase.database.FirebaseDatabase;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.net.URISyntaxException;
  import java.util.Properties;
  import java.util.concurrent.CountDownLatch;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class FirebaseConnection {
  
      private static final Logger log = Logger.getLogger(FirebaseConnection.class.getName());
      private DatabaseReference databaseReference = null;
      private Properties firebaseProperties = null;
  
      public FirebaseConnection() {
      }
  
      void setProperties(Properties firebaseProperties) {
          this.firebaseProperties = firebaseProperties;
      }
  
      public void runQuery() {
          try {
              query();
          } catch (IOException | InterruptedException | URISyntaxException ex) {
              Logger.getLogger(FirebaseConnection.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      private void query() throws FileNotFoundException, IOException, InterruptedException, URISyntaxException {
          String url = firebaseProperties.getProperty("url");
          log.fine(url);
          FileInputStream serviceAccount = new FileInputStream(new File(getClass().getResource("serviceAccounteyKey.json").toURI()));
          log.info(serviceAccount.toString());
          FirebaseOptions options = new FirebaseOptions.Builder()
                  .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                  .setDatabaseUrl(url)
                  .build();
          FirebaseApp firebaseApp = FirebaseApp.initializeApp(options);
          FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp, url);
          databaseReference = firebaseDatabase.getReference().getRoot();
  
          log.info(databaseReference.getKey());
  
          CountDownLatch latch = new CountDownLatch(1);
          databaseReference.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
              @Override
              public void onDataChange(DataSnapshot dataSnapshot) {
                  String key = null;
                  String value = null;
                  for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
                      key = childSnapshot.getKey();
                      value = (String) childSnapshot.getValue();
                      log.info(key + "\t\t" + value);
                  }
                  latch.countDown();
              }
  
              @Override
              public void onCancelled(DatabaseError databaseError) {
                  latch.countDown();
                  throw databaseError.toException();
              }
          });
          latch.await();
      }
  
  }
  
  
  
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase$ cd build/libs/
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ ll
  total 6620
  drwxrwxr-x 2 thufir thufir    4096 Jul 19 22:13 ./
  drwxrwxr-x 6 thufir thufir    4096 Jul 19 22:13 ../
  -rw-rw-r-- 1 thufir thufir 6768013 Jul 19 22:13 mbaasFirebase-all.jar
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ jar xf mbaasFirebase-all.jar 
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ ll
  total 6652
  drwxrwxr-x 7 thufir thufir    4096 Jul 19 22:14 ./
  drwxrwxr-x 6 thufir thufir    4096 Jul 19 22:13 ../
  -rw-rw-r-- 1 thufir thufir     598 Jun 13 17:24 admin_sdk.properties
  drwxrwxr-x 4 thufir thufir    4096 Jun 13 17:24 com/
  -rw-rw-r-- 1 thufir thufir      37 Jul 19 22:13 firebase.properties
  drwxrwxr-x 3 thufir thufir    4096 Jan  8  2009 javax/
  -rw-rw-r-- 1 thufir thufir 6768013 Jul 19 22:13 mbaasFirebase-all.jar
  drwxrwxr-x 4 thufir thufir    4096 Jul 19 22:13 META-INF/
  drwxrwxr-x 3 thufir thufir    4096 Jul 19 22:13 net/
  drwxrwxr-x 4 thufir thufir    4096 Aug 10  2016 org/
  -rw-rw-r-- 1 thufir thufir    2338 Jul 19 22:13 serviceAccountKey.json
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ head -n 2 serviceAccountKey.json 
  {
    "type": "service_account",
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ 
  thufir@doge:~/NetBeansProjects/mbaasFirebase/build/libs$ 
  </code></pre>
  
  <p>see also:</p>
  
  <p><a href="https://stackoverflow.com/q/45019504/262852">Compiling Java Maven project in Netbeans causing app to not run</a></p>
  
  <p><a href="https://stackoverflow.com/q/44981655/262852">How do I correctly add the file input stream to my Firebase JSON file</a></p>
  
  <p><a href="https://stackoverflow.com/q/41762124/262852">FileNotFoundException while running as a jar</a></p>