Results 1 to 4 of 4
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    6

    Default [newbie] Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException:

    Any ideas?

    1. What is the location of the file?
    2. Why include "" next to the properties (see code)?

    NOTE: I copied this code from a bowk..

    :confused:

    Java Code:
    [B]PropertiesTest.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.EventQueue;
    import java.awt.event.*;
    import java.io.*;
    import java.util.Properties;
    import javax.swing.*;
    
    /** A program to test properties.  The program remembers the 
     * frame position, size, and title.
     */
    public class PropertiesTest {
    	
    	public static void main (String[] args)
    	{
    		EventQueue.invokeLater(new Runnable()
    		{
    			public void run()
    			{
    				PropertiesFrame frame = new PropertiesFrame();
    				frame.setVisible(true);
    			}
    		});
    	}
    
    }
    
    
    [B]PropertiesFrame.java[/B]
    package homenetwork.bkr.training;
    
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.*;
    import java.util.Properties;
    import javax.swing.*;
    
    
    /**
     * A frame that restores position and size from a properties file and updates
     * the properties upon exit.
     */
    @SuppressWarnings("serial")
    public class PropertiesFrame extends JFrame {
    	
    	public PropertiesFrame()
    	{
    		//get position, size and title from properties
    		String userDir = System.getProperty("user.home");
    		File propertiesDir = new File(userDir, ".corejava");
    		if (!propertiesDir.exists()) propertiesDir.mkdir();
    		propertiesFile = new File(propertiesDir, "program.properties");
    		
    		Properties defaultSettings = new Properties();
    		defaultSettings.put("left", 0);
    		defaultSettings.put("top", 0);
    		[B]defaultSettings.put("width", "" + DEFAULT_WIDTH);[/B]
    		[B]defaultSettings.put("height", "" + DEFAULT_HEIGHT); //why include a blank string ""?[/B]
    		defaultSettings.put("title", "");
    		settings = new Properties(defaultSettings);
    		
    		if (propertiesFile.exists())
    			try
    				{
    					FileInputStream in = new FileInputStream(propertiesFile);
    					settings.load(in);
    				}
    			catch (IOException ex)
    				{
    					ex.printStackTrace();
    				}
    			
    		[B]int left = Integer.parseInt(settings.getProperty("left")); //code breaks here[/B]
    		int top = Integer.parseInt(settings.getProperty("top"));
    		int width = Integer.parseInt(settings.getProperty("width"));
    		int height = Integer.parseInt(settings.getProperty("height"));
    		setBounds(left, top, width, height);
    		
    		//if no title given, ask user
    		String title = settings.getProperty("title");
    		if (title.equals("")) title = JOptionPane.showInputDialog("Please supply a frame title:");
    		if (title == null) title = "";
    		setTitle(title);
    		
    		addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent event)
    			{
    				settings.put("left", "" + getX());
    				settings.put("top", "" + getY());
    				settings.put("width", "" + getWidth());
    				settings.put("height", "" + getHeight());
    				settings.put("title", getTitle());
    				
    				try 
    				{
    					FileOutputStream out = new FileOutputStream(propertiesFile);
    					settings.store(out, "Program Properties");
    				}
    				catch (IOException ex)
    				{
    					ex.printStackTrace();
    				}
    				System.exit(0);
    			}
    		});
    	}
    
    	private File propertiesFile;
    	private Properties settings;
    	
    	public static final int DEFAULT_WIDTH = 300;
    	public static final int DEFAULT_HEIGHT = 200;
    }
    Error:
    Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: null
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at homenetwork.bkr.training.PropertiesFrame.<init>(Pr opertiesFrame.java:44)
    at homenetwork.bkr.training.PropertiesTest$1.run(Prop ertiesTest.java:20)

    at java.awt.event.InvocationEvent.dispatch(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)
    Last edited by jon80; 06-06-2009 at 10:29 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,577
    Rep Power
    7

    Default

    What is the location of the file?
    Have a look:
    Java Code:
    import java.io.*;
    
    public class Test {
        public static void main(String[] args) {
            String userDir = System.getProperty("user.home");
            System.out.println("userDir = " + userDir);
            File propertiesDir = new File(userDir, ".corejava");
            System.out.println("propertiesDir = " + propertiesDir.getPath());
            if (!propertiesDir.exists()) propertiesDir.mkdir();
            File propertiesFile = new File(propertiesDir, "program.properties");
            System.out.println("propertiesFile = " + propertiesFile.getPath());
        }
    }
    Why include "" next to the properties
    Do you mean in the put method statements, like this?
    Java Code:
    defaultSettings.put("width", "" + DEFAULT_WIDTH);
    It's a trick way to make a String.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    6

    Default

    The only problem is that I could not find say "user.home" anywhere on my hdd.

    Can you expand on why this trick is needed?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,577
    Rep Power
    7

    Default

    The only problem is that I could not find say "user.home" anywhere on my hdd.
    That's because it is a java property key. Run the Test app and see what the output is — it will show you the address of your home folder.

    Can you expand on why this trick is needed?
    Look up the put method in the Properties class api:
    You find it down in the section Methods inherited from class java.util.Hashtable. Follow the put link:
    The Hashtable put method takes an Object; in this app a String.
    DEFAULT_WIDTH is a primitive data type int.
    If you didn't use the trick to make a String you would do
    Java Code:
    defaultSettings.put("width", String.valueOf(DEFAULT_WIDTH));

Similar Threads

  1. Replies: 2
    Last Post: 12-14-2009, 01:46 AM
  2. Replies: 4
    Last Post: 06-07-2009, 12:59 AM
  3. Replies: 12
    Last Post: 05-26-2009, 01:48 PM
  4. Replies: 10
    Last Post: 01-29-2009, 08:00 AM
  5. Replies: 3
    Last Post: 01-29-2008, 01:37 AM

Posting Permissions

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