import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class Directions extends JFrame{ JFrame f1; JFrame f2; JFrame f3; JFrame f4; JFrame f5; JFrame f6; JButton jbtnUp; JButton jbtnDown; JButton jbtnLeft; JButton jbtnRight; JLabel jlblScore; JLabel jlblTime; JLabel imgDirections = new JLabel (new ImageIcon ("c:/blank.JPG")); JLabel imgUp = new JLabel (new ImageIcon ("c:/up.JPG")); JTextField jtxtDirections; Container cdirections; Timer dirTimer; String x = "up"; int score = 0; JFrame Directions; public Directions() { cdirections = this.getContentPane(); this.setTitle("Directions"); this.setSize(600,460); this.setLocation(0,0); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new FlowLayout()); jbtnUp = new JButton("Up"); jbtnDown = new JButton("Down"); jbtnLeft = new JButton("Left"); jbtnRight = new JButton("Right"); jlblScore = new JLabel("Score: ", score); jtxtDirections = new JTextField (x); jtxtDirections.setEditable(false); cdirections.add(imgDirections, BorderLayout.NORTH); cdirections.add(jlblScore, BorderLayout.EAST); cdirections.add(jbtnUp, BorderLayout.SOUTH); cdirections.add(jbtnDown, BorderLayout.SOUTH); cdirections.add(jbtnLeft, BorderLayout.SOUTH); cdirections.add(jbtnRight, BorderLayout.SOUTH); // ActionHandler actH = new ActionHandler(); // jbtStart.addActionListener(actH); // jbtInstructions.addActionListener(actH); f3 = this; jbtnUp.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dirTimer.start(); } }); jbtnDown.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dirTimer.start(); } }); jbtnLeft.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dirTimer.start(); } }); jbtnRight.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ dirTimer.start(); } }); } public void setTitleScreen(JFrame f){ f1=f; } public void setInstructions(JFrame f){ f2=f; } public void setDirections (JFrame f){ f3=f; } public void setColors (JFrame f){ f4=f; } public void setColorDirection (JFrame f){ f5=f; } public void setResults (JFrame f){ f6=f; } /*class ActionHandler implements ActionListener{ public void actionPerformed (ActionEvent e){ if (e.getSource() == jbtnUp){ score = score + 10; } } }*/ public class DirectionPicture extends JFrame{ //implements ActionListener{ // @Override //public void actionPerformed(ActionEvent e) { public void funTimes(){ dirTimer = new Timer (1, new TimerListener()); //dirTimer.start(); //System.out.println("HLEOO"); cdirections.add(imgUp, BorderLayout.NORTH); //dirTimer.stop(); //} } class TimerListener implements ActionListener{ public void actionPerformed(ActionEvent e){ repaint(); } } } }
Times & Null Pointer Exception
Page 1 of 14 Replies - 919 Views - Last Post: 04 December 2008 - 01:13 AM
#1
Times & Null Pointer Exception
Posted 03 December 2008 - 05:33 PM
So I'm new to Timers and I'm trying to get this code to work as part of my program. However I keep getting a null pointer exception when I press the up, down, left or right button (they all do the same thing at the moment but that'll be changed).
Replies To: Times & Null Pointer Exception
#2
Re: Times & Null Pointer Exception
Posted 03 December 2008 - 06:07 PM
Because your directions class doesn't have an instance/object of the timer.
#3
Re: Times & Null Pointer Exception
Posted 03 December 2008 - 09:23 PM
I see... how do you go around fixing that?
#4
Re: Times & Null Pointer Exception
Posted 03 December 2008 - 09:33 PM
Well you have the timer inside of another class that extends JFRame as well. I would pick one (which ever class needs it):
Or if the second class it to be a part of the first, add it and then call through
classTwo.timer.function()
//inside Directions //variable declaration Timer dirTimer; //inside constructor dirTimer = new Timer (1, new TimerListener());
Or if the second class it to be a part of the first, add it and then call through
classTwo.timer.function()
#5
Re: Times & Null Pointer Exception
Posted 04 December 2008 - 01:13 AM
Ah perfect got my problem fixed now.
Page 1 of 1