1

How do I load and parse the JSON file serviceAccountKey.json?

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$ 

see also:

Compiling Java Maven project in Netbeans causing app to not run

How do I correctly add the file input stream to my Firebase JSON file

FileNotFoundException while running as a jar

Thufir
  • 8,216
  • 28
  • 125
  • 273
  • Your question isn't really about Firebase, or NullPointerExceptions, or JSON etc. It looks like it's just about "How do I get an InputStream to a resource". If that's *not* what it's about, please be more specific... but reduce your question to a [mcve]. Basically it looks like you should be calling `getResourceAsStream` instead of trying to create a `FileInputStream. – Jon Skeet Jul 20 '17 at 06:15
  • close the question. As I recall loading the `properties` file works fine --- but `json` seems to be different. Bear in mind that the linked questions use the idiom utilized in my code -- or, rather, I use the idiom used in the linked examples. – Thufir Jul 20 '17 at 14:08
  • Nope, getting an input stream for a JSON file is exactly the same as getting an input stream for a properties file. – Jon Skeet Jul 20 '17 at 14:55

0 Answers0