Results 1 to 2 of 2
  1. #1
    shilpa80 is offline Member
    Join Date
    Mar 2014
    Posts
    2
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    Iam new to this forum and trying to write a code to remove comment lines from the input java file.Iam getting an error -Exception in thread "main" java.lang.NullPointerException. Please tell me whats wrong in the code.It is showing error- Exception in thread "main" java.lang.NullPointerException in- return new String(buffer),, and the word "String" everywhere present in the program.....Please help me out.

    public static void main(String[] args) {String source = readFile("sorce.java");

    System.out.println(source.replaceAll("(?:/\\*(?:[^*]|(?:\\*+[^*/]))*\\*+/)| (?://.*)",""));


    }

    static String readFile(String fileName) {

    File file = new File(fileName);

    char[] buffer = null;

    try {
    BufferedReader bufferedReader = new BufferedReader( new FileReader(file));

    buffer = new char[(int)file.length()];

    int i = 0;
    int c = bufferedReader.read();

    while (c != -1) {
    buffer[i++] = (char)c;
    c = bufferedReader.read();
    }

    } catch (IOException e) {
    e.printStackTrace();
    }

    return new String(buffer);
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Please copy the full text of the error message and paste it here. It has important info about the error.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 04-19-2012, 07:10 PM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. Replies: 5
    Last Post: 10-11-2011, 12:16 AM
  4. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  5. Replies: 1
    Last Post: 07-16-2009, 10:35 AM

Tags for this Thread

Posting Permissions

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