0
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

6 Answers 6

5
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;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Correct your getInstance implementation:

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

Comments

3
Printer pr=new Printer();

change this to

  obj=new Printer();

Comments

3

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

Comments

1
Printer pr=new Printer();

this code create new Object of Printer but no use of this and obj object is have null for his lifetime scope

suggested code for this

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.