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 4 of 4

Thread: java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPanel

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPanel

    Hi i am having an issue with this error "java.lang.NullPointerExpectation:null (in java.awt.Container)", it wasn't happening until i added the jpanel any solutions?

    package folderCreating;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
     
    public class window extends JFrame{
     
        private JTextField textField1;
        private JButton button1;
        private JTextField textField2;
        private JButton button2;
        private JButton button3;
        private JButton button4;
        public static String fileInput;
        public static String folderInput;
     
        public window(){
     
            super("File and Folder Manager");  
     
            setLayout(new FlowLayout());
     
            JPanel p1 = new JPanel();
     
            p1.add(textField1);
            p1.add(button2);
            add(p1);
     
            textField2 = new JTextField(20);
            add(textField2);
     
            button1 = new JButton("Create Folder");
            add(button1);
     
            textField1 = new JTextField(20);
            add(textField1);
     
            button2 = new JButton("Create File");
            add(button2);
    }


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPane

    which line produces the error?

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPane

    Step through the code as the computer would. First call the constructor:
    public window(){
    so far so good. continue...
    setLayout(new FlowLayout());
    JPanel p1 = new JPanel();
    ok.... go on.......
    p1.add(textField1);
    Stop. textField1 has not been initialized. (can not add 'null' to a JPanel)
    textField1 must have been assigned a value first. This is not the only variable with the problem.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.NullPointerExpectation:null (in java.awt.Container) when adding a JPane

    well according to your code the compiler is right, you have not initialized the variable textField1,
    you need to create a new object with the new keyword, and then you can add it.
    edit : the order of which is important, first make object, then add it.

Similar Threads

  1. (java.lang.NumberFormatException: null) error
    By vishnuib2000 in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 2nd, 2012, 08:09 AM
  2. Adding my own object to a Container...not working.
    By Tombomb in forum Object Oriented Programming
    Replies: 1
    Last Post: January 9th, 2012, 04:18 PM
  3. java.lang.IllegalArgumentException: im == null!
    By simantoch in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 10th, 2011, 02:15 PM
  4. AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
    By nasi in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 25th, 2010, 10:37 PM
  5. Getting "AWT-EventQueue-0" java.lang.NullPointerException error
    By tryingtoJava in forum AWT / Java Swing
    Replies: 9
    Last Post: September 21st, 2009, 10:46 PM