Results 1 to 3 of 3
  1. #1
    Dado is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default [EDIT:code tags added] Exception on a simple size converter

    Hello everyone.
    I'm relatively new to java and i'm studying it at school at the moment. As the thread name implies, i'm trying to create a small size converter, but there's a problem with the item listener assigned to the choice menu. After selecting another option it should update the string to the new value, but the string keeps a null value.What am i doing wrong? am i forgetting something around? i really can't see where's the error..

    EDIT: sorry, didn't know that :o first time on a forum like this.
    Well, i don't know where the error is, but it's probably inside the ConvertListener, so i'll just tag that one.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.lang.*;
    class ConvertListener implements ActionListener,ItemListener{
    	private TextField t_in;
    	private TextField t_out;
    	private String s;
    	private double value;
    	private float v;
    	public ConvertListener(TextField t_in, TextField t_out){
    		this.t_in=t_in;
    		this.t_out=t_out;
    	}
    	public void itemStateChanged(ItemEvent e){
    		Object x=e.getItem();
    		s=x.toString();
    		System.out.println(s);
    	}
    	public void actionPerformed(ActionEvent e){
    		String tb=e.getActionCommand();
    		System.out.println("s---->"s);
    		System.out.println(tb);
    		if(tb.equals("Cancel")){
    			t_in.setText("");
    			t_out.setText("");
    		}
    		else if(tb.equals("Convert")){
    			System.out.println("Inside Convert");
    			value=Double.parseDouble(t_in.getText());
    			if(s.equals("Meters")){
    			System.out.println("Inside Meters");
    				value=value;
    			}
    			else if(s.equals("Decimeters")){
    			System.out.println("Inside Decimeters");
    				value=value/10;
    			}
    			else if(s.equals("Centimeters")){
    			System.out.println("Inside Centimeters");
    				value=value/100;
    			}
    			else if(s.equals("Millimeters")){
    			System.out.println("Inside Millimeters");
    				value=value/1000;
    			}
    			else {
    				System.out.println("Always get here");
    
    
    			}
    			t_out.setText(Double.toString(value));
    		}
    	}
    }
    System.out lines are for debugging purposes :)
    Thanks in advance for any help
    Last edited by Dado; 01-20-2011 at 08:23 PM. Reason: added code tags

  2. #2
    Nadian is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Code tags

    I made the mistake of attaching my code to my question, but its not a good thing to do. You need to use the CODE tag. When writing the message, copy and paste the code in then select it and click in the Wrap code (#) button, this quotes the code nicely in the main thread.I don't think people will look at your code otherwise.

  3. #3
    Dado is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    sorry for doubleposting, i edited it and i'm bumping it to top.

Similar Threads

  1. Simple Swing throws Exception in thread "AWT-EventQueue-0"
    By sultanofswing in forum AWT / Swing
    Replies: 3
    Last Post: 10-29-2010, 09:20 PM
  2. I need some help with converter GUI
    By linux1man in forum AWT / Swing
    Replies: 5
    Last Post: 07-29-2009, 11:57 PM
  3. Simple Unit Converter help
    By Black.Ice. in forum New To Java
    Replies: 5
    Last Post: 06-24-2009, 08:57 PM
  4. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 04:37 PM
  5. Simple JSP page got exception by using cookies
    By jurka in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 09-02-2008, 07:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •