yabelson
asked on
NullPointerException in my code...
hi.
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 tion
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Un known Source)
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Unknown Source)
at java.lang.reflect.Method.i nvoke(Unkn own Source)
at com.intellij.rt.execution. applicatio n.AppMain. main(AppMa in.java:90 )
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 erLogo.jpg ");
JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
MainFrame.setIconImage(Poi nterLogo.g etImage()) ;
MainFrame.setLocationRelat iveTo( null );
MainFrame.add(MainFramePan el);
//MainFrame
MainFramePanel.add(DLPanel );
MainFramePanel.add(ULPanel );
// MainFrame Panel
ULPanel.add(DecriptRecieve dPanel);
ULPanel.add(RecievedMessag ePanel);
DecriptRecievedPanel.add(R ecCommandP anel);
RecCommandPanel.add(RecCom mandLabel) ;
RecCommandLabel.add(RecCom mandText);
DecriptRecievedPanel.add(R ecievedUID Panel);
RecievedUIDPanel.add(RecUI DLLabel);
RecievedUIDPanel.add(RecUI DText);
DecriptRecievedPanel.add(R ecievedDat ePanel);
RecievedDatePanel.add(RecD ateLabel);
RecievedDatePanel.add(RecD ateText);
RecievedDatePanel.add(RecT imeLabel) ;
RecievedDatePanel.add(RecT imeText);
RecievedMessagePanel.add(R ecievedMes sageLabel) ;
RecievedMessagePanel.add(R ecievedMes sageText);
//ULPanel
DLPanel.add(DLFramePanel);
DLFramePanel.add(MessageSe ndText);
DLFramePanel.add(Converted MessagePan el);
ConvertedMessagePanel.add( ConvertedL abel);
ConvertedMessagePanel.add( ConvertedM essageText );
DLFramePanel.add(SendButto n);
SendButton.addActionListen er(this);
DLFramePanel.add(Convertio nPanel);
ConvertionPanel.add(Conver tButton) ;
ConvertButton.addActionLis tener(this );
DLFramePanel.add(DateNTime Panel);
DateNTimePanel.add(DateLab el);
DateNTimePanel.add(DateFie ld);
DateNTimePanel.add(TimeLab el);
DateNTimePanel.add(TimeFie ld);
DLFramePanel.add(UIDPanel) ;
UIDPanel.add(CommandComboB ox);
UIDPanel.add(CommandLabel) ;
UIDPanel.add(InsertUIDLabe l);
UIDPanel.add(UnitIDInsertF ield);
// DLPanel
MainFrame.setDefaultCloseO peration(J Frame.EXIT _ON_CLOSE) ;
MainFrame.pack();
MainFrame.setVisible(true) ;
}
can someone guide me to my error or help me fox my code ?
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 ?
ASKER
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?
how cani go around that?
either don't create your gui in main (recomended), or declare all your member vars to be static
ASKER
i changed the Main into static,and (since the compiler demanded) changed the components so that they are static as well.
but now when i am trying to run the app.,i get this error :
non-stsic variable this cannot be referenced from a static context.
now what?
but now when i am trying to run the app.,i get this error :
non-stsic variable this cannot be referenced from a static context.
now what?
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 eOperation (JFrame.EX IT_ON_CLOS E);
}
}
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
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
ASKER
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...
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...
you need to create an instance of that class, and call method on that instance
ASKER
ok, i changed to this :
public class LoopSMS extends JFrame{
...
public static void main(String[] args){
// LoopSMSGuiBuilder.CreateGu i();
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.
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.
> JFrame GUIBuilder = new LoopSMSGuiBuilder();
should be:
JFrame GUIBuilder = new LoopSMS();
should be:
JFrame GUIBuilder = new LoopSMS();
ASKER
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...
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...
ASKER
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 l recievedDatePanel, JPanel convertedMessagePanel, JPanel recievedUIDPanel, JPanel recCommandPanel, JTextPane dateField, JTextPane convertedMessageText,
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) {
.....
}
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) {
.....
}
it shouldn't look like that. Aren't all those things member vars? You should be initialising them in the constructor.
ASKER
this IS the constructor.
ASKER
anyone?
i realy do want to solve this issue without having to rewrite everything :-(
i dont have the ime,i have a deadline in work regarding this application,especially since i already finished all the Background work for it.
please, i need some help to resolve this issue !!
i realy do want to solve this issue without having to rewrite everything :-(
i dont have the ime,i have a deadline in work regarding this application,especially since i already finished all the Background work for it.
please, i need some help to resolve this issue !!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
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 erLogo.jpg ");
JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
MainFrame.setIconImage(Poi nterLogo.g etImage()) ;
MainFrame.setLocationRelat iveTo( null );
// this.getContentPane().add( this.MainF rame);
MainFrame.add(MainFramePan el);
MainFramePanel.add(DLPanel );
MainFramePanel.add(ULPanel );
DLPanel.add(DLFramePanel);
DLFramePanel.add( DateNTimePanel); //5
DLFramePanel.add(UIDPanel) ;
DLFramePanel.add(Convertio nPanel);
ConvertionPanel.add(Conver t);
ULPanel.add(RecievedMessag ePanel);
ULPanel.add(DecriptRecieve dPanel);
DLFramePanel.add(Converted MessagePan el);
DecriptRecievedPanel.add(R ecCommandP anel);
DecriptRecievedPanel.add(R ecievedDat ePanel);
DecriptRecievedPanel.add(R ecievedUID Panel);
ConvertedMessagePanel.add( ConvertedM essageText );
DateNTimePanel.add(DateFie ld);
DateNTimePanel.add(TimeFie ld);
DLFramePanel.add(MessageSe ndText);
RecievedDatePanel.add(RecD ateText);
RecievedDatePanel.add(RecT imeText);
RecievedUIDPanel.add(RecUI DText);
RecCommandPanel.add(RecCom mandText);
RecievedMessagePanel.add(R ecievedMes sageText);
UIDPanel.add(CommandLabel) ;
ConvertedMessagePanel.add( ConvertedL abel);
DateNTimePanel.add(DateLab el);
UIDPanel.add(InsertUIDLabe l);
DateNTimePanel.add(TimeLab el);
RecievedMessagePanel.add(R ecievedMes sageLabel) ;
RecievedDatePanel.add(RecD ateLabel);
RecCommandPanel.add(RecCom mandLabel) ;
RecievedDatePanel.add(RecT imeLabel);
RecievedUIDPanel.add(RecUI DLLabel);
UIDPanel.add(UnitIDInsertF ield);
DLPanel.add(SendButton);
UIDPanel.add(CommandComboB ox);
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 erLogo.jpg ");
JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
MainFrame.setIconImage(Poi nterLogo.g etImage()) ;
MainFrame.setLocationRelat iveTo( null );
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)
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)
Always use main to do one simple thing only (unless you don't want to use oop)
public class blah{
public blah(){
//do something
}
public static void main (String args[]){
new blah();
}
}
public class blah{
public blah(){
//do something
}
public static void main (String args[]){
new blah();
}
}
should be:
public static void main(String[] args){