Java Reference
In-Depth Information
When you try to reference
employee
in line 7, Eclipse displays an error reminding you that the vari-
able
employee
has not been initialized. See Figure 6-7.
figure 6-7
The suggestion offered to initialize the variable will change line 6 to
Person employee = null;
Of course, this will lead to a
null pointer exception;
you can see that just by reading the
code. You can correct it by initializing a new
Person
object referenced by the
employee
variable.
public class ExceptionExamples {
public static void main(String[] args) {
Person employee = new Person();
printPerson(employee);
}
public static void printPerson(Person myPerson){
System.out.println(myPerson.name + " is " + myPerson.age + " years old.");
}
}
class Person{
String name;
Search WWH ::
Custom Search