Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: What is a Null pointer exception in java programs?

  1. #1
    Java Example is offline Moderator
    Join Date
    Sep 2014
    Posts
    27
    Rep Power
    0

    Default What is a Null pointer exception in java programs?

    In this tutorial, we will discuss about Null pointer exceptions.These are the most common run time exception error that arises during execution of java program. All values in Java programs are references to objects, and all the value in variable is default one. Usually in the case of object references, it is null.In Java, object reference refers to an object of known type, or be null. On making a call to a method

    y.method( );

    there are two things that can be wrong, y refers to a different object or the reference of object y is null. Normally, the latter one is more common.

    Java Code: Null Pointer Exception example
    class Wide {
    
        private int value;
    
        public Wide(int v) {
            value = v;
        }
    
        public int getValue() {
            return value;
        }
    }
    
    public class Large {
    
        private Wide theWide;
    
        public static void main(String[] args) {
            Large inst = new Large();
            System.out.println(inst.theWide.getValue());
        }
    }
    Output on Command Prompt:

    What is a Null pointer exception in java programs?-null-pointer.jpg
    Figure: Output of program with NullPointerException

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: What is a Null pointer exception in java programs?

    All values in Java programs are references to objects,
    What about primitives?

    The code could have some comments saying what variable is null.

    two things that can be wrong, y refers to a different object
    The compiler would show an error if y's class did not have a method named: method()
    Last edited by Norm; 09-27-2014 at 07:53 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: What is a Null pointer exception in java programs?

    Quote Originally Posted by Java Example View Post
    In this tutorial, we will discuss about Null pointer exceptions.These are the most common run time exception error that arises during execution of java program. All values in Java programs are references to objects, and all the value in variable is default one. Usually in the case of object references, it is null.In Java, object reference refers to an object of known type, or be null. On making a call to a method

    y.method( );

    there are two things that can be wrong, y refers to a different object or the reference of object y is null. Normally, the latter one is more common.

    Java Code: Null Pointer Exception example
    class Wide {
    
        private int value;
    
        public Wide(int v) {
            value = v;
        }
    
        public int getValue() {
            return value;
        }
    }
    
    public class Large {
    
        private Wide theWide;
    
        public static void main(String[] args) {
            Large inst = new Large();
            System.out.println(inst.theWide.getValue());
        }
    }
    Output on Command Prompt:

    Click image for larger version. 

Name:	Null pointer.jpg 
Views:	8 
Size:	18.2 KB 
ID:	6594
    Figure: Output of program with NullPointerException
    Really? NPE at line 27 in 22 lines of code?

    db
    Norm likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Null Pointer Exception in java applets...
    By Nivedita123 in forum Java Applets
    Replies: 1
    Last Post: 01-20-2014, 11:31 PM
  2. Null pointer exception
    By DBaskov in forum New To Java
    Replies: 14
    Last Post: 07-10-2011, 11:16 PM
  3. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 10:21 AM
  4. JAVA SOUND API's NULL POINTER EXCEPTION
    By ank64 in forum New To Java
    Replies: 14
    Last Post: 08-10-2010, 11:26 AM
  5. Null pointer exception
    By izzy in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 05:19 PM

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
  •