-1

I have the following example in which I am trying to call a method othermethod() of class otherClass from inside the run method. I have created an object of this otherClass class as "obj" and using it to call the method . But it is throwing up NullPointerException. Please let me know why is so.

package isAlive;

 class MyClass
{
     static int ans=0; 



     void  counter () throws InterruptedException
     {
         System.out.println("----------------------------");
         synchronized (this){
             System.out.println("----entering>>>>"+this+"  " +this.getClass().getName());

         for (int i=0;i<=10;i++)
             {System.out.println(Thread.currentThread()+"   "+i);
         Thread.sleep(3000);}
         System.out.println("----exit>>>>"+this.getClass().getName()); }
     }


}
 public class staticSync extends Thread
 {
     MyClass obj;
     otherClass oth;
     int num;
     staticSync(int n)
     {
         num=n;
     }
     staticSync(MyClass m,int n)
     {
        obj= m;
        num= n;

     }

     public void run() 
     { 
         try {

        //  obj.counter();
            oth.othermethod();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
     public static void main(String ... as) 
     {try{
        // MyClass m1= new MyClass();
        // MyClass m2= new MyClass();
        staticSync s1= new staticSync(20);
        System.out.println("s1--"+s1);


        //System.out.println("m1="+m1);System.out.println("m2="+m2);
        staticSync s2= new staticSync(15);
        System.out.println("s2--"+s2);


        staticSync s3= new staticSync(15);

        staticSync s4= new staticSync(10);//staticSync s5= new staticSync(m1,10);
        s1.start();
          s2.start();
     }
        catch (Exception e)
        {}
     }

 }


 class otherClass
 {
    public synchronized void  othermethod()
     {
         System.out.println("---------------inside othermethod-..>"+this.getClass().getName());
     }
 }

And the output is :

s1--Thread[Thread-0,5,main]
s2--Thread[Thread-1,5,main]
java.lang.NullPointerException
    at isAlive.staticSync.run(staticSync.java:67)
java.lang.NullPointerException
    at isAlive.staticSync.run(staticSync.java:67)

Even while using the counter() method i am facing the same problem.

Alboz
  • 1,833
  • 20
  • 29
jayendra bhatt
  • 1,337
  • 2
  • 19
  • 41
  • 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) – Alboz Oct 04 '14 at 18:52
  • 2
    "I have created an object of this otherClass class as obj" No you haven't. Nowhere do you invoke `new MyClass()` or `new otherClass()`. – Boann Oct 04 '14 at 18:54

1 Answers1

1

The reason why you're getting a null pointer exception is that you are never assigning a value to oth, and therefore it remains being null from when you declared it.

This

otherClass oth;
              ^ no value is being assigned

Is basically the same as

otherClass oth = null;

Since it is always null, calling a method from the object throws the error.

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44