Quantcast
Channel: JetBrains Developer Community : Thread List - IntelliJ IDEA Users
Viewing all articles
Browse latest Browse all 5661

How to make Java singleton from .form?

$
0
0

I need to create a swing GUI using IDEA tool "UI Designer" and make it a Singleton.

I already have a ServerFace.form that is binded to class ServerFace.java I also have a class Index.java that makes the first initialization of ServerFace.

 

See the code of both classes below (there is no code in ServerFace.form):

 

ServerFace.java(class binded to ServerFace.form):

 

import javax.swing.*;publicclassServerFace{    privateJPanel panel1;    privateJButton startServerButton;    privateJButton stopServerButton;    privateJButton clearLogButton;    privateJTextArea textArea1;//Make it Singleton--------------------------------------    privatestaticvolatileServerFace instance;    publicstaticServerFace getInstance(){        if(instance==null){            synchronized(ServerFace.class){                if(instance==null){                    try{                        instance=newServerFace();                    }catch(Exception e){                        System.out.println("failed to create UI: "+e+"  |  "+e.getMessage());                    }                }            }        }        return  instance;    }    privateServerFace()throwsException{    }    privatevoid createUIComponents(){        // TODO: place custom component creation code here    }}

Index.java (class that gets instance of ServerFace.java):

 

import javax.swing.*;publicclassIndex{    privatestaticServerFace _gui;    publicstaticvoid main(String[] args){        _gui =ServerFace.getInstance();    }}

When I try to compile it throws an Exception "failed to create UI: java.lang.NullPointerException | null"

What's wrong and how to get it right?


Viewing all articles
Browse latest Browse all 5661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>