alibaba / alibaba/fastjson2

[BUG] 2.0.57版本 Could not write JSON: Attempt to get length of null array 给null或者给空的数组也不行

Open
#3,545 7 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
Caused by: java.io.IOException: Could not write JSON: Attempt to get length of null array
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.support.retrofit.Retrofit2ConverterFactory$RequestBodyConverter.convert(Retrofit2ConverterFactory.java:139)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.support.retrofit.Retrofit2ConverterFactory$RequestBodyConverter.convert(Retrofit2ConverterFactory.java:113)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at retrofit2.ParameterHandler$Body.apply(ParameterHandler.java:438)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W ... 19 more
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W Caused by: java.lang.NullPointerException: Attempt to get length of null array
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterBaseModule$WriterAnnotationProcessor.getFieldInfo(ObjectWriterBaseModule.java:311)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterProvider.getFieldInfo(ObjectWriterProvider.java:235)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterCreator.creteFieldWriter(ObjectWriterCreator.java:147)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterCreator.lambda$createObjectWriter$1$com-alibaba-fastjson2-writer-ObjectWriterCreator(ObjectWriterCreator.java:377)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterCreator$$ExternalSyntheticLambda1.accept(D8$$SyntheticClass:0)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.util.BeanUtils.declaredFields(BeanUtils.java:324)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.util.BeanUtils.declaredFields(BeanUtils.java:255)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterCreator.createObjectWriter(ObjectWriterCreator.java:374)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterProvider.getObjectWriterInternal(ObjectWriterProvider.java:464)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterProvider.getObjectWriter(ObjectWriterProvider.java:340)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.JSONWriter.getObjectWriter(JSONWriter.java:555)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterObject.getObjectWriterVoid(FieldWriterObject.java:154)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterObject.getObjectWriter(FieldWriterObject.java:76)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterObject.writeInternal(FieldWriterObject.java:351)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterObject.write(FieldWriterObject.java:247)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:312)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:378)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterListMethod.write(FieldWriterListMethod.java:79)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriter6.write(ObjectWriter6.java:80)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterList.writeList(FieldWriterList.java:378)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.FieldWriterListMethod.write(FieldWriterListMethod.java:79)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:312)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.JSON.toJSONBytes(JSON.java:3517)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W at com.alibaba.fastjson2.support.retrofit.Retrofit2ConverterFactory$RequestBodyConverter.convert(Retrofit2ConverterFactory.java:130)
2025-05-09 10:51:42.682 6152-6152 System.err com.txc.mms.pad W ... 21 more

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Start with com.alibaba.fastjson2.writer.ObjectWriterBaseModule$WriterAnnotationProcessor.getFieldInfo at line 311, then follow the related ObjectWriterProvider and ObjectWriterCreator calls in the stack trace. Reproduce the Retrofit2 request-body conversion with null and empty arrays, and verify that JSON serialization completes without the reported NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.