Results 1 to 4 of 4
- 06-06-2009, 10:24 PM #1
[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:
Error: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; }
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.
- 06-06-2009, 11:03 PM #2
What is the location of the file?
Have a look:
Why include "" next to the propertiesJava 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()); } }
Do you mean in the put method statements, like this?
It's a trick way to make a String.Java Code:defaultSettings.put("width", "" + DEFAULT_WIDTH);
- 06-06-2009, 11:51 PM #3
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?
- 06-07-2009, 12:14 AM #4
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
-
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException:
By satishkumar_lskin in forum AWT / SwingReplies: 2Last Post: 12-14-2009, 01:46 AM -
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.Error
By jon80 in forum New To JavaReplies: 4Last Post: 06-07-2009, 12:59 AM -
[newbie] Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
By jon80 in forum New To JavaReplies: 12Last Post: 05-26-2009, 01:48 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM -
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-29-2008, 01:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks