Link to home
Start Free TrialLog in
Avatar of yabelson
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.NullPointerException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.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\\PointerLogo.jpg");
       
        JFrame MainFrame = new JFrame("Loop SMS    Ver. 1.01");
        MainFrame.setIconImage(PointerLogo.getImage());
        MainFrame.setLocationRelativeTo( null );
        MainFrame.add(MainFramePanel);
        //MainFrame
        MainFramePanel.add(DLPanel);
        MainFramePanel.add(ULPanel);
        // MainFrame Panel

        ULPanel.add(DecriptRecievedPanel);
        ULPanel.add(RecievedMessagePanel);

        DecriptRecievedPanel.add(RecCommandPanel);
        RecCommandPanel.add(RecCommandLabel);
        RecCommandLabel.add(RecCommandText);
        DecriptRecievedPanel.add(RecievedUIDPanel);
        RecievedUIDPanel.add(RecUIDLLabel);
        RecievedUIDPanel.add(RecUIDText);
        DecriptRecievedPanel.add(RecievedDatePanel);
        RecievedDatePanel.add(RecDateLabel);
        RecievedDatePanel.add(RecDateText);
        RecievedDatePanel.add(RecTimeLabel) ;
        RecievedDatePanel.add(RecTimeText);
        RecievedMessagePanel.add(RecievedMessageLabel);
        RecievedMessagePanel.add(RecievedMessageText);
        //ULPanel

        DLPanel.add(DLFramePanel);
        DLFramePanel.add(MessageSendText);
        DLFramePanel.add(ConvertedMessagePanel);
        ConvertedMessagePanel.add(ConvertedLabel);
        ConvertedMessagePanel.add(ConvertedMessageText);
        DLFramePanel.add(SendButton);
        SendButton.addActionListener(this);
        DLFramePanel.add(ConvertionPanel);
        ConvertionPanel.add(ConvertButton) ;
        ConvertButton.addActionListener(this);
        DLFramePanel.add(DateNTimePanel);
        DateNTimePanel.add(DateLabel);
        DateNTimePanel.add(DateField);
        DateNTimePanel.add(TimeLabel);
        DateNTimePanel.add(TimeField);
        DLFramePanel.add(UIDPanel);
        UIDPanel.add(CommandComboBox);
        UIDPanel.add(CommandLabel);
        UIDPanel.add(InsertUIDLabel);
        UIDPanel.add(UnitIDInsertField);
        // DLPanel
        MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        MainFrame.pack();
        MainFrame.setVisible(true);
    }


can someone guide me to my error or help me fox my code ?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>     public void main(String[] args){

should be:

    public static void main(String[] args){
Avatar of yabelson
yabelson

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?
either don't create your gui in main (recomended), or declare all your member vars to be static
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?
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

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...
you need to create an instance of that class, and call method on that instance
ok, i changed to this :

public class LoopSMS extends JFrame{
...
   public static void main(String[] args){
       // LoopSMSGuiBuilder.CreateGui();
        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();
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,JPanel 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) {
.....
}

       
it shouldn't look like that. Aren't all those things member vars?  You should be initialising them in the constructor.
this IS the constructor.
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 !!
ASKER CERTIFIED SOLUTION
Avatar of eoin2000
eoin2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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\\PointerLogo.jpg");

        JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
        MainFrame.setIconImage(PointerLogo.getImage());
        MainFrame.setLocationRelativeTo( null );
       // this.getContentPane().add(this.MainFrame);
        MainFrame.add(MainFramePanel);
        MainFramePanel.add(DLPanel);
        MainFramePanel.add(ULPanel);
        DLPanel.add(DLFramePanel);
        DLFramePanel.add( DateNTimePanel);        //5
        DLFramePanel.add(UIDPanel);
        DLFramePanel.add(ConvertionPanel);
        ConvertionPanel.add(Convert);
        ULPanel.add(RecievedMessagePanel);
        ULPanel.add(DecriptRecievedPanel);
        DLFramePanel.add(ConvertedMessagePanel);
        DecriptRecievedPanel.add(RecCommandPanel);
        DecriptRecievedPanel.add(RecievedDatePanel);
        DecriptRecievedPanel.add(RecievedUIDPanel);
        ConvertedMessagePanel.add(ConvertedMessageText);
        DateNTimePanel.add(DateField);
        DateNTimePanel.add(TimeField);
        DLFramePanel.add(MessageSendText);
        RecievedDatePanel.add(RecDateText);
        RecievedDatePanel.add(RecTimeText);
        RecievedUIDPanel.add(RecUIDText);
        RecCommandPanel.add(RecCommandText);
        RecievedMessagePanel.add(RecievedMessageText);
        UIDPanel.add(CommandLabel);
        ConvertedMessagePanel.add(ConvertedLabel);
        DateNTimePanel.add(DateLabel);
        UIDPanel.add(InsertUIDLabel);
        DateNTimePanel.add(TimeLabel);
        RecievedMessagePanel.add(RecievedMessageLabel);
        RecievedDatePanel.add(RecDateLabel);
        RecCommandPanel.add(RecCommandLabel);
        RecievedDatePanel.add(RecTimeLabel);
        RecievedUIDPanel.add(RecUIDLLabel);
        UIDPanel.add(UnitIDInsertField);
        DLPanel.add(SendButton);
        UIDPanel.add(CommandComboBox);
         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\\PointerLogo.jpg");

        JFrame MainFrame = new JFrame("Loop SMS Ver. 1.01");
        MainFrame.setIconImage(PointerLogo.getImage());
        MainFrame.setLocationRelativeTo( 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)
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();
    }
}