简体   繁体   中英

Exception in Thread(java.nullPointerException)

public class Printer {
    static Printer obj =null;
    private Printer(){}
    public static Printer getInstance(){
        if(obj==null){
            Printer pr=new Printer();
        }
        return obj;  
    }
    void print(String msg){
        System.out.println("["+msg);
        try {
            Thread.sleep(1000);
        } catch(InterruptedException c) {
            c.printStackTrace();
        }
        System.out.println("]");
    }
}

class PrintThread implements Runnable {
    Printer p;
    String msg;
    PrintThread(Printer p,String msg) {
        this.p=p;
        this.msg=msg;
    }
    public void run() {
        p.print(msg);//Getting error in this line
    }
}

//Deploying main class
public class Execution {
    public static void main(String[] args) {
        Printer pr=Printer.getInstance();
    Thread t1=new Thread(new PrintThread(pr,"java"));
    t1.start();
    PrintThread r=new PrintThread(pr,"javadeveloper");
    Thread t2=new Thread(r);
    t2.start();
    }
}

Hi, I have written this program to understand that how the thread works. Here i made Printer class as singleton and tried to implement thread in second class PrintThread by implementing Runnable. Here i overrided the Run(){} method but at the the time of execution the jvm is throwing an error saying that there is a

Exception in "Thread-0"(java.nullPointerException) at PrintThread.run(Printer.java:31).

I tried to google it and also read other related question but still i am not able to rectify the problem

static Printer obj =null;
    private Printer(){}
     public static Printer getInstance(){
         if(obj==null){
             Printer pr=new Printer();
         }
     return obj;  
     }

You create a new Printer object, but return the null value.

Correct:

static Printer obj = null;
private Printer(){}

public static Printer getInstance(){
 if(obj == null){
  obj = new Printer();
 }
 return obj;
}

Correct your getInstance implementation:

public static Printer getInstance() {
    if(obj==null) {
         obj = new Printer(); // don't create local variable
    }
return obj;  
}
Printer pr=new Printer();

更改为

  obj=new Printer();

You are not correctly initializing the Printer variable: Printer.getInstance() always return null , what leads to a NullPointerException . Do this instead:

public static Printer getInstance() {
     if (obj == null) {
         obj = new Printer();
     }
     return obj;
}
Printer pr=new Printer();

this code create new Object of Printer but no use of this and object is have null for his lifetime scope 对象在其生命周期范围内为null

suggested code for this

public static Printer getInstance(){
     if(obj==null){
    obj=new Printer();            // Printer pr=new Printer();  /// here it's wrong
     }
 return obj;  
 }

You need to correctly implement a singleton class. Declare a constructor as a private method. Implement a static method which returns the instance of the same class. Also implement the creation of the instance in thread safe way. like

Printer p= new Printer();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM