Results 1 to 9 of 9
  1. #1
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default NULL- the great word

    Sometimes a nullpointerexception is thrown, in some cases null is easily printed as a value of a variable without an exception being thrown.

    What is this?

  2. #2
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: NULL- the great word

    If you have an object that is null and call a method on that object the Exception is thrown.
    As long as you do not call a method on the object there will be no problem.

    Example:
    Object o = null;
    System.out.println(o); // works
    o.wait(); // Throws Exception

  3. #3
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: NULL- the great word

    Ok.

    Is it a keyword in Java?

  4. #4
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    Default Re: NULL- the great word

    It is a keyword, used to show that something does not exist.

    So if we have an Object o that is "null", than it means that o has been defined but is no object is referenced with it.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: NULL- the great word

    Quote Originally Posted by Tobias Kürbis View Post
    If you have an object that is null and call a method on that object the Exception is thrown.
    As long as you do not call a method on the object there will be no problem.

    Example:
    Object o = null;
    System.out.println(o); // works
    o.wait(); // Throws Exception
    That depends what the method in question does with it. This example still produces a NPE:
    Java Code:
    Object o = null;
    printObject(o);
    ...
    public void printObject(Object x) {
       System.out.println(x.toString());
    }
    In this case I know it's going to be bad, but Writers/Readers/InputStreams/OutputStreams will not be happy when you feed them null.


    Quote Originally Posted by jmohandos304 View Post
    Ok.
    Is it a keyword in Java?
    Technically it's a literal: Chapter*3.*Lexical Structure
    And: http://docs.oracle.com/javase/specs/...4.html#jls-4.1
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: NULL- the great word

    If an object needs to printed, the String presentation of that object is always obtained by the static method String.valueOf(Object o) method; that method is responsible for the "null" representation of a null value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    jmohandos304 is offline Senior Member
    Join Date
    Apr 2014
    Posts
    122
    Rep Power
    0

    Default Re: NULL- the great word

    That depends what the method in question does with it. This example still produces a NPE:

    As per what Tobias Kürbis said you get a NPE when you invoke a method on null object. Even your example does the same and throws a NPE. So how is your example different? Please explain

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: NULL- the great word

    Tobias' example of:
    Java Code:
    System.out.println(o); // works
    only works because, as Jos points out, the println(Object) method calls String.valueOf(Object), which checks for null.
    So a method is never actually called on the null reference.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,094
    Rep Power
    4

    Default Re: NULL- the great word

    Quote Originally Posted by jmohandos304 View Post
    That depends what the method in question does with it. This example still produces a NPE:

    As per what Tobias Kürbis said you get a NPE when you invoke a method on null object. Even your example does the same and throws a NPE. So how is your example different? Please explain
    What I was trying to make clear is that using the "null-object" as parameter is not a guarantee that you won't get a NPE. It all depends on what the method does with it. String.valueOf(o) does a check, that's why it doesn't fail:
    Java Code:
        public static String valueOf(Object obj) {
    	return (obj == null) ? "null" : obj.toString();
        }
    Other classes will fail hopelessly when passing null as parameter.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2™ © 2013

Similar Threads

  1. Replies: 7
    Last Post: 10-11-2013, 01:34 AM
  2. How to define what a word is for a word count?
    By Fluxtime in forum New To Java
    Replies: 1
    Last Post: 12-03-2012, 11:59 AM
  3. Replies: 3
    Last Post: 08-30-2011, 05:44 PM
  4. Replies: 6
    Last Post: 10-18-2010, 11:40 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
  •