kafka消费者java.lang.NullPointerException

最近写kafka程序突然发现消费者会产程空指针异常,百思不得其解于是就百度寻找解决方案。参考了一下链接网上的结论但是感觉还是不怎么样于是自己一步一步寻找根源
发现源码中这么一句代码

    public Map<String, ConsumerRecords<K, V>> poll(long timeout) {
        return null;
    }

他直接返回了null指针,于是空指针异常就产生了,于是我产看这个类的文件地址

spark-examples-1.6.0-hadoop2.6.0.jar

他是spark-example中的类,并不是我导入的kafka文件夹中的lib包中的类,于是我把它删了,然后就可以了
我们也查看kafka中这个方法的实现

   public ConsumerRecords<K, V> poll(long timeout) {
        this.acquire();

        try {
            if(timeout < 0L) {
                throw new IllegalArgumentException("Timeout must not be negative");
            } else if(this.subscriptions.hasNoSubscriptionOrUserAssignment()) {
                throw new IllegalStateException("Consumer is not subscribed to any topics or assigned any partitions");
            } else {
                long start = this.time.milliseconds();
                long remaining = timeout;

                do {
                    Map records = this.pollOnce(remaining);
                    if(!records.isEmpty()) {
                        if(this.fetcher.sendFetches() > 0 || this.client.hasPendingRequests()) {
                            this.client.pollNoWakeup();
                        }

                        ConsumerRecords elapsed1;
                        if(this.interceptors == null) {
                            elapsed1 = new ConsumerRecords(records);
                            return elapsed1;
                        }

                        elapsed1 = this.interceptors.onConsume(new ConsumerRecords(records));
                        return elapsed1;
                    }

                    long elapsed = this.time.milliseconds() - start;
                    remaining = timeout - elapsed;
                } while(remaining > 0L);

                ConsumerRecords records1 = ConsumerRecords.empty();
                return records1;
            }
        } finally {
            this.release();
        }
    }

可以看到这里并不是null所以是没问题的。希望我的方法对你也有用!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天心有情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值