0

I know this question is common, so I have been frantically looking across the web for solutions, but to no avail. I simply want to load a file containing some text from inside the program JAR. Here is what I am trying to read it with:

            String text = "";
            int read, N = 1024 * 1024;
            char[] buffer = new char[N];
            try {

                InputStream is = this.getClass().getClassLoader().getResourceAsStream("/help.txt");
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                while(true) {
                    read = br.read(buffer, 0, N);
                    text += new String(buffer, 0, read);
                    if(read < N) {
                        break;
                    }
                }
            } catch(Exception ex) {
                ex.printStackTrace();
            }

I do not really understand java/eclipse build path stuff. But I have put help.txt in the root of the project folder. I currently get this error when the code above is executed.

java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
    at ajax.Gui.printHelp(Gui.java:330)
    at ajax.Gui.onCommand(Gui.java:151)
    at ajax.Gui$1.actionPerformed(Gui.java:130)
    at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
    at javax.swing.JTextField.postActionEvent(JTextField.java:721)
    at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1661)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2870)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2917)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2833)
    at java.awt.Component.processEvent(Component.java:6282)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
    at java.awt.Component.dispatchEventImpl(Component.java:4731)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

What do? Some extra info: OS = Linux Mint 14 KDE, JDK = JavaSE1.7, IDE = Eclipse

2 Answers2

0

You can try without the '/'

InputStream is = this.getClass().getClassLoader().getResourceAsStream("help.txt");

If you gave added the file in the root, this should work.

or you can create URL with the abosolute file path and then use

InputStream is = url.openStream();            
CoderP
  • 1,361
  • 1
  • 13
  • 19
0

The method getResourceAsStream is returning null because it cannot find the file.

You can do one of the following:

  • Make sure that the file is in the classpath; that's where getResourceAsStream searches.
  • Place the file in a resources directory as in this example.
  • Or you can place it in the source directory structure as in this stackoverflow question.

Additionally, you may want to use a StringBuilder to build your string instead of concatenating String after String.

Community
  • 1
  • 1
rgettman
  • 176,041
  • 30
  • 275
  • 357