Results 1 to 9 of 9
- 07-09-2009, 12:10 PM #1
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`:
Thanks in advance.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"); } }
P.S. Sorry for my bad English, and non-English variables.Last edited by YouGina; 07-09-2009 at 12:12 PM.
- 07-09-2009, 12:26 PM #2
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:
- 07-09-2009, 01:04 PM #3
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, 01:32 PM #4
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:
- 07-09-2009, 01:38 PM #5
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, 01:41 PM #6
Have u run my code?
Ramya:cool:
- 07-09-2009, 02:02 PM #7
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, 02:09 PM #8
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
RamyaRamya:cool:
- 07-10-2009, 11:08 AM #9
Member
- Join Date
- Jul 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Determining ActionListener
By siamino in forum New To JavaReplies: 12Last Post: 05-25-2009, 11:04 PM -
ActionListener Error
By blackstormattack in forum New To JavaReplies: 1Last Post: 03-05-2009, 08:36 AM -
Java actionlistener help
By justsomeguy in forum AWT / SwingReplies: 1Last Post: 05-27-2008, 05:42 AM -
How to use KeyListener and ActionListener
By Java Tip in forum javax.swingReplies: 0Last Post: 04-23-2008, 08:19 PM -
ActionListener interface
By tsantana in forum New To JavaReplies: 2Last Post: 03-30-2008, 10:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks