Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple vector prob

 
Jon omara
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Adrian Yan
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic