Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By Tolls

Thread: getting Exception:"AWT-EventQueue-0" java.lang.NullPointerException

  1. #1
    ishant04 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Question getting Exception:"AWT-EventQueue-0" java.lang.NullPointerException

    I am trying to build a calculator for which i am using event handling(ActionListener) and have taken a 2d array for button button but getting Exception::"AWT-EventQueue-0" java.lang.NullPointerException
    Attached Files Attached Files

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

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

    The exception will tell you exactly at which line the NPE occurred.
    Second, post your code not in attachments, but in a [code][/code] block.
    Third, to help you with the NPE, it would be useful to post the actual stacktrace.

    Now, try again :)

  3. #3
    ishant04 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

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

    my code is:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    class Test implements ActionListener
    {
    	Frame f;
    	TextField tf;
    	Button b[][];
    	Button l;
    	Test(String s)
    	{
    		Frame f=new Frame(s);
    
    		TextField tf=new TextField("aba");
    		tf.setBounds(200,150,150,60);
    		f.add(tf);
    	
    		
    		Button b[][]=new Button[6][5];
    		
    			int k=0;
    
    		String s1[]={"MC","MR","MS","M+","M-","<-","CE","C","_+","~","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","~","0","00",".","+","="};
    			
    
    
    		for(int j=0;j<6;j++)
    		{
    			
    		
    			for(int i=0;i<5;i++)
    			{
    			b[j][i]=new Button(s1[k]);
    			b[j][i].setBounds(200+(i*30),220+(j*30),25,15);
    			f.add(b[j][i]);
    			
    			k++;
    			
    			
    			}
    			
    		}
    		
    		
    		b[0][0].addActionListener(this);
    		
    
    		if(b[0][0]==null)
    		{
    		System.out.println("shabahs");
    		}		
    		f.setSize(400,400);
    		f.setLayout(null);
    		f.setVisible(true);
    	
    	}
    	public void actionPerformed(ActionEvent e)
    	{	
    		if(e.getSource()==b[0][0])
    		{
    			String s1=tf.getText();
    			System.out.println(s1);
    		
    		}
    	      
    
    
    
    	}
    	public static void main(String arg[])
    	{
    		
    		new Test("Calculator");
    	}
    
    
    }
    The exact error i am getting is:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Test.actionPerformed(calculator.java:58)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

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

    One thing I notice is that the file is called calculator.java, while it contains a class called Test. I assume the compiler complains that you should have a public class Test in a file called Test.java. Or if your class is supposed to be called Caluculator, it should be in a file called Calculator.java.

    On line 18 you declare Button b[][] but by doing that the field is still null. Remove the type "Button" from the start of the line.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

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

    Also, in this day and age why are you using AWT, which was superseded by Swing more than 10 years ago?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

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

    Quote Originally Posted by DarrylBurke View Post
    Also, in this day and age why are you using AWT, which was superseded by Swing more than 10 years ago?

    db
    I'm going to go with "lecturer/teacher who hasn't updated their course notes for 15 years"...
    ketki likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    ishant04 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

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

    Still getting the same error..
    code after changes:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    class  Calculator1 implements ActionListener
    {
    	Frame f;
    	TextField tf;
    	Button b[][]=new Button[6][5];;
    	Button l;
    	Calculator1(String s)
    	{
    		Frame f=new Frame(s);
    
    		TextField tf=new TextField("aba");
    		tf.setBounds(200,150,150,60);
    		f.add(tf);
    	
    		
    		
    		
    			int k=0;
    
    		String s1[]={"MC","MR","MS","M+","M-","<-","CE","C","_+","~","7","8","9","/","%","4","5","6","*","1/x","1","2","3","-","~","0","00",".","+","="};
    			
    
    
    		for(int j=0;j<6;j++)
    		{
    			
    		
    			for(int i=0;i<5;i++)
    			{
    			b[j][i]=new Button(s1[k]);
    			b[j][i].setBounds(200+(i*30),220+(j*30),25,15);
    			f.add(b[j][i]);
    			
    			k++;
    			
    			
    			}
    			
    		}
    		
    		
    		b[0][0].addActionListener(this);
    		
    
    		if(b[0][0]==null)
    		{
    		System.out.println("shabahs");
    		}		
    		f.setSize(400,400);
    		f.setLayout(null);
    		f.setVisible(true);
    	
    	}
    	public void actionPerformed(ActionEvent e)
    	{	
    		if(e.getSource()==b[0][0])
    		{
    			String s1=tf.getText();
    			System.out.println(s1);
    		
    		}
    	      
    
    
    
    	}
    	public static void main(String arg[])
    	{
    		
    		new Calculator1("Calculator");
    	}
    
    
    }
    Exception getting:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Calculator1.actionPerformed(Calculator1.java:60)
    at java.awt.Button.processActionEvent(Unknown Source)
    at java.awt.Button.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Sour
    ce)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)



    Done all the changes but still getting the same EXCEPTION

  8. #8
    ishant04 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

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

    Error is comming when I click on button

  9. #9
    ishant04 is offline Member
    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

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

    I am just practicing awt and my teachers will teach us swing in comming lectures.they taught us awt as a intro to GUI and thats why i am practicing that.

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    946
    Rep Power
    2

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

    Same type of error, this time for field tf. Look at the stacktrace. It says line 60. Look at line 60. There is only one object on which a method is invoked. That must be null. Looks like tf. Look in the beginning of your code, field tf is declared two times.
    Last edited by SurfMan; 03-28-2013 at 06:22 PM. Reason: Added detailed explanation

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

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

    Quote Originally Posted by ishant04 View Post
    I am just practicing awt and my teachers will teach us swing in comming lectures.they taught us awt as a intro to GUI and thats why i am practicing that.
    Yeah, what Tools said at #6. Sad, that.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 11
    Last Post: 09-24-2012, 03:37 AM
  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

Posting Permissions

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