CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Hybrid View

  1. #1
    Join Date
    Sep 2011
    Posts
    197

    [RESOLVED] NullPointer/warnings

    why does the toCharArray throw nullpointer?
    Code:
    String allTxt;
    allTxt = br.readLine();
    char[] araFull = allTxt.toCharArray();
    Last edited by kolt007; October 31st, 2011 at 05:51 PM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: NullPointer/warnings

    Because allTxt is null. Presumably because readline() is returning null.

    Always read the API docs so you know what a method does and what values it can return. I'm guessing br is a BufferredReader in which case the API docs say "Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached"
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: NullPointer/warnings

    If there is a chance that you might get null pointer then please use use try catch block , in this case it is a null pointer exception.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: NullPointer/warnings

    If there is a chance that you might get null pointer then please use use try catch block , in this case it is a null pointer exception.
    Generally speaking you should not catch NullPointerException as they are normally thrown because of a coding error ie you are trying to use a reference which you believe is valid but it isn't.

    If a variable may be null (eg because, as in this case, the method can return null) you should explicitly test for it not being null before you attempt to use it.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Sep 2011
    Posts
    197

    Re: NullPointer/warnings

    I was going to say that exact thing keang +D. Just because, the board instructions call for me to do so.
    my solution:
    Code:
               public static void readOutput() throws IOException{
        String allTxt;
        int cap;
        int read;
          try 
        (BufferedReader br = new BufferedReader(new FileReader(file))) {
              allTxt = br.readLine();
               cap = allTxt.length();
    if(allTxt != null) {
      CharArrayWriter caw = new CharArrayWriter(cap);
        caw.write(allTxt, 0, cap);
      char[] araFull = caw.toCharArray();
     // code continues
    Last edited by kolt007; November 1st, 2011 at 01:14 PM.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: NullPointer/warnings

    Code:
            allTxt = br.readLine();
            cap = allTxt.length();
    if(allTxt != null) {...
    Do you really think you've solved the problem with this code?

    What's going to happen when the second line runs if allTxt is null?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Sep 2011
    Posts
    197

    Re: NullPointer/warnings

    I am missunderstanding unless that if statment returns true it won't run at all?

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured