Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2009, 01:10 PM
Member
 
Join Date: Jul 2009
Posts: 5
Rep Power: 0
YouGina is on a distinguished road
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(500500);
        
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 OkCancelStartStop;
    public 
JTextField tijdStarttijdEindVoorVan;
    
    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(0050030);
        
        
tijdStart = new JTextField(10);
        
tijdEind = new JTextField(10);
        
Voor = new JTextField(10);
        
Van = new JTextField(10);
        
        
tijdStart.setBounds(1005030020);
        
tijdEind.setBounds(10015030020);
        
Voor.setBounds(10025030020);
        
Van.setBounds(10035030020);
        
        
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 01:12 PM.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-09-2009, 01:26 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
RamyaSivakanth is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-09-2009, 02:04 PM
Member
 
Join Date: Jul 2009
Posts: 5
Rep Power: 0
YouGina is on a distinguished road
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(500500);
        
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 OkCancelStartStop;
    public 
JTextField tijdStarttijdEindVoorVan;
    
    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(0050030);
        
        
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(1005030020);
        
tijdEind.setBounds(10015030020);
        
Voor.setBounds(10025030020);
        
Van.setBounds(10035030020);
        
        
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
    
}

Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-09-2009, 02:32 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
Hi,
Something u want to experiment with Java Compiler and make everyone to get ..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.

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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-09-2009, 02:38 PM
Member
 
Join Date: Jul 2009
Posts: 5
Rep Power: 0
YouGina is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-09-2009, 02:41 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
RamyaSivakanth is on a distinguished road
Default
Have u run my code?
__________________
Ramya
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-09-2009, 03:02 PM
Member
 
Join Date: Jul 2009
Posts: 5
Rep Power: 0
YouGina is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-09-2009, 03:09 PM
RamyaSivakanth's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
RamyaSivakanth is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-10-2009, 12:08 PM
Member
 
Join Date: Jul 2009
Posts: 5
Rep Power: 0
YouGina is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining ActionListener siamino New To Java 12 05-26-2009 12:04 AM
ActionListener Error blackstormattack New To Java 1 03-05-2009 09:36 AM
Java actionlistener help justsomeguy AWT / Swing 1 05-27-2008 06:42 AM
How to use KeyListener and ActionListener Java Tip javax.swing 0 04-23-2008 09:19 PM
ActionListener interface tsantana New To Java 2 03-30-2008 11:24 PM


All times are GMT +2. The time now is 03:40 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org