使用Stripe为已有客户添加新卡片时遭遇NullPointerException问题求助
Stripe为已有客户添加新卡片时遭遇NullPointerException问题求助
我最近在做Stripe相关的开发,想用测试令牌来指定要添加的卡片类型,测试令牌的名称是从Stripe官方文档里获取的。
我写了下面这段Java代码:
import com.stripe.Stripe; import com.stripe.exception.StripeException; import com.stripe.model.Card; import com.stripe.model.Customer; import java.util.HashMap; import java.util.Map; public class StripeExample { public static void main(String[] args) throws StripeException { final String API_KEY = System.getenv("STRIPE_API_KEY"); assert API_KEY != null : "API_KEY SHOULD NOT BE NULL"; Stripe.apiKey = API_KEY; // Retrieve the existing customer by ID Customer customer = Customer.retrieve(customerID); // Use a Stripe test token instead of raw card details String testToken = "tok_visa"; // Simulates a Visa card // Attach the test card to the customer Map<String, Object> sourceParams = new HashMap<>(); sourceParams.put("source", testToken); Card card = (Card) customer.getSources().create(sourceParams); System.out.println("Card added successfully: " + card.getId()); } }
但运行后得到了下面的错误:
null Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.stripe.model.PaymentSourceCollection.create(java.util.Map)" because the return value of "com.stripe.model.Customer.getSources()" is null at org.example.Application.main(Application.java:82)
我本来想给客户成功添加卡片并输出卡片ID,结果却遇到了空指针异常,提示customer.getSources()的返回值是null,没法调用create方法。有没有朋友能帮我看看这是怎么回事,该怎么解决呀?
备注:内容来源于stack exchange,提问作者Marius Carchilan
相关产品推荐
相关产品推荐

