Solved

NullPointerException in my code...

Posted on 2006-11-15
18
617 Views
Last Modified: 2008-01-09
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 ?
0
Comment
Question by:yabelson
18 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17951258
>     public void main(String[] args){

should be:

    public static void main(String[] args){
0
 
LVL 1

Author Comment

by:yabelson
ID: 17953450
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
 
LVL 92

Expert Comment

by:objects
ID: 17953467
either don't create your gui in main (recomended), or declare all your member vars to be static
0
 
LVL 1

Author Comment

by:yabelson
ID: 17953475
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
 
LVL 2

Expert Comment

by:saintsairforce
ID: 17953498
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
 
LVL 1

Author Comment

by:yabelson
ID: 17953560
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
 
LVL 92

Expert Comment

by:objects
ID: 17953587
you need to create an instance of that class, and call method on that instance
0
 
LVL 1

Author Comment

by:yabelson
ID: 17953595
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
 
LVL 92

Expert Comment

by:objects
ID: 17953665
> JFrame GUIBuilder = new LoopSMSGuiBuilder();

should be:

JFrame GUIBuilder = new LoopSMS();
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Author Comment

by:yabelson
ID: 17953817
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
 
LVL 1

Author Comment

by:yabelson
ID: 17953825
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
 
LVL 92

Expert Comment

by:objects
ID: 17953848
it shouldn't look like that. Aren't all those things member vars?  You should be initialising them in the constructor.
0
 
LVL 1

Author Comment

by:yabelson
ID: 17953856
this IS the constructor.
0
 
LVL 1

Author Comment

by:yabelson
ID: 17955395
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
 

Accepted Solution

by:
eoin2000 earned 125 total points
ID: 17956035
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
 
LVL 1

Author Comment

by:yabelson
ID: 17958123
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
 
LVL 1

Author Comment

by:yabelson
ID: 17963093
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
 
LVL 6

Expert Comment

by:SamsonChung
ID: 17969310
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars2 challenge 6 68
noX challenge 17 76
mapShare challenge 13 68
Unexpected HttpURLConnection connection behavior 2 50
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now