Results 1 to 4 of 4
  1. #1
    Sasquatch192 is offline Member
    Join Date
    Oct 2009
    Location
    Victoria, Australia
    Posts
    4
    Rep Power
    0

    Cool Null Pointer Exception with BufferedWriter

    Hi Guys,

    The code below is throwing a Null Pointer Exception at this line:
    Java Code:
    out = new BufferedWriter(fw);
    and I canít work out why. I first thought it was to do with scope, but after fiddling with the declarations it didnít make any difference. It's like it can't see what the out object is or something.
    Could you please have a look and let me know?

    Thanks :)

    Java Code:
    import java.io.*;
    
    public class Storage {
        protected File file;
        protected String path = "C:/Users/Chicky/Documents/NetBeansProjects/Drive/Input";
        protected String filepath = "C:/Users/Chicky/Documents/NetBeansProjects/Drive/Input/Data.csv";
        protected FileWriter fw;
        protected BufferedWriter out;
    
        public void Storage(){
    
        }
    
        protected void setup() throws IOException{
            file = new File(path);
            if(!file.isDirectory()){
                file.mkdir();
                fw = new FileWriter(filepath, true);
            }
            else if(file.isDirectory() && file.exists()){
                //don't want to do anything here
            }
            out = new BufferedWriter(fw);
        }
    ...
    ...

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Look at the stacktrace reported for the exception and post the first in your code where the exception was reported. Perhaps you didn't call setup at all but the line number reported should tell us more.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,343
    Blog Entries
    7
    Rep Power
    20

    Default

    Suppose your file is a directory; in that case your member variable 'fw' won't be initialized to a non-null value and presto: a NullPointerException when you want to create a Writer ...

    kind regards,

    Jos

  4. #4
    senorbum is offline Member
    Join Date
    Aug 2009
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Suppose your file is a directory; in that case your member variable 'fw' won't be initialized to a non-null value and presto: a NullPointerException when you want to create a Writer ...

    kind regards,

    Jos

    Yeah, I assume this is the issue.

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  3. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •