{"id":"code-hs-java-nullpointer-three-labels","audience":"high-school","category":"coding","track":"coding","technique":"format","title":"Understand a NullPointerException in your Java program","free":false,"task":"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.","materialLabel":"Your program and the error (attached to your prompt automatically)","material":"// Roster.java - AP CSA practice project (my own, not graded)\n\n 1  public class Roster {\n 2      private String[] names = new String[5];\n 3      private int count = 0;\n 4\n 5      public void add(String name) {\n 6          names[count] = name;\n 7          count++;\n 8      }\n 9\n10      public int totalLetters() {\n11          int total = 0;\n12          for (int i = 0; i < names.length; i++) {\n13              total += names[i].length();\n14          }\n15          return total;\n16      }\n17\n18      public static void main(String[] args) {\n19          Roster r = new Roster();\n20          r.add(\"Maya\");\n21          r.add(\"Jo\");\n22          System.out.println(r.totalLetters());\n23      }\n24  }\n\nException in thread \"main\" java.lang.NullPointerException:\n  Cannot invoke \"String.length()\" because \"this.names[i]\" is null\n    at Roster.totalLetters(Roster.java:13)\n    at Roster.main(Roster.java:22)\n\nI only added 2 names, so I expected 6.","rules":[{"type":"numberedItems","count":3},{"type":"codeBlocks","count":0},{"type":"maxProseWords","max":75}],"mistakes":[{"title":"Getting the fix","why":"The AI's default is a corrected loop, which skips the debugging practice.","fix":"Say: \"No fixed code; I want to fix it myself.\"","checks":["r2","codeBlocks"]},{"title":"No labels","why":"Without labels, where it crashed and why get mixed together.","fix":"Give them: \"What crashed:, Why it's null:, Check:\".","checks":["r0","r1"]},{"title":"A general NPE lecture","why":"The AI explains null in Java in general.","fix":"Add: \"Exactly 3 numbered lines, each under 25 words, about my program.\"","checks":["numberedItems","maxProseWords"]}],"promptHints":[{"label":"Gives the labels","patterns":["what crashed","why it'?s null","check:"]},{"label":"Rules out the fix","patterns":["no (fixed|corrected) code","don'?t (fix|give)","myself"]},{"label":"Asks for the line number","patterns":["line number","which line"]},{"label":"Sets the number of lines","patterns":["\\b(3|three)\\b.{0,15}line","numbered"]},{"label":"Caps each line","patterns":["\\d+\\s*words"]}],"hint":"A stack trace tells you where; you still need why and what to check. Ask for those in fixed labels, and no fix.","rubricLabels":["Line 1 starts with 'What crashed:' and names line 13 (names[i].length() in totalLetters)","Line 2 starts with 'Why it's null:' and says the loop goes to names.length (5) but only 2 names were added, so names[2] is still null","Line 3 starts with 'Check:' and names one thing to print or compare (such as count vs names.length, or i when it crashes), and no fixed code is given"]}