在 bitcoind 中创建 JSONRPCCLIENT 对象时出现空指针异常

Getting null pointer exception while creating JSONRPCCLIENT object in bitcoind

Class 用于创建 JsonRpcClient 对象以通过比特币测试网连接我的应用程序 已经安装了 bitcoind 并将其与测试网以及比特币主网络同步。 尝试创建唯一的钱包地址但出现空指针异常,因为我可以观察到 application.properties 文件中的值未被使用 我正在使用 spring 工具套件和 Maven dependencywf.bitcoin 比特币-rpc-客户端 1.1.0

package bitcoin.utils;

import java.net.URL;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.naming.CommunicationException;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import wf.bitcoin.javabitcoindrpcclient.BitcoinJSONRPCClient;
import wf.bitcoin.javabitcoindrpcclient.BitcoinRPCException;

@Configuration
@Component
public class ResourceUtils {

    private static final Logger logger = LoggerFactory.getLogger(ResourceUtils.class);

    @Value("${node.bitcoind.rpc.protocol}")
    private static String protocol;
    @Value("${node.bitcoind.rpc.host}")
    private static String host;
    @Value("${node.bitcoind.rpc.port}")
    private static String port;
    @Value("${node.bitcoind.rpc.user}")
    private static String user;
    @Value("${node.bitcoind.rpc.password}")
    private static String password;
    @Value("${node.bitcoind.http.auth_scheme}")
    private static String authScheme;

    private static Properties nodeConfig;


    @PostConstruct
    void init() {
        logger.info("in init {}", "in init");
        nodeConfig = new Properties();
        nodeConfig.setProperty("node.bitcoind.rpc.protocol", protocol);
        logger.debug("protocol==============================================={}", protocol);
        nodeConfig.setProperty("node.bitcoind.rpc.host", host);
        logger.debug("host==============================================={}", host);
        nodeConfig.setProperty("node.bitcoind.rpc.port", port);
        logger.debug("port==============================================={}", port);
        nodeConfig.setProperty("node.bitcoind.rpc.user", user);
        logger.debug("user==============================================={}", user);
        nodeConfig.setProperty("node.bitcoind.rpc.password", password);
        logger.debug("password==============================================={}", password);
        nodeConfig.setProperty("node.bitcoind.http.auth_scheme", authScheme);
        logger.debug("authScheme==============================================={}", authScheme);
    }

    public ResourceUtils() {

    }

    public static CloseableHttpClient getHttpProvider() {
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        return HttpClients.custom().setConnectionManager(connManager).build();

    }

    public static BitcoinJSONRPCClient getBtcdProvider() throws BitcoinRPCException, CommunicationException {
        logger.info("getHttpProvider {}", getHttpProvider());
        logger.info("getNodeConfig {}", getNodeConfig());
        BitcoinJSONRPCClient bitcoinJsonRpcClient = null;
        try {
            URL url = new URL(protocol + "://" + user + ':' + password + "@" + host + ":" + port + "/");
            bitcoinJsonRpcClient = new BitcoinJSONRPCClient(url);
            logger.info("btcdClientt {}", bitcoinJsonRpcClient);
            // bitcoinJsonRpcClient.setTxFee(BigDecimal.valueOf(0.001));
        } catch (Exception e) {
            return bitcoinJsonRpcClient;
        }
        return bitcoinJsonRpcClient;
    }

    public static Properties getNodeConfig() {
        return nodeConfig;
    }
}

配置文件

node.bitcoind.rpc.protocol = http
node.bitcoind.rpc.host = 127.0.0.1
node.bitcoind.rpc.port = 8332
node.bitcoind.rpc.user = username
node.bitcoind.rpc.password = password
node.bitcoind.http.auth_scheme = Basic

Bitcoin.config 文件

rpcuser=user
rpcpassword=password
testnet=1
rpcport=8332
rpcallowip=127.0.0.1
rpcallowip=195.154.11.93
server=1

比特币钱包地址获取方法

public String generateWalletAddress(String id) {
        try {
            BitcoinJSONRPCClient client = ResourceUtils.getBtcdProvider();
            return client.getNewAddress();
        } catch (CommunicationException c) {
            System.err.println(c);
            return null;
        }

    }

堆栈跟踪

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-12 11:50:45.205 ERROR 10991 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceUtils': Invocation of init method failed; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at bitcoin.BitcoinApplication.main(BitcoinApplication.java:10) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.4.RELEASE.jar:2.2.4.RELEASE]
Caused by: java.lang.NullPointerException: null
    at java.util.Hashtable.put(Hashtable.java:460) ~[na:1.8.0_242]
    at java.util.Properties.setProperty(Properties.java:166) ~[na:1.8.0_242]
    at bitcoin.utils.ResourceUtils.init(ResourceUtils.java:47) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_242]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_242]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_242]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_242]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 23 common frames omitted


我得到了我的解决方案,实际上问题出在我使用的版本 rpcport 从 bitcoind 版本 17.0 更改为 test.rpcport for test net 因此 bean 没有从我的本地主机服务器获得连接,导致空指针异常。