Results 1 to 14 of 14
- 06-01-2011, 04:03 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
FileInputStream Referencing (Probably Really Easy)
Hi,
I need to reference a FileInputStream (FIS) in a method but it is not letting me. The FIS is in my constructor and I the only solution can think of is taking it out and creating it at the top of my class but Eclipse is saying no because of necessary try and catch statements. How can I enable referencing of the FIS to objects in my method?
Thanks
- 06-01-2011, 04:09 AM #2
Define the variable in a globally scoped area and assign it a value within a method in a try-catch block
- 06-01-2011, 04:10 AM #3
In future it would help if you posted relevant code. However taking a guess, perhaps you can declare the variable as an instance variable and then initialise it in your constructor. Or you could pass it as a parameter.
- 06-01-2011, 04:11 AM #4
By the way if this is related to your continuing questions then read this from the API:
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader
- 06-01-2011, 04:16 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
Odd... FIS works when reading the chars.
- 06-01-2011, 04:21 AM #6
Show us the code and the results.
- 06-01-2011, 04:42 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
Well this is a bust...
How can I get this simple method to work for me?
The String Brem
The two files and two propertiesJava Code:String Brem = "Bremner's";
The get property value method.Java Code:Properties defprops = new Properties(); Properties props = new Properties(defprops); File defFile = new File("/Users/eric.manget/Desktop/defpoints.txt"); File file = new File("/Users/eric.manget/Desktop/points.txt");
Java Code:public Integer getbValue(){ try{ FileInputStream in = new FileInputStream(defFile); defprops.load(in); in.close(); in = new FileInputStream(file); props.load(in); String gbv = props.getProperty(Brem); Integer val = Integer.parseInt(gbv); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return val; }
It is giving me this NPE and it is saying val is not a variable.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.jav a:103)
at HouseHockey.Statistics.getbValue(Statistics.java:1 17)
at HouseHockey.Statistics.<init>(Statistics.java:29)
at HouseHockey.MainPage$2.actionPerformed(MainPage.ja va:89)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6374)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:613 9)
at java.awt.Container.processEvent(Container.java:208 5)
at java.awt.Component.dispatchEventImpl(Component.jav a:4736)
at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
at java.awt.Component.dispatchEvent(Component.java:45 66)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:45 66)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:680)
at java.awt.EventQueue.access$000(EventQueue.java:86)
at java.awt.EventQueue$1.run(EventQueue.java:639)
at java.awt.EventQueue$1.run(EventQueue.java:637)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:653)
at java.awt.EventQueue$2.run(EventQueue.java:651)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 650)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
- 06-01-2011, 04:53 AM #8
What are you trying to achieve? Do not describe what are you trying to do because it is highly probable that the way you have chosen to solve a problem is wrong and can be achieved a different way. As I said earlier I believe the choice to use a Properties file is wrong.
If you are trying to read data from one or more text files and write the data to another text file then I definately disagree with using a Properties file.
As to your actual code:
Why are you creating 2 Properties objects?
You have declared val inside the try statement and then try to access it outside the try statment. Once again you have a scope problem.
- 06-01-2011, 04:57 AM #9
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
Is there another class that lets me use keys and values?
1. Because I need a default properties file to access the variable initially.
2. I know which is why I need help.
- 06-01-2011, 05:04 AM #10
Did you even read what I said in another post?
You don't need a Properties file and key/value pairs. You just need to have your data in a plain text file and know what the format is so you can write to or read from that file. If you write "team name=points" to a file you can read the entire line back and use the split method on the = to get the 2 relevant bits of information.
Properties files are used to configure applications. For example if your app needed to connect to a server you can place the url inside a props file. Then if you need to change the url for testing purposes you simply change the props file and there is no need to compile you code, just run the app again so that it can pick up the new url.
- 06-01-2011, 05:09 AM #11
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
Ok I will try your method but this split method is something I have no knowledge on.
- 06-01-2011, 05:11 AM #12
Member
- Join Date
- Feb 2011
- Posts
- 83
- Rep Power
- 0
Also wouldn't that make it extremely difficult to access the information?
- 06-01-2011, 05:14 AM #13
It all depends upon how you store the data. Do you have the choice? If you are extremely lazy then you can store the data as:
team name
points
team name
points
team name
points
etc
Then you can use the Scanner class and call nextLine to read the team name and nextInt to read the points (followed by a nextLine by itself to move to the next line).
- 06-01-2011, 09:59 AM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Where is this code?Java Code:Properties defprops = new Properties(); Properties props = new Properties(defprops); File defFile = new File("/Users/eric.manget/Desktop/defpoints.txt"); File file = new File("/Users/eric.manget/Desktop/points.txt");
I suspect you are redeclaring your variables and thus hiding the "real" ones you use later on in here:
Also, how did you run this since this won't compile since val is out of scope in that return. it was declared inside the try/catch block.Java Code:public Integer getbValue(){ try{ FileInputStream in = new FileInputStream(defFile); defprops.load(in); in.close(); in = new FileInputStream(file); props.load(in); String gbv = props.getProperty(Brem); Integer val = Integer.parseInt(gbv); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return val; }
Similar Threads
-
Referencing object from another Thread
By Floiancu in forum Threads and SynchronizationReplies: 6Last Post: 05-15-2011, 01:20 PM -
referencing objects via method help
By alacn in forum New To JavaReplies: 13Last Post: 08-08-2010, 08:52 PM -
referencing files in .exe
By minime12358 in forum Advanced JavaReplies: 7Last Post: 07-26-2010, 08:28 PM -
Using weak referencing to gc listeners.
By MartinR in forum New To JavaReplies: 0Last Post: 05-29-2010, 11:37 PM -
Referencing JPanel Class
By uncopywritable in forum New To JavaReplies: 2Last Post: 08-12-2007, 01:31 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks