Results 1 to 6 of 6
Thread: NullPointerException
- 11-24-2007, 04:32 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
NullPointerException
What am i doing wrong? It says Exception in thread "main" java.lang.NullPointerException due to the lineJava Code:BufferedReader br = null; try { String inputFileLine = JOptionPane.showInputDialog("Insert text file: "); while((inputFileLine = br.readLine()) != null) {Java Code:while((inputFileLine = br.readLine()) != null) {
- 11-24-2007, 04:43 PM #2
Member
- Join Date
- Nov 2007
- Posts
- 1
- Rep Power
- 0
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
- 11-24-2007, 05:55 PM #3
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
thanks man!
Last edited by Feng; 11-24-2007 at 06:03 PM.
- 11-24-2007, 06:21 PM #4
Java 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 06:23 PM. Reason: remove tabs
- 11-24-2007, 07:17 PM #5
Member
- Join Date
- Nov 2007
- Posts
- 13
- Rep Power
- 0
@hardwired
a)why we saybefore the try? and generally why we (need to) write it?Java Code:BufferedReader br=null;
b)you typed
why you didnt typeJava Code:br = new BufferedReader( new InputStreamReader( new FileInputStream(inputFileLine)));?Java Code:BufferedReader br=new BufferedReader(new FileReader (inputFileLine));
- 11-24-2007, 07:51 PM #6
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
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 07:56 PM.
Similar Threads
-
java.lang.NullPointerException
By ravian in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:39 PM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 04:20 PM -
nullPointerException problem
By conandor in forum NetworkingReplies: 1Last Post: 08-14-2007, 01:22 PM -
NullPointerException problem
By warship in forum AWT / SwingReplies: 5Last Post: 08-10-2007, 04:43 PM -
ERROR: nullPointerException
By mathias in forum New To JavaReplies: 1Last Post: 08-05-2007, 06:54 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks