Hi i am trying to build a simple observer class but am having a problem with something that should be realtivley staright foward. In my main method below on complilation the following error is generated and i was wondering if anyone could spot a prolem with the way i have passed the string to the vector Many thanks C:\>java Driver Subject Vector created... New Observer one Created... New Observer two Created... New Observer three Created... Exception in thread "main" java.lang.NullPointerException at Subject.addSubject(Subject.java:15) at Driver.main(Driver.java:17) [ edited to preserve formatting using the [code] and [/code] UBB tags -ds ] [ February 15, 2004: Message edited by: Dirk Schreckmann ]
posted 11 years ago
You never initialize the Vector hector in your class scope.
As you can see in your constructor, your initialized a new object called hector but only for your local scope. And then you try to add element to your global level Vector hector, which was never initialized. Just make your constructor like this: