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

Thread: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

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

    Post Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    I get this Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    when i click the button Start, below is it's handler

    Can't understand why...Please help me!!!!!!!!!!!!!!!!!

    private void StartActionPerformed(java.awt.event.ActionEvent evt) {

    int numberOfButtons = a.length+ a2.length+2;
    System.out.println(numberOfButtons);

    jPanel1.setVisible(true);
    jPanel1.setEnabled(true);
    jPanel1.setBackground(Color.LIGHT_GRAY);
    jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));
    for(int i=0; i<numberOfButtons; i++)
    {
    JPanel p[] = new JPanel[numberOfButtons];
    JButton b[] = new JButton[numberOfButtons];
    p[i] = new JPanel();
    b[i] = new JButton();
    b[i].setEnabled(true);
    b[i].setVisible(true);
    b[i].setText("gf");
    b[i].setSize(50, 30);



    jPanel1.setLayout(new FlowLayout(FlowLayout.TRAILING));

    jPanel1.add(b[0]);
    if(i==1){
    b[1].setBounds(30,50,40,50); //1
    jPanel1.setBounds(300, 300, 100, 100);
    jPanel1.add(b[1]);

    b[i].addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
    JDialog dwindow = new JDialog();
    dwindow.setEnabled(true);
    dwindow.setVisible(true);
    dwindow.setSize(200, 120);
    }

    });



    }//end of for

    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    What line is the error on? What is happening on that line? Something must be null there- where do you initialize each of the variables you're dereferencing?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    in this line is the error :jPanel1.add(b[0]);
    i want to put a button into jPanel1 ...when I use it outside the for there is no problem
    Somehow there is a problem in the for loop

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    You still haven't posted the actual error- what is the actual Exception? You also haven't used highlight tags, which makes your code pretty hard to read.

    Hint: What is the value of each element of the array when you're trying to use it? Use some print statements to check your assumptions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    this the error : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    in this line : jPanel1.add(b[0]);
    the line in blue

    start is a button and bellow is it's actionPerformed event

    private void StartActionPerformed(java.awt.event.ActionEvent evt)
    {

    int numberOfButtons = a2;
    System.out.println(numberOfButtons);

    jPanel1.setVisible(true);
    jPanel1.setEnabled(true);
    jPanel1.setBackground(Color.LIGHT_GRAY);
    jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER));

    for(int i=0; i<numberOfButtons; i++)
    {
    JPanel p[] = new JPanel[numberOfButtons];
    JButton b[] = new JButton[numberOfButtons];
    p[i] = new JPanel();
    b[i] = new JButton();
    b[i].setEnabled(true);
    b[i].setVisible(true);
    b[i].setText("gf");
    b[i].setSize(50, 30);

    jPanel1.add(b[0]);


    b[i].addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
    JDialog dwindow = new JDialog();
    dwindow.setEnabled(true);
    dwindow.setVisible(true);
    dwindow.setSize(200, 120);
    }

    });



    }//end of for

    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    Ah, so it's a NullPointerException. Now your job is to figure out what's null in that line. It could either be jPanel1 or b[0], right? So what is the value of each? Are you sure? Step through it with a debugger or add some print statements to test your assumptions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    kathaki88 (October 10th, 2012)

  8. #7
    Junior Member
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    Thanks KevinWorkman!!
    Yes as I checked the b[0] is null. But why?? I create the buttons
    " JButton b[] = new JButton[numberOfButtons];

    b[i] = new JButton();
    b[i].setEnabled(true);
    b[i].setVisible(true);
    b[i].setText("gf");
    b[i].setSize(50, 30);

    "

  9. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    You seem to be declaring your array inside the for loop that is supposed to fill it? Don't do that, but instead declare it *before* the for loop. Don't try to manipulate b[0] from within the for loop either.

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    Quote Originally Posted by curmudgeon View Post
    You seem to be declaring your array inside the for loop that is supposed to fill it? Don't do that, but instead declare it *before* the for loop. Don't try to manipulate b[0] from within the for loop either.
    That's what I was trying to get him to find for himself by stepping through it with a debugger or adding print statements. Ah well.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Trouble with buttonhandler - Exception in thread "AWT-EventQueue-0"

    Oops, sorry, KW!

Similar Threads

  1. Replies: 9
    Last Post: September 1st, 2012, 07:25 AM
  2. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM