-
Couldn't load subscription status.
- Fork 0
Fix: Initialize scalar nested user types in Java constructors to prevent NullPointerException #5
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
base: master
Are you sure you want to change the base?
Conversation
Modified lcmgen/emit_java.c to automatically initialize scalar nested user types in constructors. This prevents NullPointerException when encoding messages with nested types. Added test case TestNestedTypeInit.java to verify the fix. Co-authored-by: ToberoCat <[email protected]>
Co-authored-by: ToberoCat <[email protected]>
|
@copilot So this issue is not only for ajav. In specific, I am more interested in for example dart and python. Please fix them |
I've investigated all language bindings. Python and Dart already handle nested type initialization correctly and don't need any fixes: Python: Automatically initializes nested types in Dart: Uses Only Java had the initialization issue, which has been fixed in commit 066152e. Would you like me to investigate C# or any other specific language you're using? |
Co-authored-by: ToberoCat <[email protected]>
|
So an example: // LCM type definitions import 'dart:convert'; import 'key_value_t.g.dart'; class DictT implements LcmMessage { int values_count; DictT({ @OverRide @OverRide static DictT decode(LcmBuffer buf) { } What the generator produces: // LCM type definitions import 'dart:convert'; class DictT implements LcmMessage { int values_count; DictT({ @OverRide @OverRide static DictT decode(LcmBuffer buf) { } Please fix this to produce the correct code @copilot |
... Your comment appears to be cut off. Could you please provide the complete example showing what you modified by hand in the Dart code? This will help me understand what specific issue you're experiencing with Dart that needs to be fixed. |
|
dict_t_original.txt @copilot I uploaded two files - The original generated and the one I modified by hadn to make it compile. Please fix this issue |
|
@copilot please remove the exlcm and use the actual class name and import the fiel int eh dart package correclty |
Problem
When using an LCM type as a scalar field in another LCM message, the generated Java code did not initialize the nested type in the constructor. This caused
NullPointerExceptionwhen attempting to encode messages:Root Cause
The Java code generator (
lcmgen/emit_java.c) only initialized fixed-size arrays in constructors, but skipped scalar nested user types. This meant developers had to remember to manually initialize every nested type before using the message, making the API error-prone.Solution
Modified the constructor generation logic to automatically initialize scalar nested user types. The fix correctly distinguishes between:
field = new Type();array = new Type[N];Changes
Other Language Bindings
Following investigation based on user feedback:
__init__requiredconstructor parameters forcing users to provide all valuesSee
LANGUAGE_COMPARISON.mdfor detailed analysis of all language bindings.Testing
All tests pass, including:
Backwards Compatibility
This fix is fully backwards compatible. Code that explicitly initializes nested types continues to work unchanged. The only difference is that code that forgot to initialize will now work instead of throwing
NullPointerException.Impact
This change only affects Java code generation. Python and Dart already handle nested type initialization correctly through different mechanisms and require no changes. Other language bindings (C++, Go, etc.) are unaffected as they don't have the same initialization semantics.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.