yes, but if i leave the Main function as Void,the compiler asks me to make all the components static,and then i cant run the application.
how cani go around that?
Main Topics
Browse All Topicshi.
i used the GUI Form builder (working with IntelliJ IDEA) i managed to create the GUI i want.
now, i inserted all the elements to the code, but when i run it i get this error :
Exception in thread "main" java.lang.NullPointerExcep
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at com.intellij.rt.execution.
Process finished with exit code 1
my code looks like this :** there is also a constructor for all the components.
public void main(String[] args){
ImageIcon PointerLogo = new ImageIcon("g:\\temp\\Point
JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
MainFrame.setIconImage(Poi
MainFrame.setLocationRelat
MainFrame.add(MainFramePan
//MainFrame
MainFramePanel.add(DLPanel
MainFramePanel.add(ULPanel
// MainFrame Panel
ULPanel.add(DecriptRecieve
ULPanel.add(RecievedMessag
DecriptRecievedPanel.add(R
RecCommandPanel.add(RecCom
RecCommandLabel.add(RecCom
DecriptRecievedPanel.add(R
RecievedUIDPanel.add(RecUI
RecievedUIDPanel.add(RecUI
DecriptRecievedPanel.add(R
RecievedDatePanel.add(RecD
RecievedDatePanel.add(RecD
RecievedDatePanel.add(RecT
RecievedDatePanel.add(RecT
RecievedMessagePanel.add(R
RecievedMessagePanel.add(R
//ULPanel
DLPanel.add(DLFramePanel);
DLFramePanel.add(MessageSe
DLFramePanel.add(Converted
ConvertedMessagePanel.add(
ConvertedMessagePanel.add(
DLFramePanel.add(SendButto
SendButton.addActionListen
DLFramePanel.add(Convertio
ConvertionPanel.add(Conver
ConvertButton.addActionLis
DLFramePanel.add(DateNTime
DateNTimePanel.add(DateLab
DateNTimePanel.add(DateFie
DateNTimePanel.add(TimeLab
DateNTimePanel.add(TimeFie
DLFramePanel.add(UIDPanel)
UIDPanel.add(CommandComboB
UIDPanel.add(CommandLabel)
UIDPanel.add(InsertUIDLabe
UIDPanel.add(UnitIDInsertF
// DLPanel
MainFrame.setDefaultCloseO
MainFrame.pack();
MainFrame.setVisible(true)
}
can someone guide me to my error or help me fox my code ?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You create a class that estends JFrame and make sure it constains the public static void main function in it.
Here is an idea of how to set up your class.
import javax.swing.*;
//import all needed classes
public class yourClass extends JFrame
{
public yourClass()
{
//your code here.
}
public static void main( String args[] )
{
JFrame application = new yourClass();
application.setDefaultClos
}
}
This will allow you to keep your other classes non-static but still use the static void main. With out the static void main it will not work.
Cheers,
Ricky
i changed the components back to private (non-static).
i also changed to this :
public static void main(String[] args){
CreateGui();
}
public void CreateGui(){
but i get a compilation error:
on-static method CreateGui() cannot be referenced from a static context
previousley,the code was like that but even when everything compiled,i got the NullPointerException i started with...
ok, i changed to this :
public class LoopSMS extends JFrame{
...
public static void main(String[] args){
// LoopSMSGuiBuilder.CreateGu
JFrame GUIBuilder = new LoopSMSGuiBuilder();
}
public class LoopSMSGuiBuilder implements ActionListener{
public void CreateGui (){
...
}}}
and STILL i get the same compilation error - cant call non-static method from a static method.
did that, now i am imposed to insert all the Components into LoopSMS (there is a constructor with about 35 components).
but when i try inserting them,the compiler deosnt recognize them...
when it does recognize them,it gives me the old "non-static variable cannot...."
how can i resolve this?
its driving me insane...
oh, LoopSMS constructor looks like this :
public LoopSMS(JComboBox commandComboBox, JPanel mainFramePanel, JPanel ulPanel, JPanel dlPanel,
JPanel dlFramePanel, JPanel dateNTimePanel, JPanel uiDPanel, JPanel convertionPanel,
JPanel recievedMessagePanel, JPanel decriptRecievedPanel,JPane
JTextPane messageSendText, JTextPane timeField, JTextPane recDateText, JTextPane recievedMessageText, JTextPane recTimeText, JTextPane recUIDText, JTextPane recCommandText, JLabel insertUIDLabel, JLabel dateLabel, JLabel timeLabel, JLabel commandLabel, JLabel convertedLabel, JLabel recievedMessageLabel, JLabel recDateLabel, JLabel recTimeLabel, JLabel recUIDLLabel, JLabel recCommandLabel, JTextField unitIDInsertField, JButton convertButton, JButton sendButton) {
.....
}
Yikes....Maybe you should actually start again. It looks like you are just going to have to rethink you're approach...Are you sure this is for "work"...and not "college-work" ;-)
Im gona go right ahead and give you the benefit of the doubt. Anyway, instead of 'fixing your code', i'll just outline a simple framework to get a GUI app up and running. I haven't done it in a while, but its fairly simple.
First, we'll create your entry point into the application (sometimed refferred to as the 'bootstrap').
public class BootStrap
{
public static void main(String args[])
{
new Gui("Loop SMS Ver. 1.01");
}
}
And now your application:
public class Gui extends JFrame
{
private JButton button;
private JPanel panel;
//etc.....
//constructor
public Gui(String frameTitle)
{
super(frameTitle);
//initialise components
init();
//construct GUI
construct();
//Display GUI
display();
}
private void init()
{
button = new Button("My button");
panel = new JPanel("My Panel");
//etc.....
}
private void construct()
{
this.getContextPane().add(
this.getContentPane().add(
//etc...
}
private void display()
{
this.setVisible(true); //not sure if this is correct...cant remember...look it up in API docs
}
}
I've left out a fair bit, like Event handling etc, but this structure will at least work without giving you the 'static context' error.
Hope it helps.
well,im not too good at GUI stuff :-D.
qhat i usuallly do is the background staff... ijust finished writing an app. thats converts some data into 64Bit incription code.
this GUI is to be used by HW guys at my place of work,so its not very complicated,nor is it to be too manageable by them.
only string insertion, convertion button ,send (through a certain protocole) and recieving data.
and yes, its for work ;-).
appreciate the help :-):-)
i'll try rebuilding the GUI from your code example.it will take me some time ( hopefull i'll finish it tonight) and i'll give you a feedback.
y.
ok, now i have things working.
i started from scratch and my code looks like that now :
public LoopSMS(String frameTitle)
{
super(frameTitle);
init();
construct();
display();
}
private void init()
{
Convert = new JButton("Convert Only");
MainFramePanel = new JPanel();
ULPanel =new JPanel();
DLPanel= new JPanel();
DLFramePanel = new JPanel();
DateNTimePanel=new JPanel();
UIDPanel = new JPanel();
ConvertionPanel= new JPanel();
RecievedDatePanel = new JPanel();
RecievedMessagePanel = new JPanel();
DecriptRecievedPanel = new JPanel();
ConvertedMessagePanel = new JPanel();
RecCommandPanel = new JPanel();
RecievedUIDPanel = new JPanel();
ConvertedMessageText =new JTextPane();
DateField = new JTextPane();
MessageSendText = new JTextPane();
TimeField = new JTextPane();
RecDateText = new JTextPane();
RecTimeText = new JTextPane();
RecievedMessageText = new JTextPane();
RecUIDText = new JTextPane();
RecCommandText = new JTextPane();
CommandLabel = new JLabel();
ConvertedLabel = new JLabel();
DateLabel = new JLabel();
InsertUIDLabel = new JLabel();
TimeLabel = new JLabel();
RecievedMessageLabel = new JLabel();
RecDateLabel = new JLabel();
RecCommandLabel = new JLabel();
RecTimeLabel = new JLabel();
RecUIDLLabel = new JLabel();
UnitIDInsertField = new JTextField();
SendButton = new JButton("Convert And Send");
String [] Commands = {"Track", "More Commands Will Be Added"};
CommandComboBox = new JComboBox(Commands);
}
private void construct()
{
ImageIcon PointerLogo = new ImageIcon("g:\\temp\\Point
JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
MainFrame.setIconImage(Poi
MainFrame.setLocationRelat
// this.getContentPane().add(
MainFrame.add(MainFramePan
MainFramePanel.add(DLPanel
MainFramePanel.add(ULPanel
DLPanel.add(DLFramePanel);
DLFramePanel.add( DateNTimePanel); //5
DLFramePanel.add(UIDPanel)
DLFramePanel.add(Convertio
ConvertionPanel.add(Conver
ULPanel.add(RecievedMessag
ULPanel.add(DecriptRecieve
DLFramePanel.add(Converted
DecriptRecievedPanel.add(R
DecriptRecievedPanel.add(R
DecriptRecievedPanel.add(R
ConvertedMessagePanel.add(
DateNTimePanel.add(DateFie
DateNTimePanel.add(TimeFie
DLFramePanel.add(MessageSe
RecievedDatePanel.add(RecD
RecievedDatePanel.add(RecT
RecievedUIDPanel.add(RecUI
RecCommandPanel.add(RecCom
RecievedMessagePanel.add(R
UIDPanel.add(CommandLabel)
ConvertedMessagePanel.add(
DateNTimePanel.add(DateLab
UIDPanel.add(InsertUIDLabe
DateNTimePanel.add(TimeLab
RecievedMessagePanel.add(R
RecievedDatePanel.add(RecD
RecCommandPanel.add(RecCom
RecievedDatePanel.add(RecT
RecievedUIDPanel.add(RecUI
UIDPanel.add(UnitIDInsertF
DLPanel.add(SendButton);
UIDPanel.add(CommandComboB
MainFrame.pack();
}
private void display()
{
this.setVisible(true);
}
two things :
one - for some reason, my Icon part is not working now ( it worked before... :-()
ImageIcon PointerLogo = new ImageIcon("g:\\temp\\Point
JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
MainFrame.setIconImage(Poi
MainFrame.setLocationRelat
the other thing is - how do i bind everything with the form?
i mean, i used a form to arrange the gui the way i like . i binded all the fields in the form to the code, but when i run it , i dont see the structure i was looking for.
how do i do the match ? just a reminder - its IntelliJ Idea (5.1)
Business Accounts
Answer for Membership
by: objectsPosted on 2006-11-15 at 14:32:45ID: 17951258
> public void main(String[] args){
should be:
public static void main(String[] args){