Question

NullPointerException in my code...

Asked by: yabelson

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 ?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-11-15 at 11:26:07ID22061985
Tags

nullpointerexception

,

gui

,

intellij

,

idea

Topic

Java Programming Language

Participating Experts
4
Points
125
Comments
18

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

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.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

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.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

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.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. JFrame
    Hi, I created a JFrame with a JButton.If i pressed the JButton I am calling a JInternalFrame . Here My problem is once the JInternalFrame comes ,the JButton in the JFrame is not visible. Is there any method to avoid this.How will solve this problem. Here is My code. JFrame...
  2. Redrawing JFrame
    I have an application written in JB3. At some point of execution, I call invalidate() method of the main window (which in this case extends JFrame) but it doesn't do anything. Why is that? Also: I tried setBackground() method on the same main window. It works fine on Buttons...
  3. JFrame vs. JFrame or JFrame vs JInternalFrame
    hi all.i hope u all can help me with this question. i create a JFrame with menu. when i choose the option from menu, i dunno i should display a new Frame(let say userFrame) using JFrame or JInternalFrame. and the userFrame is not moveable and only can be close when the Close ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

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.

Join the Community

Answers

 

by: objectsPosted on 2006-11-15 at 14:32:45ID: 17951258

>     public void main(String[] args){

should be:

    public static void main(String[] args){

 

by: yabelsonPosted on 2006-11-15 at 20:50:30ID: 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?

 

by: objectsPosted on 2006-11-15 at 20:55:16ID: 17953467

either don't create your gui in main (recomended), or declare all your member vars to be static

 

by: yabelsonPosted on 2006-11-15 at 20:57:36ID: 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?

 

by: saintsairforcePosted on 2006-11-15 at 21:01:23ID: 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

 

by: yabelsonPosted on 2006-11-15 at 21:18:01ID: 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...

 

by: objectsPosted on 2006-11-15 at 21:26:33ID: 17953587

you need to create an instance of that class, and call method on that instance

 

by: yabelsonPosted on 2006-11-15 at 21:29:22ID: 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.

 

by: objectsPosted on 2006-11-15 at 21:46:23ID: 17953665

> JFrame GUIBuilder = new LoopSMSGuiBuilder();

should be:

JFrame GUIBuilder = new LoopSMS();

 

by: yabelsonPosted on 2006-11-15 at 22:38:35ID: 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...

 

by: yabelsonPosted on 2006-11-15 at 22:41:06ID: 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) {
.....
}

       

 

by: objectsPosted on 2006-11-15 at 22:48:18ID: 17953848

it shouldn't look like that. Aren't all those things member vars?  You should be initialising them in the constructor.

 

by: yabelsonPosted on 2006-11-15 at 22:52:26ID: 17953856

this IS the constructor.

 

by: yabelsonPosted on 2006-11-16 at 05:08:11ID: 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 !!

 

by: eoin2000Posted on 2006-11-16 at 06:39:54ID: 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.

 

by: yabelsonPosted on 2006-11-16 at 09:52:25ID: 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.

 

by: yabelsonPosted on 2006-11-16 at 22:25:03ID: 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)

 

by: SamsonChungPosted on 2006-11-17 at 15:02:05ID: 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();
    }
}

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...