1Z0-829 Controlling Program Flow Practice Question
Exhibit
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
at com.example.Main.main(Main.java:8)Refer to the exhibit. The exhibit shows a stack trace from a Java application. Which line in the code caused the NullPointerException?
⚠ Common exam trap
Candidates often confuse the line where the exception is thrown (line 8) with the line where the variable is declared (line 12), mistakenly thinking the declaration itself causes the error, but the exception only occurs when the null reference is actually used.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Line 8: int len = s.length();
The NullPointerException occurs when invoking the `length()` method on a null reference. In the stack trace, the exception is thrown at line 8, where `s.length()` is called, but `s` has not been assigned a non-null value (it is null by default if declared as a field, or uninitialized locally). The JVM throws NullPointerException when any instance method is called on a null object reference.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Line 10: System.out.println(len);
Why it's wrong here
The stack trace does not include line 10.
- ✗
Line 12: String s = "hello";
Why it's wrong here
The stack trace points to line 8, not 12.
- ✗
Line 5: int x = 10;
Why it's wrong here
The stack trace shows line 8, not line 5.
- ✓
Line 8: int len = s.length();
Why this is correct
The stack trace explicitly shows line 8.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.