使用我的项目ID

时间:2019-04-19 16:56:34

标签: spring google-app-engine google-cloud-platform google-cloud-firestore

我正在尝试通过Google App Engine Web应用程序建立Firestore连接。我已按照Google网站(https://cloud.google.com/firestore/docs/quickstart-servers)上的教程进行操作,并设置了必要的服务密钥,并使用密钥将环境变量GOOGLE_APPLICATION_CREDENTIALS设置为json文件的路径。但是,当我调用build()方法设置项目ID时,我一直在线获取NullPointerException。有没有人遇到过同样的问题,并且以前已经解决过?我在网上找不到任何类似的问题。代码行不多,因此看来问题应该相对简单。

这是错误本身: 造成原因: java.lang.NullPointerException     在com.google.common.base.Preconditions.checkNotNull(Preconditions.java:770)     在com.google.cloud.ServiceOptions $ Builder.setCredentials(ServiceOptions.java:203)     在com.google.cloud.firestore.FirestoreOptions $ Builder.build(FirestoreOptions.java:213)     在com.tellafirma.mappers.PierMapper。(PierMapper.java:63)     在com.tellafirma.controller.PierController.displayLogin(PierController.java:60)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处     在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)     在org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)     在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)     在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)     在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)     在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)     在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)     在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)     在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)     在org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:687)     在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)     在org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:867)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1623)     在com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:134)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1610)     在com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1610)     在com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1610)     在com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1610)     在com.google.appengine.tools.development.jetty9.StaticFileFilter.doFilter(StaticFileFilter.java:123)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1610)     com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)     在com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)     在com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1610)     在com.google.appengine.tools.development.DevAppServerRequestLogFilter.doFilter(DevAppServerRequestLogFilter.java:44)     在org.eclipse.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1602)     在org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:540)     在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:146)     在org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:524)     在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)     在org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:257)     在org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1588)     在org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:255)     在org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1345)     在org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:203)     在org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:480)     在org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1557)     在org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:201)     在org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1247)     在com.google.appengine.tools.development.jetty9.DevAppEngineWebAppContext.doScope(DevAppEngineWebAppContext.java:94)     在org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:144)     在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)     在com.google.appengine.tools.development.jetty9.JettyContainerService $ ApiProxyHandler.handle(JettyContainerService.java:595)     在org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)     在org.eclipse.jetty.server.Server.handle(Server.java:502)     在org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:364)     在org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:260)     在org.eclipse.jetty.io.AbstractConnection $ ReadCallback.succeeded(AbstractConnection.java:305)     在org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:103)     在org.eclipse.jetty.io.ChannelEndPoint $ 2.run(ChannelEndPoint.java:118)     在org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)     在org.eclipse.jetty.util.thread.QueuedThreadPool $ 2.run(QueuedThreadPool.java:683)     在java.lang.Thread.run(Thread.java:748)

我尝试重做服务密钥并通过命令行再次设置GOOGLE_APPLICATION_CREDENTIALS。我已打印出FirestoreOptions对象和FirestoreOptions.Builder对象,并且这些对象的内存地址为非null。我分离了每个方法调用,以尝试查看错误发生在何处,并且在build()行上的specificalyl。我还更新了云组件,以查看是否有任何过时的内容。

    public PierMapper(String id)
{
    projectId = id;
    firestoreOptions = FirestoreOptions.getDefaultInstance();
    System.out.println(firestoreOptions.toString());
    firestoreBuilder = firestoreOptions.toBuilder();
    System.out.println(firestoreBuilder.toString());
    firestoreBuilder = firestoreBuilder.setProjectId(projectId);
    System.out.println(firestoreBuilder.toString());
    System.out.println(firestoreBuilder.build());
    firestoreOptions = firestoreBuilder.build();
    database = firestoreOptions.getService();
}

我需要使此连接与Firestore保持正常连接,以便可以开始从Web应用程序读取和写入数据。

1 个答案:

答案 0 :(得分:0)

尝试将GOOGLE_APPLICATION_CREDENTIALS设置为json文件的完整路径。我怀疑您的应用程序找不到您的密钥文件。

您还可以尝试将gcloud auth application-default login用作described here,而不是env变量。