Rizze
0
Q:

null pointer exception method

public class MainClass {

    public static void main(String[] args) {
        anotherTest();
        test();
    }
    public static void test() {
        System.out.println("Printed from the test method.");
    }
    public static void anotherTest() {
        System.out.println("Printed from the anotherTest method.");
    }
}
0
// To use randomUUID function. 
import java.util.UUID; 
import java.io.*; 
  
class Singleton 
{ 
    // Initializing values of single and ID to null. 
    private static Singleton single = null; 
    private String ID = null; 
  
    private Singleton() 
    { 
        /* Make it private, in order to prevent the  
           creation of new instances of the Singleton 
           class. */
  
        // Create a random ID 
        ID = UUID.randomUUID().toString(); 
    } 
  
    public static Singleton getInstance() 
    { 
        if (single == null) 
            single = new Singleton(); 
        return single; 
    } 
  
    public String getID() 
    { 
        return this.ID; 
    } 
} 
  
// Driver Code 
public class TestSingleton 
{ 
    public static void main(String[] args) 
    { 
        Singleton s = Singleton.getInstance(); 
        System.out.println(s.getID()); 
    } 
} 
0

New to Communities?

Join the community