问题
I have an applet that needs to load saved data from a local text file that is in the eclipse package with the .java files. For use in this questions its name is "saveData.txt".
When I run the applet through eclipse, everything works perfectly. However, once I jar it up (making sure the txt file and .classpath files are included) and put it onto a website, the applet fails to load. I am getting an error of Unknown source on my InputStreamReader. I have included my code for loading and saving below. The error comes on the creation of the InputStreamReader Line.
Any Ideas where I screwed up?
private ArrayList loadLocations() throws IOException{
ArrayList locations = new ArrayList();
InputStream is = getClass().getResourceAsStream("\saveData.txt");
;; BELOW LINE is where exception is thrown
InputStreamReader isr = new InputStreamReader(is);
mInputStream = new BufferedReader(isr);
String line = mInputStream.readLine();
while (line != null && !(line.equals(""))) {
locations.add(new Location(line));
line = mInputStream.readLine();
}
if (mInputStream != null)
mInputStream.close();
Collections.sort(locations);
return locations;
}
private void saveLocations() throws IOException {
try {
mOutputStream = new PrintWriter(new File(getClass().getResource("\\saveData.txt").toURI()));
}
catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
;
for (Location l : locations) {
mOutputStream.print(l.getRawListing()+ "\n");
}
if (mOutputStream != null)
mOutputStream.close();
}
Full Error details (when run in browser)
Java Plug-in 1.6.0_24 Using JRE version 1.6.0_24-b07 Java HotSpot(TM) Client VM User home directory = C:\Users\Ross ---------------------------------------------------- c: clear console window f: finalize objects on finalization queue g: garbage collect h: display this help message l: dump classloader list m: print memory usage o: trigger logging q: hide console r: reload policy configuration s: dump system and deployment properties t: dump thread list v: dump thread stack x: clear classloader cache 0-5: set trace level to ---------------------------------------------------- java.lang.reflect.InvocationTargetException at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source) at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source) at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source) at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.NullPointerException at java.io.Reader.(Unknown Source) at java.io.InputStreamReader.(Unknown Source) at listings.listings.loadLocations(listings.java:624) at listings.listings.setup(listings.java:667) at listings.listings.(listings.java:59) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$000(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.awt.EventQueue$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$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) Exception: java.lang.reflect.InvocationTargetException
来源:https://stackoverflow.com/questions/5526632/getresourceasstream-working-in-eclipse-but-not-when-run-as-applet-in-browser