1
resposta

NullPointer no método isFraud

Olá, eu fiz a solução de um mesmo tópico que já estava aqui com a mesma pergunta, porém não funcionou.

package com.br.alura;

import org.apache.kafka.clients.consumer.ConsumerRecord;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;

public class FraudDetectorService {
    public static void main(String[] args) {
        var fraudService = new FraudDetectorService();
        try (var service = new KafkaService
                (EmailService.class.getSimpleName(),
                        "ECOMMERCE_NEW_ORDER",
                        fraudService::parse, Order.class,
                        new HashMap<String, String>())) {
            service.run();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }


    }

    private void parse(ConsumerRecord<String, Order> record) throws InterruptedException {
        System.out.println("------------------------------");
        System.out.println("Processing new order");
        System.out.println(record.key());
        System.out.println(record.value());
        System.out.println(record.partition());
        System.out.println(record.offset());
        Thread.sleep(500);
        var order = record.value();
        if(isFraud(order)) {
            System.out.println("Order is a fraud");
        } else {
            System.out.println("Approved" + order);
        }
        System.out.println("Order processed");
    }

    private boolean isFraud(Order order) {
        return order.getAmount().compareTo(new BigDecimal("4500")) >= 0;
    }


}
1 resposta
Exception in thread "main" org.apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition ECOMMERCE_NEW_ORDER-0 at offset 0. If needed, please seek past the record to continue consumption.
Caused by: java.lang.NullPointerException
    at com.google.gson.internal.$Gson$Preconditions.checkNotNull($Gson$Preconditions.java:39)
    at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
    at com.google.gson.reflect.TypeToken.get(TypeToken.java:296)
    at com.google.gson.Gson.fromJson(Gson.java:930)
    at com.google.gson.Gson.fromJson(Gson.java:897)
    at com.google.gson.Gson.fromJson(Gson.java:846)
    at com.google.gson.Gson.fromJson(Gson.java:817)
    at com.br.alura.GsonDeserializer.deserialize(GsonDeserializer.java:29)
    at org.apache.kafka.common.serialization.Deserializer.deserialize(Deserializer.java:60)
    at org.apache.kafka.clients.consumer.internals.Fetcher.parseRecord(Fetcher.java:1386)
    at org.apache.kafka.clients.consumer.internals.Fetcher.access$3400(Fetcher.java:133)
    at org.apache.kafka.clients.consumer.internals.Fetcher$CompletedFetch.fetchRecords(Fetcher.java:1617)
    at org.apache.kafka.clients.consumer.internals.Fetcher$CompletedFetch.access$1700(Fetcher.java:1453)
    at org.apache.kafka.clients.consumer.internals.Fetcher.fetchRecords(Fetcher.java:686)
    at org.apache.kafka.clients.consumer.internals.Fetcher.fetchedRecords(Fetcher.java:637)
    at org.apache.kafka.clients.consumer.KafkaConsumer.pollForFetches(KafkaConsumer.java:1303)
    at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1237)
    at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1210)
    at com.br.alura.KafkaService.run(KafkaService.java:40)
    at com.br.alura.FraudDetectorService.main(FraudDetectorService.java:17)

Stack trace do erro