Results 1 to 4 of 4
  1. #1
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default InputStream/InputStreamReader/maybe path... I'm not sure.

    Reading all the documentation, I can't find a reason why the following code works only when I give it a relative path. (canonical fails) Yes, I'm sure the file exists, and is where I think it is. I've even done a .exists using the canonical path, and this method still fails. Any idea's?
    Java Code:
    	/** This method reads a text file and puts each line in an array.
    	 * @param path
    	 * @param arraySize
    	 * @return String Array of "arraySize"
    	 */
    	public static String[] fileToStringArray(String path, int arraySize){
    		String[] array = new String[arraySize];
    				
    		try {
    			InputStream inputStream = Utility.class.getResourceAsStream(path);
    //This next line, BufferedReader is line 2351 per the Stack Trace
    			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    			try {
    				String line = reader.readLine();
    				int i = 0;
    				while (line != null) {
    					array[i] = line;
    					line = reader.readLine();
    					i++;
    				}
    			}
    			finally {
    				inputStream.close();
    				reader.close();
    			}
    		}
    		catch (IOException e){
    			e.printStackTrace();
    			JOptionPane.showMessageDialog(null, "An Error occured attempting " +
    					"to open the file:   \n" + path);
    		}
    		return array;
    	}
    OMG at the Stack Trace!
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.io.Reader.<init>(Unknown Source)
    at java.io.InputStreamReader.<init>(Unknown Source)
    at beta.pro.Utility.fileToStringArray(Utility.java:23 51)
    at beta.pro.Utility.fetchCustomLesson(Utility.java:39 29)
    at beta.pro.AlphaBest1$5.itemStateChanged(AlphaBest1. java:439)
    at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
    at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
    at javax.swing.JComboBox.contentsChanged(Unknown Source)
    at javax.swing.AbstractListModel.fireContentsChanged( Unknown Source)
    at javax.swing.DefaultComboBoxModel.setSelectedItem(U nknown Source)
    at javax.swing.JComboBox.setSelectedItem(Unknown Source)
    at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$Handler.mou seReleased(Unknown Source)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at javax.swing.plaf.basic.BasicComboPopup$1.processMo useEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(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)

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: InputStream/InputStreamReader/maybe path... I'm not sure.

    getResource gets a resource path relative either to the associated class, or to the execution root (eg the "top" of a jar file) if it starts with a "/".
    So passing in an absolute path into it will invariably result in a null resource uri coming back.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    NotTooOld is offline Hostage
    Join Date
    Apr 2013
    Location
    China
    Posts
    128
    Rep Power
    0

    Default Re: InputStream/InputStreamReader/maybe path... I'm not sure.

    Quote Originally Posted by Tolls View Post
    getResource gets a resource path relative either to the associated class, or to the execution root (eg the "top" of a jar file) if it starts with a "/".
    So passing in an absolute path into it will invariably result in a null resource uri coming back.
    I'm very likely misunderstanding you. I try sending that method a String holding the canonical path to the file.
    ie "C:\Eclipse\workspaceZero\Project\Custom Lessons\LS4_test1.aclf"
    Since canonical pathes start with the drive letter, (in my case C:) I don't get where the problem would be. I've even tried converting to a Path and using .relativize(). One difference I do see is that when I construct the path for the "known" lessons, I use "/", and not "\", but those paths don't start with a "/".
    Oh! OK, I see your meaning now. If I use getResource, or getResourceAsStream, I need to include the path from where the method is to C:, and then it should work. So what's the easy way to do that, or is there one? Or just don't bother making the file a resource at all? What did I read... getResource somehow records that file/resource into the java app's buildpath? Is that right?
    Last edited by NotTooOld; 05-07-2013 at 07:13 PM.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: InputStream/InputStreamReader/maybe path... I'm not sure.

    As I said, you;re using getResource, which gets the thing you asked for relative to the class or application.
    So if you pass in the above then it will look in:
    <your application directory/jar>/<package your class is in>/C/Eclipse/...etc etc

    Keep the file as a resource, assuming it's a core part of the application, and use a relative path to it.
    Ensure eclipse knows this file is part of the application, which is easy enough to do by simply ensuring it's in a source folder, so it will be copied over to the correct spot when the app is built. When eclipse jars up the application then it should add this resource in the correct spot.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. InputStreamReader
    By aaroncarpet in forum New To Java
    Replies: 1
    Last Post: 11-02-2012, 01:07 AM
  2. problem with InputStreamReader
    By vitaly87 in forum New To Java
    Replies: 2
    Last Post: 12-12-2010, 04:44 PM
  3. Replies: 0
    Last Post: 05-19-2010, 05:38 PM
  4. BufferedReader/InputStreamReader - NON-TECHNICAL
    By Abder-Rahman in forum New To Java
    Replies: 4
    Last Post: 09-27-2009, 10:49 PM
  5. Problem with in InputStreamReader, pls help
    By hyperkossan in forum New To Java
    Replies: 0
    Last Post: 12-10-2008, 08:50 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
  •