Wednesday, July 28, 2010


Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 9, Question 16

This code compiles. The JVM gives the following runtime error:

Exception in thread "Thread-0" Exception in thread "Thread-1" java.lang.NullPointerException at at Source)java.lang.NullPointerException at at Source)

This was discussed on Coderanch

Two options to fix the runtime error
1. Automatically assign each instance of Chicks a value, i.e. replace line 10 with

Chicks c = new Chicks();

and remove line 15.

2. Make Chicks static, i.e. replace line 10 with

static Chicks c;

Line 15 will point c to a new instance of Chicks, i.e.

c = new Chicks();

No comments:

Post a Comment