0

In the below code I got a NullPointerException:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

timer.setInitialDelay((int) delay);

I tried to add

//timer = new Timer(0, null);
long delay = dates.getTime() - System.currentTimeMillis();
  timer = new Timer(0, null);
  new TimerStart();
  if (delay >= 0) {
   timer.setInitialDelay((int) delay);
   timer.restart();
  }

to avoid the Exception, it works but doesn't trigger the timer ?

With this Example it works with timer trigger too, but how could it be corrected in the code above :

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;
import java.util.Date;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SpinnerTest {

 Timer timer;
 Date date;
 public static void main(String args[]) {
   new SpinnerTest().startApp();
 }

 private void startApp() {
 JFrame frame = new JFrame("JSpinner Sample");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 date = new Date();
 SpinnerModel model1 = new SpinnerDateModel(date,null,null,0);
 JSpinner spinner1 = new JSpinner(model1);

 spinner1.addChangeListener(new CalendarListener());
 spinner1.setEditor(new JSpinner.DateEditor(spinner1, "HH:mm dd-MM-yyyy"));

 JLabel label1 = new JLabel("Times/Dates");
 JPanel panel1 = new JPanel(new BorderLayout());
 JPanel panel2 = new JPanel(new BorderLayout());

 panel2.add(label1, BorderLayout.WEST);
 panel1.add(spinner1, BorderLayout.CENTER);

 frame.add(panel1, BorderLayout.CENTER);
 frame.add(panel2, BorderLayout.WEST);

 frame.setSize(200, 90);
 frame.setVisible(true);
}
  private class CalendarListener implements ChangeListener {
   @Override
   public void stateChanged(ChangeEvent e)  {
    JSpinner jSpinner = (JSpinner) e.getSource();
    date = (Date) jSpinner.getValue() ;
    long delay = date.getTime() - System.currentTimeMillis();
    //System.out.println(delay);
    timerStart();
    if (delay > 0) {
    timer.setInitialDelay((int) delay);
    timer.restart();
   }
  }   
 }
 private void timerStart() {
     this.timer = new Timer(Integer.MAX_VALUE, (ActionEvent evt) -> {
     System.out.println("okey");
    });}
   } 
loadP
  • 404
  • 1
  • 4
  • 15
  • The heuristic for NullPointerExceptions is almost always the same: You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. – Hovercraft Full Of Eels Nov 19 '16 at 12:30
  • Thanks, I know which line, now I could solve it. It was a Problem with Timer, the trigger was a class TimerStart() which couldn't herited and instance the timer variable, I just delted the class and started the method directly. Should I post an answer ? – loadP Nov 19 '16 at 12:56
  • ? `timerStart()` is a method, not a class. – Hovercraft Full Of Eels Nov 19 '16 at 13:11
  • And no, NPE questions have been well asked and answered many times before, and in fact I've closed your question as yet another of the thousands of similar duplicates to this. See the [canonical question and its answers](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) for more on this. – Hovercraft Full Of Eels Nov 19 '16 at 13:12
  • In the future, search this site on your error message to find the similar questions first before asking. A simple Google search like [this one](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+NullPointerException) would have pointed you to the answer quickly. – Hovercraft Full Of Eels Nov 19 '16 at 13:12
  • Yes Sir I will do. In th code above new TimerStart() is a class. Thanks – loadP Nov 19 '16 at 13:20
  • Yes, `CalendarListener` is a class, but it doesn't appear to have a timer field of its own. – Hovercraft Full Of Eels Nov 19 '16 at 13:21
  • I didn't mean this code, but the code snippet more above. Which is part of another longer gui code. Thank you – loadP Nov 19 '16 at 13:25
  • Ah you mean the code you posted doesn't throw the NPE? Sorry, but this is not good. In the future, when asking about similar issues, you will want to post a valid [mcve] that does show us the exception, one we can run and modify. Else if we can't experience it, we can't easily help you. – Hovercraft Full Of Eels Nov 19 '16 at 13:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128530/discussion-between-giarui-and-hovercraft-full-of-eels). – loadP Nov 19 '16 at 13:38
  • Okey, I will care about. – loadP Nov 19 '16 at 13:48

0 Answers0