9 Replies - 639 Views - Last Post: 30 April 2009 - 04:57 AM Rate Topic: -----

#1 Eeyore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 09

Having trouble with file save code

Posted 29 April 2009 - 04:47 PM

This is the code i am having trouble with:

	
if(event.getSource()== ItemListButton){
		
		
		String fileName = JOptionPane.showInputDialog(null, "Enter FileName: ");
		PrintWriter pW = null;
		
		fileName = "F:/Item List/itemlist.txt";
		try{
			pW = new PrintWriter(new FileOutputStream(new File(fileName)));
			for(int i=0; i <5; i++);
				pW.append(dispensableItems[counter].getItemValues());
			}
		catch(Exception e){
			e.printStackTrace();
		}
		pW.close();

}



When i run the above code it creates the file but when i open the file it is empty and i get runtime errors in the console screen.
Can anyone help me?

Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble with file save code

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: Having trouble with file save code

Posted 29 April 2009 - 05:13 PM

First off I would like to welcome you to DIC! Great to have you here.

Second:
Did you previously open the file? If So did you close it before you opened it again?

This post has been edited by markhazlett9: 29 April 2009 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#3 Eeyore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 09

Re: Having trouble with file save code

Posted 29 April 2009 - 05:15 PM

View Postmarkhazlett9, on 29 Apr, 2009 - 04:13 PM, said:

Did you previously open the file? If So did you close it before you opened it again?


No i didn't previously open it.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Having trouble with file save code

Posted 29 April 2009 - 05:47 PM

and what type of run time error ?
and what the toString() method of dispensableItem[xxx].getItemValues() returns ?
Was This Post Helpful? 0
  • +
  • -

#5 trtherrien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 09

Re: Having trouble with file save code

Posted 29 April 2009 - 07:46 PM

View Postpbl, on 29 Apr, 2009 - 04:47 PM, said:

and what type of run time error ?
and what the toString() method of dispensableItem[xxx].getItemValues() returns ?


This may not fix your issue, but for safe practice add a finally block after the catch so that you know that the file is always closed.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Having trouble with file save code

Posted 29 April 2009 - 08:39 PM

View Posttrtherrien, on 29 Apr, 2009 - 06:46 PM, said:

This may not fix your issue, but for safe practice add a finally block after the catch so that you know that the file is always closed.

If the write didn't finish correctly we would have ad a stack dump
I suspect he never entered that try { for...
Was This Post Helpful? 0
  • +
  • -

#7 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Having trouble with file save code

Posted 29 April 2009 - 08:59 PM

Your setting a value for fileName twice, try taking out one of them and see if that fixes your problem
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Having trouble with file save code

Posted 29 April 2009 - 09:16 PM

View Postwebmin, on 29 Apr, 2009 - 07:59 PM, said:

Your setting a value for fileName twice, try taking out one of them and see if that fixes your problem

I guess the first one is the original one... that takes input fromn the user
The second is "hard coded" stuff for testing purposes
perfectly legitimate
Was This Post Helpful? 0
  • +
  • -

#9 Eeyore  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-April 09

Re: Having trouble with file save code

Posted 30 April 2009 - 02:37 AM

View Postpbl, on 29 Apr, 2009 - 08:16 PM, said:

View Postwebmin, on 29 Apr, 2009 - 07:59 PM, said:

Your setting a value for fileName twice, try taking out one of them and see if that fixes your problem

I guess the first one is the original one... that takes input fromn the user
The second is "hard coded" stuff for testing purposes
perfectly legitimate



These are the errors i am getting:

java.lang.ArrayIndexOutOfBoundsException: 5
at DispenserMachineGUI.actionPerformed(DispenserMachineGUI.java:154)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.window.dispatchEventImpl(window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

p.s. i'm not a bloke!
Was This Post Helpful? 0
  • +
  • -

#10 trtherrien  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 17-April 09

Re: Having trouble with file save code

Posted 30 April 2009 - 04:57 AM

View PostEeyore, on 30 Apr, 2009 - 01:37 AM, said:

View Postpbl, on 29 Apr, 2009 - 08:16 PM, said:

View Postwebmin, on 29 Apr, 2009 - 07:59 PM, said:

Your setting a value for fileName twice, try taking out one of them and see if that fixes your problem

I guess the first one is the original one... that takes input fromn the user
The second is "hard coded" stuff for testing purposes
perfectly legitimate



These are the errors i am getting:

java.lang.ArrayIndexOutOfBoundsException: 5
at DispenserMachineGUI.actionPerformed(DispenserMachineGUI.java:154)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6134)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5899)
at java.awt.Container.processEvent(Container.java:2023)
at java.awt.Component.dispatchEventImpl(Component.java:4501)
at java.awt.Container.dispatchEventImpl(Container.java:2081)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4301)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3965)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3895)
at java.awt.Container.dispatchEventImpl(Container.java:2067)
at java.awt.window.dispatchEventImpl(window.java:2458)
at java.awt.Component.dispatchEvent(Component.java:4331)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

p.s. i'm not a bloke!


Could it be that you are using i in your for loop, but using counter to access your array? Or just make the loop be 0 - the length of your array to make sure you are not out of bounds.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1