Exceptions


What if you do not supply an initial value for the state field? In the test, you create a new Student object and immediately send it the message isInState. The isInState message results in the equals message being sent to the state field. Find out what happens if you send a message to an uninitialized object. Change the declaration of the state field to comment out the initialization:

 private String state; // = ""; 

Then rerun the tests. JUnit will report an error, not a test failure. An error occurs when neither your production code nor your test accounts for a problem. In this case, the problem is that you are sending a message to an uninitialized field reference. The second panel in JUnit shows the problem.

 java.lang.NullPointerException   at studentinfo.Student.isInState(Student.java:37)   at studentinfo.StudentTest.testInState(StudentTest.java:39)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ... 

This is known as a stack walkback, or stack trace. It provides you with the information on what went wrong, but figuring out the stack trace can take a little bit of detective work. The first line in a stack trace tells you what the problem is. In this case, you got something known as a NullPointerException. A NullPointerException is actually an error object, or exception, that is "thrown" by some underlying, problematic code.

The rest of the lines in a stack trace "walk back" through the message sends leading up to the error. Some of the lines refer to classes and methods that you have coded; other lines refer to Java system library code and third-party library code. The easiest way to decipher stack traces is to read down and find the first line of code that you recognize as being "your" code. Then keep reading lines up to the last line that you recognize. This last recognized line is the entry point into your code; you will probably want to dig down from there.

In the above example, the last line to execute was line 37 in Student.java (the line numbers in your code will likely differ). That code was invoked by the message send in StudentTest line 39. So start with StudentTest line 39 in order to follow the trail leading up to the trouble. That should take you to the following line of code:

 assertFalse(student.isInState()); 

Taking a look at line 37 in Student.java reveals the line of your code that generated the NullPointerException:

 return state.equals(Student.IN_STATE); 

A reference that you do not explicitly initialize has a value of null. The value null represents the unique instance of an object known as the null object. If you send a message to the null object, you receive a NullPointerException. In this line of code, you sent the message equals to the uninitialized state reference, hence the NullPointerException.

In Lesson 6, you will see how to determine if a field is null before attempting to send a message to it. For now, ensure that you initialize String fields to the empty String ("").

Revert your code so that the state field is initialized properly. Rerun your tests.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net