javaspring-bootspring-tool-suitebitcoindbitcoin-testnet

Getting null pointer exception while creating JSONRPCCLIENT object in bitcoind


Class for creation of JsonRpcClient object for connecting my application by bitcoin test net already installed bitcoind and sync it with testnet as well from main network of bitcoin. Trying to create unique wallet address but getting null pointer exception as i can observe values are not being used from application.properties file i m using spring tool suite and maven dependencywf.bitcoin bitcoin-rpc-client 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;
    }
}

Configuration File

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 file

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

Methode for getting bitcoin wallet address

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

    }

Stack Trace

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$0(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



Solution

  • I got my solution actually the problem was of version i was using rpcport which is changed to test.rpcport for test net from bitcoind version 17.0 hence bean were not getting the connection from my localhost server causing null pointer exception.