Note that there are some explanatory texts on larger screens.

plurals
  1. POLoad a file from src folder into a reader
    text
    copied!<p>I would like to know how can I load a file <code>lol.txt</code> from <code>src</code> folder into my close method. The code so far:</p> <pre><code> public void close() throws IOException { boolean loadFromClasspath = true; String fileName = "..."; // provide an absolute path here to be sure that file is found BufferedReader reader = null; try { if (loadFromClasspath) { // loading from classpath // see the link above for more options InputStream in = getClass().getClassLoader().getResourceAsStream("lol.txt"); reader = new BufferedReader(new InputStreamReader(in)); } else { // load from file system reader = new BufferedReader(new FileReader(new File(fileName))); } String line = null; while ( (line = reader.readLine()) != null) { // do something with the line here System.out.println("Line read: " + line); } } catch (IOException e) { JOptionPane.showMessageDialog(null,e.getMessage()+" for lol.txt","File Error",JOptionPane.ERROR_MESSAGE); } finally { if (reader != null) { reader.close(); } } } </code></pre> <p>Console error output on initiation:</p> <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.io.Reader.&lt;init&gt;(Unknown Source) at java.io.InputStreamReader.&lt;init&gt;(Unknown Source) at main.main.close(main.java:191) at main.main$1.windowClosing(main.java:24) at java.awt.Window.processWindowEvent(Unknown Source) at javax.swing.JFrame.processWindowEvent(Unknown Source) at java.awt.Window.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown 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$000(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.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(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.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload