CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  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?

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

    Re: NullPointer/warnings

    But that line of code is before the if statement
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Sep 2011
    Posts
    197

    Re: NullPointer/warnings

    Those variables are not instantiated yet. They are able to be assinged null?

    Code:
    String s;
    Is a null string anyway isn't it? Because, it has no instantiated value/variable?

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

    Re: NullPointer/warnings

    Those variables are not instantiated yet.
    No I mean the line:
    Code:
     cap = allTxt.length();
    It's before the if statement that checks alltxt is not null.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #11
    Join Date
    Sep 2011
    Posts
    197

    Re: NullPointer/warnings

    Excuse my ignorence if I'm wrong but, I thought that 'cap' was considedered null untill assinged to a variable. Sense I am assigning it to a possible null if it's null the cap vairable will not throw an exception. So in my head it will work like this..

    Code:
    try 
        (BufferedReader br = new BufferedReader(new FileReader(file))) {
              allTxt = br.readLine();
               cap = allTxt.length(); // at this point both allTxt variable and, cap are considered either 
                                                        // null or, the integer value of length()/readLine()
    if(allTxt != null) {  // test's to see if allTxt is null and, if it is the below refrences will not be 
                                   // executed, causing no null pointer to be thrown.
      CharArrayWriter caw = new CharArrayWriter(cap);
        caw.write(allTxt, 0, cap);
      char[] araFull = caw.toCharArray();

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

    Re: NullPointer/warnings

    Excuse my ignorence if I'm wrong but, I thought that 'cap' was considedered null untill assinged to a variable. Sense I am assigning it to a possible null if it's null the cap vairable will not throw an exception. So in my head it will work like this..
    Why do you think this has anything to do with cap?
    cap is presumably an int, ie a primitive type, and so you can't assign null to it anyway, but that aside the issue is not with cap it's with allTxt.

    cap = allTxt.length(); // at this point both allTxt variable and, cap are considered either
    // null or, the integer value of length()/readLine()
    Read what you have written as a comment - how can it make any sense to attempt to call a method on allTxt when it might be null. If allTxt is null there is no object to call the method on and so you get a NullPointerException.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  13. #13
    Join Date
    Aug 2011
    Location
    West Yorkshire, U.K.
    Posts
    54

    Re: NullPointer/warnings

    You assign:
    Code:
    allTxt = br.readLine();
    followed by
    Code:
    cap = allTxt.length();
    without checking if allTxt is null.
    If br.readLine() returns null, allTxt is null.
    If allTxt is null, you get a null pointer exception thrown.
    It is thrown because you are trying to use a reference (allTxt) which is pointing to nothing (null), not the assignment of cap.

  14. #14
    Join Date
    Sep 2011
    Posts
    197

    Re: NullPointer/warnings

    Ah, yes I see where my mistake is. Null can be anything, but nothing can be null..

  15. #15
    Join Date
    Sep 2011
    Posts
    197

    Re: NullPointer/warnings

    Quote Originally Posted by AlexVV View Post
    You assign:
    Code:
    allTxt = br.readLine();
    followed by
    Code:
    cap = allTxt.length();
    without checking if allTxt is null.
    If br.readLine() returns null, allTxt is null.
    If allTxt is null, you get a null pointer exception thrown.
    It is thrown because you are trying to use a reference (allTxt) which is pointing to nothing (null), not the assignment of cap.
    Wouldn't I want to check if br.readLine() is null?

Page 1 of 2 12 LastLast

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