Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: setText() NULL pointer excception problem

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default setText() NULL pointer excception problem

    I'm currently trying to implement a GUI ontop of my program which extracts information from a '.dat' file. However, when trying to extract the data from the classes responsible for this extraction a NULL pointer exception is thrown. I know where it is and why it is happening, however I am not sure how to solve it.
    public class GUInterface extends JFrame implements ActionListener
    {
            //creating GUI variables
           JLabel type, loc, size;
     
           public void create()
           {
                guInterface();
           }//end create
     
           public void guInterface()
           {
                //initialising variables
               type = new JLabel("Type");
               loc = new JLabel("Loc");
               size = new JLabel("Size");
     
               add(type);
               add(loc);
               add(size);
           }
     
           public void indexOutput(String output)
           {
                size.setText(output);
           }
    }//end GUInterface

    This is just a small section of the code used to create the GUI (for example, other JPanels and JTextAreas are used), however, I've tried to include all the main components that I believe relate to the problem.
    The method indexOutput is called by another class which returns the file size of the file being read.
    GUInterface gui = new GUInterface();
     
    public void readFile(String fileLocation) throws Exception
    {
                    output = "File Size: " + fileSize + " Bytes";
                    gui.indexOutput(output);
    }

    However, when ran, the size JLabel variable within the indexOutput() method is seen as null, therefore throwing the exception.

    If anyone could offer any adive on this issue it would be appreciated.
    Thanks,
    David.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: setText() NULL pointer excception problem

    The size variable (and loc and type) never get instantiated. You should do so in a constructor, or at least call create or guInterface() on an instance before accessing the variables in readFile

Similar Threads

  1. the infamous null pointer applet problem
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 22nd, 2010, 07:09 PM
  2. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 01:32 PM
  3. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM
  4. NullPointerException:null problem
    By derky in forum Exceptions
    Replies: 8
    Last Post: September 18th, 2009, 03:06 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 01:21 PM