20 of the best prompts for handling null values in code, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
20 of the best prompts for handling null values in code, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
Published June 28, 2026
Null or undefined values in generated code cause runtime crashes, disrupting workflow. These crashes can lead to significant downtime and debugging efforts. These prompts guide developers to diagnose, fix, verify, and prevent null value issues effectively. Built across 4 distinct stages covering Diagnose the Wall, Isolate and Fix, Verify the Resolution and more, this guide gives you one expert prompt per step so you never have to write from scratch or guess what the AI needs. The prompts work in ChatGPT, Claude, and Gemini and are designed to get usable output on the first try.
Identify where null or undefined values are causing issues in the code.
Identify Problematic Code
Please scan the project code for any instances where null or undefined values may not be handled properly. Identify and list all potential problem areas, including the specific lines of code and a brief description of the issue for each instance. Additionally, suggest best practices or modifications that could be implemented to address these issues and improve the overall robustness of the code. Format the output as a detailed report that includes the code snippets and recommendations.
Check Error Logs
Analyze the error logs from the application to identify instances where null values are causing runtime errors. Provide a detailed report that includes the specific files and line numbers where these null values occur, along with a brief description of the error associated with each instance. Additionally, suggest potential solutions or checks that could be implemented to prevent these null-related errors in the future. Format the report in a clear and organized manner for easy reference.
Review Recent Changes
Please review the recent changes made in the codebase and identify any modifications that may have introduced null value issues. Summarize your findings in a structured format, including specific files or functions affected, the nature of the changes, and any potential risks associated with null values. Additionally, suggest possible solutions or best practices to mitigate these issues moving forward. Your summary should be clear and concise, ideally fitting within one page.
Analyze Function Returns
Please analyze the following functions in my codebase for their handling of null or undefined return values. For each function, identify whether it properly checks for these cases and highlight any that do not. Provide specific recommendations for how to implement appropriate null checks or error handling where necessary. Additionally, summarize the potential impact of not addressing these issues on the overall functionality and reliability of the application.
Inspect Variable Declarations
Inspect the variable declarations in the codebase for proper initialization. Identify any variables that may lead to null reference issues and provide a detailed report on their locations and potential impacts. Include suggestions for how to properly initialize these variables to prevent null-related errors. Ensure the report is structured with sections for each variable, including its type, current initialization status, and recommended fixes.
Focus on correcting the identified null value issues in the code.
Add Null Checks
Please review the following code for potential runtime issues due to null values: [INSERT CODE SNIPPETS]. Identify all areas where null checks are necessary to prevent crashes. For each identified area, provide the updated code snippets with the appropriate null checks added. Additionally, explain briefly why each null check is important and how it enhances the stability of the code.
Refactor Problematic Functions
Refactor the following functions to ensure they handle null or undefined values gracefully. The functions are: [FUNCTION_1], [FUNCTION_2], and [FUNCTION_3]. For each function, provide the original code and the refactored version, clearly indicating the changes made to improve error handling. Additionally, include comments explaining the rationale behind each change and how it enhances the function's robustness.
Update Variable Initializations
Review the code provided and ensure that all variables are properly initialized to avoid null values. For each variable, identify its original initialization state and provide a corrected version that includes a default value or appropriate initialization. After making the necessary changes, display a summary of the modifications made, highlighting the variables that were updated and the reasons for their new initializations.
Implement Default Values
Please review the following function definitions and introduce default values for any parameters that might be null. For each function, provide the updated definitions and explain the rationale behind the chosen default values. Ensure that the changes maintain the intended functionality of the functions while preventing potential null reference errors. Include examples of how the updated functions can be called with and without the parameters to demonstrate the default values in action.
Enhance Error Handling
Please enhance the error handling mechanisms in the following code to effectively catch and manage null value exceptions. I am working with a codebase that includes functions for [FUNCTION NAME] and [ANOTHER FUNCTION NAME]. Provide specific examples of the revised code, including the implementation of try-catch blocks, custom error messages, and any necessary checks for null values. Additionally, explain how these changes improve the robustness of the application and prevent potential runtime errors.
Confirm that the null value issues have been resolved and no longer cause runtime errors.
Run Test Suite
Please execute the full test suite for the project and ensure that all tests pass without any failures related to null value issues. Provide a detailed report of the test results, including the number of tests run, the number of tests that passed, and any tests that failed along with the reasons for their failure. Additionally, summarize any specific areas of the code that may require attention to prevent null value issues in the future.
Check Application Logs
Please review the application logs after the recent fix to ensure that null value errors are no longer present. Summarize your findings in a report that includes the total number of log entries reviewed, any remaining errors related to null values, and any other relevant issues that may have surfaced. Additionally, provide insights on the overall health of the application based on the log data, and suggest any further actions if necessary. Format the report in a clear and concise manner, highlighting key points for easy reference.
Perform Manual Testing
Please conduct manual testing on the affected features of [APPLICATION NAME] to verify that they function correctly without any null-related crashes. Focus on the following areas: [LIST SPECIFIC FEATURES OR FUNCTIONS TO TEST]. For each feature, document any issues encountered, the steps taken during testing, and the outcomes. Additionally, provide a summary that includes the overall status of the testing, any critical bugs found, and recommendations for further actions if necessary.
Validate Function Outputs
Please validate the outputs of the following functions: [FUNCTION NAMES]. Ensure that each function returns the expected values for a variety of inputs, including edge cases and null values. For each function, provide a summary of the validation results, indicating whether the function handled null inputs correctly and if the outputs matched the expected results. Format the results in a clear table, listing the function name, input tested, expected output, actual output, and a pass/fail status for each test case.
Confirm Variable States
Please analyze the runtime state of the following variables: [VARIABLE NAMES]. Confirm that none of these variables are null at critical points in the code execution. Provide a detailed report that includes the values of these variables at key checkpoints, any instances where a variable is null when it shouldn't be, and suggestions for implementing null checks to prevent potential errors. Additionally, outline the impact of these null states on the overall functionality of the program.
Create mechanisms to prevent future null value issues in the codebase.
Create CLAUDE.md Rule
Please create a new rule for CLAUDE.md that enforces null checks in all functions. The rule should specify the conditions under which null checks must be implemented and provide clear examples of correct and incorrect implementations. Additionally, include guidelines on how to document these checks within the codebase. Format the rule text in a way that can be easily added to the existing documentation, ensuring it is concise yet comprehensive.
Add Code Comments
Please insert comments in the following code to remind developers to handle null values appropriately. The comments should clearly indicate where null checks should be implemented and provide examples of how to perform these checks. Additionally, include explanations for why handling null values is important in each context. Ensure that the comments are concise yet informative, and format them in a way that is easy for developers to understand and follow.
Develop Checklist
Create a comprehensive checklist for developers to utilize during code reviews, specifically aimed at identifying and addressing null value issues in the code. The checklist should include key points such as: verifying input parameters for null values, ensuring proper initialization of variables, checking return values from functions for null, and implementing appropriate null checks in critical sections of the code. Additionally, include best practices for documenting potential null value scenarios and strategies for testing edge cases. Format the checklist in bullet points for easy reference.
Establish Naming Conventions
Define a set of naming conventions that clearly indicate when a variable or function might return null. Include specific examples of how to name variables and functions to reflect their potential nullability, such as using prefixes or suffixes. Explain the rationale behind each convention and how it can help developers understand the code better. Additionally, provide guidelines on how to implement these conventions consistently across a codebase to prevent null-related issues in the future.
Implement Guard Clauses
Please provide a detailed explanation of guard clauses and their importance in preventing null values from causing issues in critical functions. Include specific examples of how to implement guard clauses in code, demonstrating their placement and functionality. Additionally, explain the potential problems that can arise when null values are not properly handled and how guard clauses can mitigate these risks. Format the response with code snippets and a brief summary of best practices for using guard clauses effectively.
Claude Code can be guided to handle null values by setting specific rules in the CLAUDE.md file. This ensures that it checks for nulls during code generation and modification.
Claude Code may miss null checks if the CLAUDE.md file lacks explicit rules for handling nulls. Adding these rules can improve its code handling capabilities.
Yes, Claude Code can automatically fix null issues if prompted correctly. Use the provided prompts to guide it through diagnosing and fixing these issues.
Include rules that enforce null checks in function definitions and variable initializations. This helps Claude Code to automatically apply these checks.
Run the test suite and review logs to ensure that Claude Code's fixes have resolved null value issues. Manual testing can also confirm the resolution.
AI Prompts for Adding Null Checks to Prevent Crashes
The generated code is failing to handle null or undefined values, leading to runtime crashes.
See promptsAI Prompts for Adding Null Checks to Generated Code
Generated code that does not handle null or undefined values can lead to runtime crashes, disrupting development.
See promptsAI Prompts for Handling Null Values in Code
Cursor-generated code is failing due to unhandled null or undefined values, causing runtime crashes.
See prompts