| | |
getting a nullpointer exception
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
I keep getting a null pointer exception with this code:
It's suppose to load a text file into a textArea.
It's suppose to load a text file into a textArea.
Java Syntax (Toggle Plain Text)
JFileChooser chooser = new JFileChooser(); int returnVal = chooser.showOpenDialog(this); String getFile = chooser.getName(); if(returnVal == JFileChooser.APPROVE_OPTION) { try { FileReader filegetter = new FileReader(getFile); BufferedReader br = new BufferedReader(filegetter); String line = br.readLine(); while ((line)!= null) { textArea2.setText(line); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (Exception myE) { myE.printStackTrace(); } }
•
•
Join Date: Sep 2004
Posts: 84
Reputation:
Solved Threads: 1
OK, I think I found your problem.
Change
String getFile = chooser.getName();
to
String getFile = chooser.getSelectedFile().getPath();
The reason you received a Null Pointer exception is
FileReader filegetter = new FileReader(getFile);
returns the name of the file. However getFile only contains the name of the file, not where to get it (path).
So since 'new FileReader(getFile);' can't find the file it returns NULL, hence the NPE.
Using:
getFile = chooser.getSelectedFile().getPath();
returns the fully qualified file with path information.
Hope this helps!
Change
String getFile = chooser.getName();
to
String getFile = chooser.getSelectedFile().getPath();
The reason you received a Null Pointer exception is
FileReader filegetter = new FileReader(getFile);
returns the name of the file. However getFile only contains the name of the file, not where to get it (path).
So since 'new FileReader(getFile);' can't find the file it returns NULL, hence the NPE.
Using:
getFile = chooser.getSelectedFile().getPath();
returns the fully qualified file with path information.
Hope this helps!
![]() |
Similar Threads
- Exception error for heap (Java)
- my program is compiled.but on run that i have message "Exception in thread "main" jav (Java)
- nullpointer exception...HELP!!! (Java)
- Thinking OOP!: From BASIC to JAVA (Java)
- modifying all of the instances in an array at once (Java)
Other Threads in the Java Forum
- Previous Thread: Why Doesn't My Sort Work!?
- Next Thread: problem with program finding "magic squares"
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






