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

Practice this now ↑

Related exercises