Reputation:
when i am clicking the submit button nothing in happening i created a program to make a counter where we can add the time and when i am entering the time in seconds and clicking the submit button it is not working
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
public class CountdownTimer extends Thread
{
JTextField tf,tf2;
JLabel l1,l2;
JFrame fr;
JButton b;
String n;
int t;
public void run()
{
buildGUI();
}
public void buildGUI()
{
fr = new JFrame("Countdown Timer");
JPanel p = new JPanel();
l2 = new JLabel("Please Enter the Time:");
l1 = new JLabel("");
tf2 = new JTextField(8);
tf2.setEnabled(true);
tf2.setBackground(Color.white);
p.setBackground(Color.white);
JButton b=new JButton("Submit");
b.setEnabled(true);
b.setVisible(true);
fr.add(p);
p.add(l2);
p.add(tf2);
p.add(b);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setSize(300,200);
fr.setResizable(false);
n = tf2.getText();
fr.setSize(300,200);
t = Integer.parseInt(n);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf = new JTextField(15);
tf.setEnabled(false);
b.setVisible(false);
tf2.setVisible(false);
l2.setVisible(false);
Font f = new Font("Verdana", 0, 18);
tf.setFont(f);
tf.setBackground(Color.white);
tf.setVisible(true);
p.add(tf);
for(int i = t;i>=0;i--)
{
try
{
Thread.sleep(1000);
String s = Integer.toString(i);
tf.setText(" "+ s + " Seconds to go...");
}
catch(Exception e1)
{
System.out.println(e);
}
}
JOptionPane.showMessageDialog(fr,"Times up!!!");
tf.setText("");
tf.setEnabled(false);
}
});
}
public static void main(String args[])
{
CountdownTimer obj = new CountdownTimer();
obj.start();
}
}
and when i am clicking the submit button i created i am getting an exception
Exception in thread "Thread-4" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at CountdownTimer.buildGUI(CountdownTimer.java:40)
at CountdownTimer.run(CountdownTimer.java:15)
Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770
Upvotes: 0
Views: 165
Reputation: 61
Fixed below 2 issues.
t = Integer.parseInt(n)
statement has a issue. Because At that momnent tf2 Test Field doesn't have value. Then tf2.getText()
return null
. Then t = Integer.parseInt(n);
statement throws null point exception.Below code is correctly working.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
public class CountdownTimer {
JTextField tf,tf2;
JLabel l1,l2;
JFrame fr;
JButton b;
String n;
int t;
public void run()
{
buildGUI();
}
public void buildGUI()
{
fr = new JFrame("Countdown Timer");
JPanel p = new JPanel();
l2 = new JLabel("Please Enter the Time:");
l1 = new JLabel("");
tf2 = new JTextField(8);
tf = new JTextField(15);
tf.setVisible(false);
tf2.setEnabled(true);
tf2.setBackground(Color.white);
p.setBackground(Color.white);
JButton b=new JButton("Submit");
b.setEnabled(true);
b.setVisible(true);
fr.add(p);
p.add(l2);
p.add(tf2);
p.add(b);
p.add(l1);
p.add(tf);
fr.setVisible(true);
fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr.setSize(300,200);
fr.setResizable(false);
fr.setSize(300,200);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf.setEnabled(false);
b.setVisible(false);
tf2.setVisible(false);
l2.setVisible(false);
tf.setVisible(true);
Font f = new Font("Verdana", 0, 18);
tf.setFont(f);
tf.setBackground(Color.white);
tf.setVisible(true);
n = tf2.getText();
t = Integer.parseInt(n);
Thread countThread = new Thread(new Runnable() {
@Override
public void run() {
for(int i = t;i >= 0; i--)
{
try
{
String s = Integer.toString(i);
tf.setText(" "+ s + " Seconds to go...");
Thread.sleep(1000);
}
catch(Exception e1)
{
System.out.println(e);
}
}
JOptionPane.showMessageDialog(fr,"Times up!!!");
}
});
countThread.start();
}
});
}
public static void main(String args[])
{
CountdownTimer obj = new CountdownTimer();
obj.buildGUI();
}
}
Upvotes: 0
Reputation: 150
The problem is with line 40 t = Integer.parseInt(n);
It seems that the value of n is not valid to be parsed as integer value.
1- you can consider validating the text is valid integer using this answer:
public static boolean isInteger(String s) {
return isInteger(s,10);
}
public static boolean isInteger(String s, int radix) {
if(s.isEmpty()) return false;
for(int i = 0; i < s.length(); i++) {
if(i == 0 && s.charAt(i) == '-') {
if(s.length() == 1) return false;
else continue;
}
if(Character.digit(s.charAt(i),radix) < 0) return false;
}
return true;
}
2- or you can first try to parse the text to know if the value is invalid to be parsed as an integer using this answer
public Integer tryParse(String text) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException e) {
return null;
}
}
Upvotes: 1