I want to make a simple stopwatch (in the case of Shildt, the Calendar is used and the handler is tucked into the class with the buttons). I looked at the listing and decided to modify it.

The error takes off:

Exception in thread "java.lang.Error: Unresolved compilation problem: at Nanotime.main (Nanotime.java:24)"

import java.awt.FlowLayout; import java.awt.Graphics; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; public class Nanotime { public static JLabel jlbl; public Nanotime() { JFrame jfrm = new JFrame("Секундомір"); jfrm.setBounds(500, 100, 200, 300); JButton Startbutton = new JButton("Start"); JButton Stopbutton = new JButton("Stop"); Nano_listener NL = new Nano_listener(); Startbutton.addActionListener(NL); Stopbutton.addActionListener(NL); jfrm.setLayout(new FlowLayout()); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel jlbl=new JLabel("Press 'Start'"); jlbl.setText("Press 'Start'"); } public static void main(String[] args) { //Ошибка тут SwingUtilities.invokeLater(new Runnable() { public void run() { new Nanotime(); } }); } } 

Another file:

 import java.awt.event.*; public class Nano_listener implements ActionListener { long l_temp; public void actionPerformed(ActionEvent ae) { if (ae.getActionCommand().equals("Start")) { l_temp = System.nanoTime(); Nanotime.jlbl.setText("Counting..."); } else { long l_result = System.nanoTime() - l_temp; Nanotime.jlbl.setText(String.valueOf(l_result)); l_temp = 0; l_result = 0; } } } 

Please do not take it as a task for me, because I wrote all the code myself

  • I have your code compiled - phvoronov
  • and everything works fine? - BubbleGum
  • Of course not, NullPointerException, you jbl you have null, and you call setText () with it - phvoronov
  • so you call this method without initializing the jbl variable. you have at the time of execution of this line jlbl == null. - phvoronov
  • How to initialize? - BubbleGum

1 answer 1

@BubbleGum , it starts, there are no errors, but it does not cope with the declared functionality, to put it mildly, the code is not working. But the error is not caused by the code. Reinstall jdk (I advise a stable version 6), and some free IDE that will show possible errors with minimal idle support. And, if you are going to move along the Swing path - just create frames, buttons, search for actions on them ... Just then grab the streams.

  • I repeat the question: WHAT DO I NEED TO FIX IN THE CODE? - BubbleGum
  • I repeat the answer: java.lang.Error - runtime error, check the jvm, compiler operation. If the code is working, then reinstall jdk. The error is NOT ASSOCIATED with the code - Viacheslav
  • I started it on another machine, added jfrm.add (Startbutton); jfrm.add (Stopbutton); jfrm.add (jlbl); jfrm.setVisible (true); A cloud of errors takes off (after pressing the Start button) - BubbleGum