Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用线程调用方法时出现空指针异常_Java_Multithreading_Nullpointerexception - Fatal编程技术网

Java 使用线程调用方法时出现空指针异常

Java 使用线程调用方法时出现空指针异常,java,multithreading,nullpointerexception,Java,Multithreading,Nullpointerexception,在下面的示例中,我试图从run方法内部调用otherClass类的othermethod()方法。我已经创建了另一个类的对象“obj”,并使用它来调用该方法。但它抛出了NullPointerException。 请让我知道为什么是这样 package isAlive; class MyClass { static int ans=0; void counter () throws InterruptedException { Syst

在下面的示例中,我试图从run方法内部调用otherClass类的othermethod()方法。我已经创建了另一个类的对象“obj”,并使用它来调用该方法。但它抛出了NullPointerException。 请让我知道为什么是这样

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());
     }
 }

即使在使用counter()方法时,我也面临着同样的问题。

得到空指针异常的原因是,您从未将值赋给oth,因此从您声明它时起,它仍然是空的

这个

基本上与

otherClass oth = null;

由于它始终为空,因此从对象调用方法会抛出错误。

可能重复“我已将此otherClass的对象创建为obj”不,您没有。没有地方可以调用
new MyClass()
new otherClass()
。哦,我的错..我忘了将对象分配给引用。与oth=新的otherClass配合良好!!很多
otherClass oth;
              ^ no value is being assigned
otherClass oth = null;