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 ?
LVL 1
yabelsonAsked:
Who is Participating?
 
eoin2000Connect With a Mentor Commented:
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.button);
        this.getContentPane().add(this.panel);
        //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.
0
 
objectsCommented:
>     public void main(String[] args){

should be:

    public static void main(String[] args){
0
 
yabelsonAuthor Commented:
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?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
objectsCommented:
either don't create your gui in main (recomended), or declare all your member vars to be static
0
 
yabelsonAuthor Commented:
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?
0
 
saintsairforceCommented:
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
0
 
yabelsonAuthor Commented:
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...
0
 
objectsCommented:
you need to create an instance of that class, and call method on that instance
0
 
yabelsonAuthor Commented:
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.
0
 
objectsCommented:
> JFrame GUIBuilder = new LoopSMSGuiBuilder();

should be:

JFrame GUIBuilder = new LoopSMS();
0
 
yabelsonAuthor Commented:
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...
0
 
yabelsonAuthor Commented:
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) {
.....
}

       
0
 
objectsCommented:
it shouldn't look like that. Aren't all those things member vars?  You should be initialising them in the constructor.
0
 
yabelsonAuthor Commented:
this IS the constructor.
0
 
yabelsonAuthor Commented:
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 !!
0
 
yabelsonAuthor Commented:
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.
0
 
yabelsonAuthor Commented:
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)
0
 
SamsonChungCommented:
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();
    }
}
0
All Courses

From novice to tech pro — start learning today.