0
public class Chicks {
    synchronized void yacks(long id)
    {
        for(int x = 1; x<3; x++)
        {
            System.out.println(id + " ");
            Thread.yield();
        }
    }
}
class ChickYacks implements Runnable
{
    Chicks c; // No exception if I declare it as static
    public static void main(String[] args) {
        new ChickYacks().go();
    }
    public void run()
    {
        c.yacks(Thread.currentThread().getId()); //Throws a Nullpointer exceptin
    }
    void go()
    {
        c = new Chicks();
        new Thread(new ChickYacks()).start();
        new Thread(new ChickYacks()).start();
    }
}

Why does it throw a Nullpointer exception in run method(). Everything looks fine to me. When I declare Chicks 'c' as static but I am not understanding it why?

  • 1
    Note that this has nothing to do with multithreading. You're just creating objects, forgetting to assign a non-null value to a field of these objects, and then calling a method on this null field. – JB Nizet May 08 '15 at 07:08

1 Answers1

6

Your go method assigns a non-null value to "this" instance of ChickYacks - but then creates two new instances of ChickYacks, each of which will have a null value for c.

You could:

  • Initialize c in a constructor instead, so that each instance has a non-null value
  • Initialize c in a field initializer
  • Initialize c in the run() method
  • Initialize c for the new instances in the go method
  • Pass this to the Thread constructor instead of creating new instances
  • Make c static so that it doesn't matter which instance you access it from (or indeed whether you access it in a static method instead; it would be associated with the type rather than with instances)

This has nothing to do with threading, really. You'd get the same sort of effect if you didn't use threads, just with the exception being thrown in the original thread instead of new threads:

void go()
{
    c = new Chicks();
    new ChickYacks().run(); // Bang!
    new ChickYacks().run();
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • wow! John Skeet answering my question. It must be 100% right :) –  May 08 '15 at 07:09
  • Besides, I like that you have answered my question but I still didn't understand it :/ "non-null value to "this" instance of ChickYacks " - what it means? –  May 08 '15 at 07:15
  • @kvp: Well which bit don't you understand? I've expanded my answer. (For the second time - dodgy network connection ate the first expansion...) – Jon Skeet May 08 '15 at 07:15
  • Initializing it in this case mean `Chicks c = new Chicks();` right? so just because `c' object is null it is throwing `NullPointerException`? –  May 08 '15 at 07:23
  • @kvp: Yes - `c` is `null` in each of the instances that `run()` is being called in, so when you use `c.yacks(...)` that throws an exception. – Jon Skeet May 08 '15 at 07:24
  • 2
    @kvp: No, because you're initializing it in the wrong instance. Do you understand how instance fields work? When you create a new instance, that has a separate set of fields... – Jon Skeet May 08 '15 at 07:34
  • Right! understood now. Thanks Jon Skeet :) –  May 08 '15 at 09:26