Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-24-2007, 05:32 PM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
NullPointerException
Code:
BufferedReader br = null; try { String inputFileLine = JOptionPane.showInputDialog("Insert text file: "); while((inputFileLine = br.readLine()) != null) {
What am i doing wrong? It says Exception in thread "main" java.lang.NullPointerException due to the line
Code:
while((inputFileLine = br.readLine()) != null) {
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-24-2007, 05:43 PM
Member
 
Join Date: Nov 2007
Posts: 1
Nickk is on a distinguished road
Well, you need to instatiate the BufferedReader.

br = new BufferedReader(new FileReader(filename));

See Read from a file using a BufferedReader - A Java Code Example
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2007, 06:55 PM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
thanks man!

Last edited by Feng : 11-24-2007 at 07:03 PM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-24-2007, 07:21 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
Code:
import java.io.*; import javax.swing.JOptionPane; public class ExpTest { public static void main(String[] args) { BufferedReader br = null; try { String inputFileLine = JOptionPane.showInputDialog("Insert text file: "); // Do we have a file to read? if(!new File(inputFileLine).exists()) { System.out.println("Can not find " + inputFileLine); } System.out.println("Attempting to read " + inputFileLine); // Do we have a BufferedReader to which "br" is pointing? // In other words, has "br" been instantiated which means // is it pointing to an object that has been created with // the "new" operator? Let's see: System.out.println("br = " + br); // Instantiate "br": br = new BufferedReader( new InputStreamReader( new FileInputStream(inputFileLine))); // Do we have a BufferedReader to which "br" is pointing? System.out.println("br = " + br); while((inputFileLine = br.readLine()) != null) { System.out.println(inputFileLine); } br.close(); } catch(IOException e) { System.out.println("Read error: " + e.getMessage()); } } }

Last edited by hardwired : 11-24-2007 at 07:23 PM. Reason: remove tabs
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-24-2007, 08:17 PM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
@hardwired

a)why we say
Code:
BufferedReader br=null;
before the try? and generally why we (need to) write it?

b)you typed
Code:
br = new BufferedReader( new InputStreamReader( new FileInputStream(inputFileLine)));
why you didnt type
Code:
BufferedReader br=new BufferedReader(new FileReader (inputFileLine));
?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-24-2007, 08:51 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
If you read the API docs, you would see that BufferedReader took a Reader in its constructor. InputStreamReader is a subclass of Reader and FileReader is a subclass of InputStreamReader. That means that Reader is a superclass for both InputStreamReader and FileReader.

Simply put, it doesn't matter which one he puts in there, but in InputStreamReader he has to specifically open an InputStream to the file, while with FileReader, the file is already opened.

Last edited by staykovmarin : 11-24-2007 at 08:56 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.lang.NullPointerException ravian New To Java 1 01-13-2008 08:39 PM
NullPointerException ravian New To Java 2 12-07-2007 05:20 PM
nullPointerException problem conandor Networking 1 08-14-2007 02:22 PM
NullPointerException problem warship AWT / Swing 5 08-10-2007 05:43 PM
ERROR: nullPointerException mathias New To Java 1 08-05-2007 07:54 AM


All times are GMT +3. The time now is 11:07 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org