2017-08-31 109 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