2017-08-31 621 views
-2

我编写了一些Java钱包生成代码,并将其用于生成加密货币钱包。该代码提供,POST请求后返回空值

public synchronized WalletInfo generateAddress(GenerateWallet generateWallet) { 

     final WalletInfo walletInfo = new WalletInfo(); 

     String walletName = generateWallet.getWalletName(); 

     String currencyName = generateWallet.getCurrencyName(); 

     WalletInfo walletInfoDb = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName); 

     if (walletInfoDb == null && genWalletMap.get(walletName) == null) { 

      String currency = currencyName.toUpperCase(); 

      if (currency.equals("BITCOIN")) { 

       final WalletManager walletManager = WalletManager.setupWallet(walletName); 

       walletManager.addWalletSetupCompletedListener((wallet) -> { 

        Address address = wallet.currentReceiveAddress(); 
        WalletInfo newWallet = createWalletInfo(walletName, currencyName, address.toString()); 

        // set the properties of the walletInfo 
        // the instance is final and can work inside the lambda expression 
        walletInfo.setId(newWallet.getId()); 
        walletInfo.setName(newWallet.getName()); 
        walletInfo.setAddress(newWallet.getAddress()); 
        walletInfo.setCurrency(newWallet.getCurrency()); 

        walletMangersMap.put(newWallet.getId(), walletManager); 
        genWalletMap.remove(walletName); 
       }); 

       genWalletMap.put(walletName, walletManager); 
       return walletInfo; 
      } else if (currency.equals("ETHEREUM")) { 
       return walletInfo; 
      } else { 
       return walletInfo; 
      } 
     } 

     return walletInfo; 
    } 

当我使用cURL

curl -H "Content-Type: application/json" -X POST -d '{"walletName": "Florence8","currencyName":"Bitcoin"}' http://localhost:8080/rest/wallet/generateAddress 

我得到null是返回一个POST要求,

{ 
    "id" : null, 
    "name" : null, 
    "address" : null, 
    "currency" : null 
} 

虽然产生的实体,仍然坚持在MySQL中。

我继续调试,这是有线。调试不遵循代码的top-to-bottom序列。调试的顺序是什么样子,

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

我的意思是,如果代码涉及到这一行walletManager.addWalletSetupCompletedListener((wallet)那么它就应该执行里面的操作吧?

任何操作如何在数据库中正确保存后返回实体?在lambda表达式中使用

+2

请将您的代码缩减为[mcve]。 –

+1

为什么不直接制作'walletInfo'' final'? –

+2

我没有看到上面那个'walletInfo'不能是最终的任何理由。删除'= new WalletInfo()',因为您只是将其覆盖在下一行上,并将其合并到一行'final WalletInfo walletInfo = iWalletInfoDao/* ...* /;'还要注意,在if(walletInfo == null)分支中调用'walletInfo'上的方法没有什么意义,因为这样做会抛出一个NPE。 –

回答

2

变量应该是最终的或有效的最终

的问题是,你重新分配值,你声明变量后 - 事实上,第一次转让是多余的,因为您只需覆盖该值而不使用它。 -

因此,通过去除第一分配有效使它final

WalletInfo walletInfo = iWalletInfoDao.getWalletInfoWithWalletNameAndCurrency(walletName, currencyName); 

实际上final

final WalletInfo walletInfo = iWalletInfoDao.get/*etc*/ 

此外,这种情况:

if (walletInfo == null) { 

被倒置:在该块内部,您调用walletInfo上的方法;由于walletInfo为空,因此呼叫将失败NullPointerException

+2

也许值得注意的是,他也在使用'if(walletInfo == null)... walletInfo.setName(walletName);'。 – Nathan

+0

我看你们都是对的。的确,我改变了这个问题。你想再看一次吗?顺便说一句,我创建了一个新的实例,所以不会得到'NullPointerException' – Arefe