Results 1 to 4 of 4
  1. #1
    Sujatha90 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Getting "AWT-EventQueue-0" java.lang.NullPointerException

    I am getting this nullpointer exception.
    Its top 2 lines are: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Guiflashcard.actionPerformed(Guiflashcard.java :62)

    There is no problem during compilation and GUI is same as I wanted but problem comes as soon as I click on "Next" button. What can be the possible problem?

    My code is :

    Java Code:
    import java.awt.event.* ;
    import javax.swing.* ;
    import java.awt.* ;
    
    public class Guiflashcard implements ActionListener
    {
      JButton button ;
      JTextArea area1 ;
      JTextArea area2 ;
      JFrame frame ;
      JPanel panel1 ; 
      JPanel panel2 ;
      JPanel panel3 ;
        
     public static void main(String [] args)
     {
    
      Guiflashcard g1 = new Guiflashcard() ;
      g1.go();
    
     }
    
     public void go()
     {
      JLabel label1 = new JLabel("Question", JLabel.CENTER) ;
      JLabel label2 = new JLabel("Answer", JLabel.CENTER) ;
      JTextArea area1 = new JTextArea(10,20) ;
      JTextArea area2 = new JTextArea(10,20) ;
      area1.setLineWrap(true) ;
      area2.setLineWrap(true) ;
     
    
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ; 
    
      JButton button = new JButton("NEXT"); 
      
     
      JPanel panel1 = new JPanel() ;
      JPanel panel2 = new JPanel() ;
      JPanel panel3 = new JPanel() ; 
      panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS)) ;
      panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS)) ;
      panel1.add(label1);
      panel2.add(label2);
      panel1.add(area1);
      panel2.add(area2);
      panel3.add(button);
    
      frame.getContentPane().add(BorderLayout.NORTH, panel1) ;
      frame.getContentPane().add(BorderLayout.CENTER, panel2) ;
      frame.getContentPane().add(BorderLayout.SOUTH, panel3) ; 
      frame.setSize(300,300) ;
      frame.setVisible(true) ;
    
      button.addActionListener(this); 
      
     }
    
      public void actionPerformed(ActionEvent e)
      {
       area1.setText("") ;
       area2.setText("") ;
       area1.requestFocus() ; 
      } 
    
    }
    Last edited by Norm; 01-13-2012 at 08:57 PM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException

    At line 62 in your program it is trying to use a variable with a null value, which causes the exception.
    Look at line 62, find which variable is null and then backtrack in your code to see why that variable does not have a non-null value.

    Check that there are NOT two variables defined with the same name and if one variable is "shadowing" the other.

  3. #3
    Sujatha90 is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException

    Thank you Norm for replying. But very strange thing happened (My code is running fine and is not giving exception anymore ! :)) when I was tinkering with my code on the basis of your suggestion.

    But it left me flabbergasted because changes I made were trivial, according to me. Please check what went right !

    Instance variables I declared in class(Guiflashcard) are :
    JButton button ;
    JTextArea area1 ;
    JTextArea area2 ;
    JFrame frame ;
    JPanel panel1 ;
    JPanel panel2 ;
    JPanel panel3 ;

    I used them in method go() (for same class i.e. Guiflashcard) by declaring as follows(before consulting you) :

    JButton button = new JButton() ;
    JTextArea area1 = new JTextArea() ;
    JTextArea area2 = new JTextArea() ;
    JFrame frame = new JFrame() ;
    JPanel panel1 = new JPanel() ;
    JPanel panel2 = new JPanel() ;
    JPanel panel3 = new JPanel() ;


    but then i changed it to :

    button = new JButton() ;
    area1 = new JTextArea() ;
    area2 = new JTextArea() ;
    frame = new JFrame() ;
    panel1 = new JPanel() ;
    panel2 = new JPanel() ;
    panel3 = new JPanel() ;

    Now, it is running fine.

    My doubt is that what I was doing earlier was duplication of variables i.e. using them as instance variables as well as local variables?

    Thank you for the help.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Getting "AWT-EventQueue-0" java.lang.NullPointerException

    what I was doing earlier was duplication of variables i.e. using them as instance variables as well as local variables
    Yes that was the problem.

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2011, 07:26 PM
  2. Replies: 1
    Last Post: 12-13-2010, 03:19 PM
  3. Replies: 0
    Last Post: 05-28-2010, 02:47 PM
  4. Replies: 3
    Last Post: 04-30-2010, 07:44 PM
  5. Replies: 5
    Last Post: 08-26-2009, 11:37 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
  •