3

I am actually new to Design Pattern concepts and am trying to implement the Observer Pattern.

I have a Blog class which notifies observers of new changes.It implemets Subject interface. It has a registerObserver method for adding new observers. On the other hand, I have classes for different kinds of observers which all implement the Observer interface.

I want to have a Register method and a Unsubscribe method in observer classes so that they can choose when to be added and removed. However, when I use my code that I have written here, I get a Null Pointer Exception error in runtime which is apparently because of the line blog.registerObserver(this).

What other options do I have in order to implement Register and Unsubscribe methods?

public void registerObserver( Observer o) //when an observer resgiters we add 
                                          // it to the end of the list
{
    observers.add(o);
}

Observer is an interface and client classes implement it. Now I have a class of ClientForMusic:

public class ClientForMusic implements Observer, DisplayElement {

private String Music;
private Subject blog;
public ClientForMusic()
{}

public void Register (Subject Blog)
{
   this.blog=blog;
   blog.registerObserver(this);  
}

public void Unsubscribe(Subject Blog)
{
    this.blog=blog;
    blog.removeObserver(this);
}


public void update(String music, String movie, String news, String science )
{
    this.Music= music;
    display();
}

public void display()
{
     System.out.println("I have been notified of a new song:" + Music);
}
}
Pegah.A
  • 85
  • 2
  • 9
  • 1
    typo: `public void Register (Subject Blog)` should be `public void Register (Subject blog)`, same bug in `Unsubscribe` – wero Feb 11 '16 at 16:41
  • Is your "observers" variable initialized or is it null? – BitExodus Feb 11 '16 at 16:41
  • @wero: sorry I made the mistake here while pasting and removing unnecessary comments. – Pegah.A Feb 11 '16 at 16:49
  • 1
    Your problem is not with `this`, it's that `blog` is `null`. You can't register against a `null`. – Erick G. Hagstrom Feb 11 '16 at 16:56
  • Pegah.A : post exact code. Current code did not set blog and you will get null pointer exception since you are not using right parameter in setter method. – Ravindra babu Feb 11 '16 at 16:59
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – andrucz Feb 16 '16 at 13:28

2 Answers2

0

It seems strange that your methods register and unsubscribe receive blog as a parameter. You either need to instantiate your blog at constructor of the Observer or have a setter method in your Observer that takes your blog. Or at least if you want to pass it through method register then keep it. Method unsubscribe should be using the same instance of blog that was used to register there. NPE probably occurs if you pass null value to your register or unsubscribe method. Otherwise your code looks OK

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

Your problem can be one of two:

1) Your observers variable is not initialized, so calling:

observers.add(o)

is throwing the exception. You need to initialize your "observers" variable, for example:

observers = new ArrayList()

2) Your variable "blog" in your "blog.registerObserver(this)" call is null in which case you should initialize your blog variable.

BitExodus
  • 749
  • 6
  • 10