I am trying to use the Intfield() method and I am getting this error every time, I try to get the integer from the field. It compiles fine but doesn't load the frame, this is in the dos window. ------------------------------------------------------- Starting the login process... Exception in thread "main" java.lang.NumberFormatException: at java.lang.Integer.parseInt(Integer.java:426) at java.lang.Integer.parseInt(Integer.java:454) at IntField.getIntValue(IntField.java:56) at Bank_login.<init>(Bank_login.java:13) at Bank_login.main(Bank_login.java:85)
------------------------------------------ ------------- This is my code: ______________________________________________________ import javax.swing.*; import java.awt.*; import java.awt.event.*; import cs1.Keyboard;
class Bank_login extends JFrame {
//Text Areas. TextField bankname=new TextField(); String name=bankname.getText(); IntField bankIdentification=new IntField(20); int ID = bankIdentification.getIntValue();
//Buttons. JButton ok=new JButton("ok");
public Bank_login() { super("Login"); setSize(500,300); setLocation(200,200);
//Screen Title. JLabel welcome=new JLabel("Welcome to the NorthField Bank login",SwingConstants.LEFT); welcome.setFont (new Font ("Times New Roman", Font.BOLD, 28));
//Text labels. JLabel bankn=new JLabel("Enter the name of your bank:",SwingConstants.LEFT); bankn.setFont (new Font ("Times New Roman", Font.BOLD, 15)); JLabel bankID=new JLabel("Enter a four digit ID number:",SwingConstants.LEFT); bankID.setFont (new Font ("Times New Roman", Font.BOLD, 15));
//Creates a new container. Container contentPane = getContentPane(); contentPane.setLayout(null);
In your constructor you have a line that creates the IntField object then immediately after it you have
int ID = bankIdentification.getIntValue();
The method getIntValue() is returning an int and there is no numeric value in the textfield at this point. Therefore you will get a NumberFormatException here because you are trying to parse an empty String.
You'll have to forgive me. I looked at the code rather quickly. I am assuming that IntField is a custom class in your own package. That probably extends TextField. getIntValue() is a method in this class. You are declaring an as instance data an int
int ID = bankIdentification.getIntValue();
This field does not have a value in it at this point, but it is trying to parse it.
Again, I am Not sure if my assumption is right!!!! Would probably need to see other code.
Is the 20 that you pass to the constructor of the IntField class representing the value for the field? Is it being passed to a superclass like TextField? If so that 20 would represent the length of the TextField no the value in it.
By the way Jake, at the risk of repeating myself once again, your code will be much more readable if you use the [java] tag before it and the [/java] after it. Then people won't have as much trouble trying to visually parse it.
Here is an example without the java tags: class Slop { private boolean ugly = true;
public static void main( String [] args ) { new Slop().run(); }
public Slop() { } public Slop( boolean isUgly ) { ugly = isUgly; }
public void run() { for( int i = 0; i < 1000; i++ ) { if( ugly ) System.out.println( "This code looks like slop!" ); else System.out.println( "This code looks absolutely lovely!" ); } } }
And here is the same code (almost) with the mark-up tags:
class Slop
{
privateboolean ugly = true;
publicstaticvoid main( String [] args )
{
new Slop().run();
}
public Slop()
{
}
public Slop( boolean isUgly )
{
ugly = isUgly;
}
publicvoid run()
{
for( int i = 0; i < 1000; i++ )
{
if( ugly )
System.out.println( "This code looks like slop!" );
else
System.out.println( "This code looks absolutely lovely!" );
}
}
}
Oops, the second sample should have "ugly = false" but my session timed out and it seems that when your session times out, pressing "Preview" will do a "Post Message" and then ask you to log in (or maybe the same in the reverse order). That is another Jive bug to fix...