Java 从spring引导应用程序连接到firestore会引发空指针异常

Java 从spring引导应用程序连接到firestore会引发空指针异常,java,spring-boot,google-cloud-firestore,connection,real-time,Java,Spring Boot,Google Cloud Firestore,Connection,Real Time,您好,我正在尝试将google cloud firestore连接到spring boot应用程序以检索一些数据,但我经常遇到相同的错误。我已经检查了在环境中设置GOOGLE_CREDENTIALS变量,但是运气不好。感谢您的帮助。提前谢谢 服务类别代码 public TrainInfo getTrainInfo(String trainId) throws InterruptedException, ExecutionException{ Firestore

您好,我正在尝试将google cloud firestore连接到spring boot应用程序以检索一些数据,但我经常遇到相同的错误。我已经检查了在环境中设置GOOGLE_CREDENTIALS变量,但是运气不好。感谢您的帮助。提前谢谢

服务类别代码

 public TrainInfo getTrainInfo(String trainId) throws InterruptedException, ExecutionException{
                Firestore db=FirestoreOptions.getDefaultInstance().getService();
                DocumentReference docRef = db.collection(COLUMN).document(trainId);
                ApiFuture<DocumentSnapshot> future = docRef.get();
                DocumentSnapshot document = future.get();
        
                TrainInfo trainInfo=null;
        
                if (document.exists()) {
                    // convert document to POJO
                    trainInfo = document.toObject(TrainInfo.class);
        
                } else {
                    System.out.println("No such document!");
                }
                return trainInfo;
            }
public TrainInfo getTrainInfo(字符串trainId)抛出InterruptedException、ExecutionException{
Firestore db=FirestoreOptions.getDefaultInstance().getService();
DocumentReference docRef=db.集合(列).文件(列车ID);
ApiFuture=docRef.get();
DocumentSnapshot document=future.get();
TrainInfo TrainInfo=null;
if(document.exists()){
//将文档转换为POJO
trainInfo=document.toObject(trainInfo.class);
}否则{
System.out.println(“没有这样的文档!”);
}
返回列车信息;
}
错误显示

  java.lang.NullPointerException: null
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:892) ~[guava-30.1.1-android.jar:na]
        at com.google.firestore.v1.DatabaseRootName.<init>(DatabaseRootName.java:56) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
        at com.google.firestore.v1.DatabaseRootName.<init>(DatabaseRootName.java:29) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
        at com.google.firestore.v1.DatabaseRootName$Builder.build(DatabaseRootName.java:157) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
        at com.google.firestore.v1.DatabaseRootName.of(DatabaseRootName.java:61) ~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
        at com.google.cloud.firestore.spi.v1.GrpcFirestoreRpc.<init>(GrpcFirestoreRpc.java:111) ~
java.lang.NullPointerException:null
在com.google.common.base.premissions.checkNotNull(premissions.java:892)~[guava-30.1.1-android.jar:na]
在com.google.firestore.v1.DatabaseRootName.(DatabaseRootName.java:56)~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
在com.google.firestore.v1.DatabaseRootName.(DatabaseRootName.java:29)~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
在com.google.firestore.v1.DatabaseRootName$Builder.build(DatabaseRootName.java:157)~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
在com.google.firestore.v1.DatabaseRootName.of(DatabaseRootName.java:61)~[proto-google-cloud-firestore-v1-2.3.0.jar:2.3.0]
位于com.google.cloud.firestore.spi.v1.GrpcFirestoreRpc.(GrpcFirestoreRpc.java:111)~

从环境中设置凭据对我不起作用,所以我从代码本身开始设置,如下所示:
凭证=谷歌凭证 .fromStream(新文件输入流(“/pathtojsonfile”); FirestoreOptions FirestoreOptions=FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(项目ID).setCredentials(凭证).build()

可能会帮助别人