Library › Coding AI › High school students
Understand a NullPointerException in your Java program
Practice: Asking for a format you can use. About 5 minutes.
About this exercise
Your Java program crashed with a NullPointerException. You want to fix it yourself, so ask for exactly 3 numbered lines labeled 'What crashed:', 'Why it's null:' and 'Check:', each under 25 words. 'What crashed:' names the line number from the stack trace, and 'Check:' tells you one thing to print or look at. No code blocks and no fixed code, under 75 words.
Common mistakes when you ask AI for this
- Getting the fix. The AI's default is a corrected loop, which skips the debugging practice. Try: Say: "No fixed code; I want to fix it myself."
- No labels. Without labels, where it crashed and why get mixed together. Try: Give them: "What crashed:, Why it's null:, Check:".
- A general NPE lecture. The AI explains null in Java in general. Try: Add: "Exactly 3 numbered lines, each under 25 words, about my program."
What a strong prompt includes
- Gives the labels
- Rules out the fix
- Asks for the line number
- Sets the number of lines
- Caps each line
Related exercises
- Adapt one ad image to 3 sizes with Midjourney (Image & Video AI · Marketing and content)
- Add a cron line that matches the others in a crontab (Coding AI · Product managers)
- Add dark mode by overriding your existing CSS variables (Coding AI · Software developers)
- Add input checks to your number guessing game (Coding AI · High school students)
- Add input validation to a Java method with exact messages (Coding AI · Software developers)