
07-09-2009, 01:10 PM
|
|
Member
|
|
Join Date: Jul 2009
Posts: 5
Rep Power: 0
|
|
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 01:12 PM.
|
|

07-09-2009, 01:26 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
|
|
|
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
|
|

07-09-2009, 02:04 PM
|
|
Member
|
|
Join Date: Jul 2009
Posts: 5
Rep Power: 0
|
|
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 } }
|
|
|

07-09-2009, 02:32 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
|
|
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
|
|

07-09-2009, 02:38 PM
|
|
Member
|
|
Join Date: Jul 2009
Posts: 5
Rep Power: 0
|
|
|
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?
|
|

07-09-2009, 02:41 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
|
|
|
Have u run my code?
__________________
Ramya
|
|

07-09-2009, 03:02 PM
|
|
Member
|
|
Join Date: Jul 2009
Posts: 5
Rep Power: 0
|
|
|
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?
|
|

07-09-2009, 03:09 PM
|
 |
Senior Member
|
|
Join Date: Apr 2009
Location: Chennai
Posts: 533
Rep Power: 1
|
|
|
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
|
|

07-10-2009, 12:08 PM
|
|
Member
|
|
Join Date: Jul 2009
Posts: 5
Rep Power: 0
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:40 PM.
|
|