Results 1 to 3 of 3
- 07-11-2012, 08:32 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 2
- Rep Power
- 0
how do I handle the exception of my code?
in my code the paint method does not run and I do not know what is the problem?
the exception of my code is:
Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unk nown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.valueOf(Unknown Source)
at shoot.paint(shoot.java:192)
at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unkno wn Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Un known Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run( Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
code:
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeListener;
public class shoot extends JApplet {
boolean shoot = false;
int step = 0;
JTextField v0 ;
JTextField a0;
JTextField x0;
JTextField y0;
public void init(){
JPanel bord = new JPanel();
JLabel v = new JLabel("initial rate");
JTextField v0 = new JTextField(2);
JLabel a=new JLabel("initial angle");
JTextField a0 = new JTextField(2);
JLabel x=new JLabel("initial height");
JTextField x0 = new JTextField(2);
JLabel y=new JLabel("initial with");
JTextField y0=new JTextField(2);
JButton shootIt = new JButton("play");
JButton exit = new JButton("exit");
JLabel d = new JLabel();
JLabel dd = new JLabel();
JLabel ddd = new JLabel();
JLabel dddd = new JLabel();
JLabel ddddd = new JLabel();
JLabel l = new JLabel();
JLabel ll = new JLabel();
JLabel lll = new JLabel();
JLabel llll = new JLabel();
JLabel llllll = new JLabel();
JLabel nn = new JLabel();
JLabel nnn = new JLabel();
JLabel nnnn = new JLabel();
JLabel nnnnn = new JLabel();
JLabel nnnnnn = new JLabel();
JLabel nnnnnnn = new JLabel();
shootIt.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("you are shooting?!");
shoot = true;
repaint();
JTextField x0 = new JTextField(2);
String s="";
s=x0.getText();
Integer h = Integer.valueOf(s);
JTextField y0 = new JTextField(2);
String p="";
p=y0.getText();
Integer f = Integer.valueOf(p);
JTextField v0 = new JTextField(2);
String w="";
p=y0.getText();
Integer q = Integer.valueOf(p);
JTextField a0 = new JTextField(2);
String xx="";
xx=y0.getText();
Integer k = Integer.valueOf(p);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
bord.setBackground(Color.yellow);
bord.setLayout(new GridLayout(0,2,10,10));
bord.add(x);
bord.add(x0);
bord.add(nnnn);
bord.add(nnnnn);
bord.add(nnnnnn);
bord.add(nnnnnnn);
bord.add(a);
bord.add(a0);
bord.add(d);
bord.add(dd);
bord.add(ddd);
bord.add(dddd);
bord.add(y);
bord.add(y0);
bord.add(ddddd);
bord.add(l);
bord.add(ll);
bord.add(lll);
bord.add(v);
bord.add(v0) ;
bord.add(llll);
bord.add(llllll);
bord.add(nn);
bord.add(nnn);
bord.add(shootIt);
bord.add(exit);
exit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.setLayout(new GridLayout(1,2));
add(bord);
add(new JLabel( ));
this.setSize(800,800);
this.setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
JTextField x0 = new JTextField(2);
String s="";
s=x0.getText();
int h = Integer.valueOf(s);
JTextField y0 = new JTextField(2);
String p="";
p=y0.getText();
int f = Integer.valueOf(p);
int[] a=new int [4];
int []n=new int [4] ;
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
h=h+50;
a[i]=h;
f=f+50;
n[j]=f;
g.drawLine(n[j], a[i], n[j+1], a[i+1]);
}
}
if(shoot)
g.fillArc(f+(step++), h+(step++), 30, 30, 0, 360);
shoot = false;
g.setColor(Color.red);
g.fillRect (400, 500, 100,300);
g.setColor(Color.red);
g.fillRect (600, 500, 100,300);
g.drawString( "welcom!" , 500 , 200);
g.drawString( "if your bullet hit to barrier you will win!" , 500 , 250);
}
}
-
Re: how do I handle the exception of my code?
Duplicate thread closed. Original poster, please only *one* thread per question. Else you needlessly divide the discussion and that is not fair to the volunteers of this forum. It's also against the rules that you agreed to on joining the forum. While you're reading the forum rules and FAQ, please read up on how to use code tags so that your code is readable. Consider editing your post above and adding these tags.
-
Re: how do I handle the exception of my code?
Here's your offending code:
You're doing several things wrong here:Java Code:public void paint(Graphics g) { super.paint(g); JTextField x0 = new JTextField(2); String s = ""; s = x0.getText(); int h = Integer.valueOf(s); // *** error occurs here JTextField y0 = new JTextField(2); String p = ""; p = y0.getText(); int f = Integer.valueOf(p); int[] a = new int[4]; int[] n = new int[4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { h = h + 50; a[i] = h; f = f + 50; n[j] = f; g.drawLine(n[j], a[i], n[j + 1], a[i + 1]); } }
- You're drawing in the paint(...) method of a JApplet instead of the paintComponent(...) method of a JPanel. Please read the Swing painting tutorials as it will tell you why what you're doing is bad and why you're better off using paintComponent(...) in a JPanel or JComponent.
- You're creating JComponents inside of paint or paintComponent. Never do this. These methods are for painting only.
- You're trying to get text from these created JTextFields, fields that have never been displayed and thus have no chance of getting user input.
- Instead get your input only when the user presses a JButton -- thus get the text from the ***currently displayed JTextFields*** not made up JTextFields in an ActionListener's actionPerformed method. Then add the ActionListener to your JButton.
- Note that if a variable has the same name as another variable, it does not mean that it refers to the same object. Don't make new JTextFields in any of your listener code, since the JTextFields created have no relationship with the ones that are displayed in the GUI.
Last edited by Fubarable; 07-11-2012 at 08:58 PM.
Similar Threads
-
How to handle socket Exception
By mayank0512 in forum NetworkingReplies: 14Last Post: 12-21-2010, 11:31 PM -
how do java handle russian characters=unicode in Files&Streams code?
By lse123 in forum Advanced JavaReplies: 23Last Post: 08-12-2010, 09:59 AM -
How does the JVM handle an exception (e.g. NullPointerException) ?
By Paul Richards in forum Advanced JavaReplies: 8Last Post: 04-23-2010, 03:47 PM -
Handle exception manually
By John_28 in forum New To JavaReplies: 2Last Post: 06-05-2008, 11:26 AM -
[SOLVED] Handle own exception
By stevemcc in forum New To JavaReplies: 3Last Post: 04-10-2008, 04:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks