Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException in AbstractKafkaHeaderMapper #3581

Closed
ITman1 opened this issue Oct 21, 2024 · 1 comment · Fixed by #3582
Closed

NullPointerException in AbstractKafkaHeaderMapper #3581

ITman1 opened this issue Oct 21, 2024 · 1 comment · Fixed by #3582

Comments

@ITman1
Copy link

ITman1 commented Oct 21, 2024

In what version(s) of Spring for Apache Kafka are you seeing this issue?

3.2.4

Describe the bug

When we updated Spring Boot to latest RELEASE version we started to get NullPointerException from our listener.

I am unable to find out what triggered this, but is happening when there is in ConsumerRecord some RecordHeader with null value.
image
This is probably bug in our custom application, but I think Spring Kafka header mapper should handle such cases in better way other than to throw NPE.

Caused by: java.lang.NullPointerException: Cannot read the array length because "bytes" is null
	at java.base/java.lang.String.<init>(String.java:1425)
	at org.springframework.kafka.support.AbstractKafkaHeaderMapper.mapRawIn(AbstractKafkaHeaderMapper.java:281)
	at org.springframework.kafka.support.AbstractKafkaHeaderMapper.headerValueToAddIn(AbstractKafkaHeaderMapper.java:270)
	at org.springframework.kafka.support.DefaultKafkaHeaderMapper.lambda$toHeaders$2(DefaultKafkaHeaderMapper.java:328)
	at java.base/java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.kafka.support.DefaultKafkaHeaderMapper.toHeaders(DefaultKafkaHeaderMapper.java:310)
	at org.springframework.kafka.support.converter.MessagingMessageConverter.mapOrAddHeaders(MessagingMessageConverter.java:206)
	at org.springframework.kafka.support.converter.MessagingMessageConverter.toMessage(MessagingMessageConverter.java:184)
	at org.springframework.kafka.listener.adapter.MessagingMessageListenerAdapter.toMessagingMessage(MessagingMessageListenerAdapter.java:377)
	at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:77)
	at org.springframework.kafka.listener.adapter.RecordMessagingMessageListenerAdapter.onMessage(RecordMessagingMessageListenerAdapter.java:50)
	at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeOnMessage(KafkaMessageListenerContainer.java:2800)
	... 13 common frames omitted
@artembilan
Copy link
Member

The code there is like this:

	@Nullable
	private String mapRawIn(String header, byte[] value) {
		Boolean asString = this.rawMappedHeaders.get(header);
		if (Boolean.TRUE.equals(asString)) {
			return new String(value, this.charset);
		}
		return null;
	}

The Strinfg ctor indeed does not allow a null.
So, yeah, has to be fixed respectively.

Thank you for the catch!

@artembilan artembilan added this to the 3.3.0-RC1 milestone Oct 21, 2024
sobychacko added a commit to sobychacko/spring-kafka that referenced this issue Oct 21, 2024
Fixes: spring-projects#3581

spring-projects#3581

- Optimize headerValueToAddIn method by adding better null checks
- Add unit test to verify
artembilan pushed a commit that referenced this issue Oct 21, 2024
Fixes: #3581

#3581

- Optimize headerValueToAddIn method by adding better null checks
- Add unit test to verify

**Auto-cherry-pick to `3.2.x` & `3.1.x`**
artembilan pushed a commit that referenced this issue Oct 21, 2024
Fixes: #3581

#3581

- Optimize headerValueToAddIn method by adding better null checks
- Add unit test to verify

**Auto-cherry-pick to `3.1.x`**

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/AbstractKafkaHeaderMapper.java
spring-builds pushed a commit that referenced this issue Oct 21, 2024
Fixes: #3581

#3581

- Optimize headerValueToAddIn method by adding better null checks
- Add unit test to verify

# Conflicts:
#	spring-kafka/src/main/java/org/springframework/kafka/support/AbstractKafkaHeaderMapper.java

(cherry picked from commit cb8740d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants