So I think line# 2 must changed to private Halter myHalter = new Halter();
I believe that you are right. Because the variable myHalter is defined within the scope of a class, it defaults to null. Trying to dereference myHalter with the expression...
...is trying to dereference a null object. The exception that is thrown is actually a good thing. In older languages (such as Pascal, C, and C++) if you tried to use an object reference variable that was not initialized, the value would be whatever is left on the stack and you would get indeterminate results.
The nullPointerException here in Java saves you time because it tells you exactly which line of code is the problem.
So, why doesn't the compiler catch the problem?
click here [ January 11, 2008: Message edited by: Kaydell Leavitt ]
The compiler never finds whether an object has been instantiated or not. That is beyond it. The idea behind the NullPointerException is that it crashes the application rather than allow it to produce undefined or unpredictable results. Even though there are cases which don't follow this rule (eg NumberFormatException) most unchecked Exceptions represent a mistake in the code. Look at the Java Tutorial.
What you need to do is to instantiate the object, which I think is best done in a constructor. Whenever you declare a field, you go to the constructor and make sure to instantiate that field there.
Joined: Nov 05, 2007
Thank You very much.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com