Results 1 to 14 of 14
  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default

    Define the variable in a globally scoped area and assign it a value within a method in a try-catch block

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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

  5. #5
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Odd... FIS works when reading the chars.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,900
    Rep Power
    25

    Default

    Show us the code and the results.

  7. #7
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Well this is a bust...

    How can I get this simple method to work for me?

    The String Brem

    Java Code:
    String Brem = "Bremner's";
    The two files and two properties

    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");
    The get property value method.

    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)

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  9. #9
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    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.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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.

  11. #11
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Ok I will try your method but this split method is something I have no knowledge on.

  12. #12
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    Also wouldn't that make it extremely difficult to access the information?

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    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).

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    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");
    Where is this code?
    I suspect you are redeclaring your variables and thus hiding the "real" ones you use later on in here:

    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;
    	}
    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.

Similar Threads

  1. Referencing object from another Thread
    By Floiancu in forum Threads and Synchronization
    Replies: 6
    Last Post: 05-15-2011, 02:20 PM
  2. referencing objects via method help
    By alacn in forum New To Java
    Replies: 13
    Last Post: 08-08-2010, 09:52 PM
  3. referencing files in .exe
    By minime12358 in forum Advanced Java
    Replies: 7
    Last Post: 07-26-2010, 09:28 PM
  4. Using weak referencing to gc listeners.
    By MartinR in forum New To Java
    Replies: 0
    Last Post: 05-30-2010, 12:37 AM
  5. Referencing JPanel Class
    By uncopywritable in forum New To Java
    Replies: 2
    Last Post: 08-12-2007, 02:31 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
  •