Results 1 to 9 of 9
  1. #1
    YouGina is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default NullPointerException by ActionListener

    Hello everybody,

    At the moment I am trying to build an application that's using lot's of different classes. I am trying to create event handlers to handle the events from the different classes. Because the source is some kind of big, i have made a sort of simulation, with the same problem.

    When you click the Ok button, there will be a NullPointerException. Can anybody tell me how I can solve this without rebuilding the whole source?

    Here is the source of the `simulation`:
    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JToolBar;
    import java.awt.LayoutManager;
    
    public class TijdsRegistratie extends JFrame {   
        public static void main(String[] args) {  	
        	JFrame frame = new TijdsRegistratie();
        	frame.setSize(500, 500);
        	frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        	frame.setTitle("Tijds Registratie");
        	frame.setContentPane(new RegistratiePanel());
        	frame.setVisible(true);
        }
    }
    
    class RegistratiePanel extends JPanel {
    	private JToolBar bar;
    	private JButton Ok, Cancel, Start, Stop;
    	public JTextField tijdStart, tijdEind, Voor, Van;
    	
    	public RegistratiePanel() {
    		
    		setLayout(null);
    		
    		Ok = new JButton("Ok");
    		Cancel = new JButton("Cancel");
    		Start = new JButton("Start");
    		Stop = new JButton("Stop");
    		
    		Ok.addActionListener(new okHandler());
    		
    		bar = new JToolBar();
    		
    		bar.add(Ok);
    		bar.add(Cancel);
    		bar.add(Start);
    		bar.add(Stop);
    		
    		add(bar);		
    		bar.setFloatable(false);
    		bar.setRollover(true);
    		bar.setBounds(0, 0, 500, 30);
    		
    		tijdStart = new JTextField(10);
    		tijdEind = new JTextField(10);
    		Voor = new JTextField(10);
    		Van = new JTextField(10);
    		
    		tijdStart.setBounds(100, 50, 300, 20);
    		tijdEind.setBounds(100, 150, 300, 20);
    		Voor.setBounds(100, 250, 300, 20);
    		Van.setBounds(100, 350, 300, 20);
    		
    		add(tijdStart);
    		add(tijdEind);
    		add(Voor);
    		add(Van);
    					
    	}
    }
    class okHandler implements ActionListener {
    	RegistratiePanel reg;
    	public void actionPerformed(ActionEvent e) {
    		reg.tijdStart.setText("Er is op Ok gedrukt");
    	}
    }
    Thanks in advance.

    P.S. Sorry for my bad English, and non-English variables.
    Last edited by YouGina; 07-09-2009 at 12:12 PM.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Hi,
    First u should put what ur code is going to do in comments.Then only,it will be helpful for debugging.

    class okHandler implements ActionListener {
    RegistratiePanel reg;
    public void actionPerformed(ActionEvent e) {
    reg.tijdStart.setText("Er is op Ok gedrukt");
    }
    }

    In the above piece of code u are tring to call the setText method with null reference without instantiating.Please check it.
    Ramya:cool:

  3. #3
    YouGina is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    class okHandler implements ActionListener {
    public void actionPerformed(ActionEvent e) {
    RegistratiePanel reg = new RegistratiePanel();
    reg.tijdStart.setText("Dit is een test");
    }
    }
    Is this what you mean?

    When I try this, nothing happens, the text "Dit is een test" is not sended to the JTextField, but there's also no error, although I would expect a stackoverflow?

    The source with a couple of comments:
    PHP Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.BorderFactory;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JToolBar;
    import java.awt.LayoutManager;
    
    public class TijdsRegistratie extends JFrame {   
        public static void main(String[] args) {  	
        	JFrame frame = new TijdsRegistratie();
        	frame.setSize(500, 500);
        	frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        	frame.setTitle("Tijds Registratie");
        	frame.setContentPane(new RegistratiePanel());
        	frame.setVisible(true);
        }
    }
    
    class RegistratiePanel extends JPanel {
    	private JToolBar bar;
    	private JButton Ok, Cancel, Start, Stop;
    	public JTextField tijdStart, tijdEind, Voor, Van;
    	
    	public RegistratiePanel() {
    		
    		setLayout(null);
    		
    		Ok = new JButton("Ok"); // This button is going to be used
    		Cancel = new JButton("Cancel");
    		Start = new JButton("Start");
    		Stop = new JButton("Stop");
    		
    		Ok.addActionListener(new okHandler()); // The actionhandler is called
    		
    		bar = new JToolBar();
    		
    		bar.add(Ok);  // Button added to the JToolBar
    		bar.add(Cancel);
    		bar.add(Start);
    		bar.add(Stop);
    		
    		add(bar);		
    		bar.setFloatable(false);
    		bar.setRollover(true);
    		bar.setBounds(0, 0, 500, 30);
    		
    		tijdStart = new JTextField(10); // when Ok button is pushed, text should appear in here.
    		tijdEind = new JTextField(10);
    		Voor = new JTextField(10);
    		Van = new JTextField(10);
    		
    		tijdStart.setBounds(100, 50, 300, 20);
    		tijdEind.setBounds(100, 150, 300, 20);
    		Voor.setBounds(100, 250, 300, 20);
    		Van.setBounds(100, 350, 300, 20);
    		
    		add(tijdStart);
    		add(tijdEind);
    		add(Voor);
    		add(Van);
    					
    	}
    }
    class okHandler implements ActionListener {
    	public void actionPerformed(ActionEvent e) {
    		RegistratiePanel reg = new RegistratiePanel(); // I guess this is instantiating the RegistratiePanel?
    		reg.tijdStart.setText("Dit is een test"); // This is the text which should appear in the JTextField tijdStart
    	}
    }

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Hi,
    Something u want to experiment with Java Compiler and make everyone to get :confused: ..Just kidding.

    1.My question is when actionPerformed is called again u are instantiating and attaching the actionlistener with this object and totally u are doing the same thing make it stack overflow.

    2.Why u need separate handler class for this?

    I have corrected ur code.please check it.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Test extends JFrame {   
        public static void main(String[] args) {      
            JFrame frame = new Test();
            frame.setSize(500, 500);
            frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
            frame.setTitle("Tijds Registratie");
            frame.setContentPane(new RegistratiePanel());
            frame.setVisible(true);
        }
    }
    
    class RegistratiePanel extends JPanel implements ActionListener {
        private JToolBar bar;
        private JButton Ok, Cancel, Start, Stop;
        public JTextField tijdStart, tijdEind, Voor, Van;
        
        public RegistratiePanel() {
            
            setLayout(null);
            
            Ok = new JButton("Ok");
            Cancel = new JButton("Cancel");
            Start = new JButton("Start");
            Stop = new JButton("Stop");
            
            Ok.addActionListener(this);
            
            bar = new JToolBar();
            
            bar.add(Ok);
            bar.add(Cancel);
            bar.add(Start);
            bar.add(Stop);
            
            add(bar);        
            bar.setFloatable(false);
            bar.setRollover(true);
            bar.setBounds(0, 0, 500, 30);
            
            tijdStart = new JTextField(10);
            tijdEind = new JTextField(10);
            Voor = new JTextField(10);
            Van = new JTextField(10);
            
            tijdStart.setBounds(100, 50, 300, 20);
            tijdEind.setBounds(100, 150, 300, 20);
            Voor.setBounds(100, 250, 300, 20);
            Van.setBounds(100, 350, 300, 20);
            
            add(tijdStart);
            add(tijdEind);
            add(Voor);
            add(Van);
                        
        }
    
    	public void actionPerformed(ActionEvent e) {
            tijdStart.setText("Er is op Ok gedrukt");
        }
    }
    Ramya:cool:

  5. #5
    YouGina is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Do I have to do anything with something like this?:
    (JTextField) Beans.instantiate(getClass().getClassLoader(), JTextField.class.getName());

    Found this in a source on google, but i can't post the link, because i haven't 20 posts or more.

    If I need this, how can I submit this to my classes?

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Have u run my code?
    Ramya:cool:

  7. #7
    YouGina is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    Yes i have. I don't know if this is the right solution for my problem. Is it possible to instantiate and use the objects at the way I shown in the source?

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    Just gothru ur code piece.When u instantiate again u are trying to create the components and again u are trying to register the event results to stack overflow....Just rethink ............U shouldn't confuse yourself and make others also to get confused.

    U have to experiment but u shouldn't go out of focus.Just a freindly advice from me.

    -Regards
    Ramya
    Ramya:cool:

  9. #9
    YouGina is offline Member
    Join Date
    Jul 2009
    Posts
    5
    Rep Power
    0

    Default

    I've found out how to do this.

    I thank you for your quick replies. I think you're hint about instantiating helped me farther. I didn't no the English word for that, and Yesterday evening I googled and solved the problem.

    Thank you for your help again.

Similar Threads

  1. Determining ActionListener
    By siamino in forum New To Java
    Replies: 12
    Last Post: 05-25-2009, 11:04 PM
  2. ActionListener Error
    By blackstormattack in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 08:36 AM
  3. Java actionlistener help
    By justsomeguy in forum AWT / Swing
    Replies: 1
    Last Post: 05-27-2008, 05:42 AM
  4. How to use KeyListener and ActionListener
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 04-23-2008, 08:19 PM
  5. ActionListener interface
    By tsantana in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 10:24 PM

Posting Permissions

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