Results 1 to 6 of 6
  1. #1
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Question null pointer exception question

    I am trying to run the following code, which I believe is fairly straightforward, but it does not work.... It compiles, and it will run, but none ofn the objects are appearing in the window that opens, and when I close the window, and look at the command prompt, I see a bunch of complaints about null pointers that I do not understand??
    Java Code:
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class tutorial1 extends JFrame{ //1
      private JPanel jp;  //2
      private JButton b1; //3
      private JTextField txtfld; //4
      private JTextArea jt = new JTextArea(8,8); //5
        public tutorial1 () { //6
        setTitle("title"); //7
        setVisible(true); //8
        setSize(480,480); //9
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //10
        b1 = new JButton("Exit"); //11
        jp.add(b1); //12
        jp.add(jt); //13
        add(jp); //14
        event e = new event(); //15
        b1.addActionListener(e); //16 
        } //17
        public class event implements ActionListener{ //18
          public void actionPerformed(ActionEvent e){ //19
            System.exit(0); //20
           }} //21
      public static void main(String[] args){ //22
        tutorial1 t1 = new tutorial1(); //23
        t1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 24
        t1.setTitle("title"); //25
        t1.setSize(480,480); //26
        t1.setVisible(true); //27
        } //28
    } //29
    When I run this, it compiles fine, and runs, but the components of the interface do not show up. No panel, no textfield, no buttons, just a blank frame with a title, and the compiler kicks back this error:
    Exception in thread "main" java.lang.NullPointerException
    at tutorial1.<init><tutorial1.java:15>
    at tutorial1.main<tutorial1.java:26>

    Can anyone PLEASE tell me why this won't work?? I know it is something simple and stupid that I have missed, but I am stuck right here, and cannot continue!!
    HELP!!
    Last edited by zlloyd1; 01-26-2013 at 05:15 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: null pointer exception question

    The key is to check the line that causes the NPE to be thrown. So please tell us, which line is the exception telling you that there is a problem?

  3. #3
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: null pointer exception question

    What do you see that is wrong with line 15?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: null pointer exception question

    OK, so line 15 is this?

    jp.add(b1);

    If so, you will want to check to see that *all* the variables used on this line have been initialized. There are only two variables, so this should be easy for you to check (and solve!).

  5. #5
    zlloyd1 is offline Member
    Join Date
    Nov 2012
    Posts
    57
    Rep Power
    0

    Default Re: null pointer exception question

    I have checked line 15, and I could not find any problems with initializing the button. I also checked line 26, but could not figure what would cause that to be a nullpointerexception issue.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: null pointer exception question

    Quote Originally Posted by zlloyd1 View Post
    I have checked line 15, and I could not find any problems with initializing the button. I also checked line 26, but could not figure what would cause that to be a nullpointerexception issue.
    The button is not the variable that is dereferenced on that line. There's another variable on that line that you call a method on. Have you checked it? That's why I used the term *all* in my post above.

Similar Threads

  1. Null pointer Exception
    By aortell24 in forum New To Java
    Replies: 5
    Last Post: 07-21-2012, 12:38 AM
  2. null pointer exception
    By Herah in forum New To Java
    Replies: 1
    Last Post: 12-01-2011, 09:44 AM
  3. Null pointer exception
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 07-11-2011, 12:16 AM
  4. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 11:21 AM
  5. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 04:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •