This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Quetion on RunTime Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Quetion on RunTime Exception" Watch "Quetion on RunTime Exception" New topic
Author

Quetion on RunTime Exception

Krishna Tota
Ranch Hand

Joined: Mar 22, 2008
Posts: 40
I know some of runtime exceptions like:

NullpointerException,
ArrayIndexOutOfBoundsException,
ArthematicException,
.
.
.
.
.
etc.

I have one quetion, Can any one give the solution for that.

My quetion is "What is ment by NullPointerException" ?

Please give the answer in a sentence .When this exception will occur with example.

Thank you,
Krishna
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11356
    
  16

if you have a reference that has never been assigned to an actual object, then try to call what would be a method on that object, you'll get a NPE.

example:

String myString;

myString.charAt(3);


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14194
    
  20

Have a look at the API documentation for class NullPointerException.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
deenbandhu prasad
Greenhorn

Joined: Feb 21, 2008
Posts: 9
nullpointerexception occurs when an object reference variable is used to invoke the method on object before the object is being instantiated. eg.

String str = new String ("test"); //instantiated
str.lenght();//valid

String str = null;
str.lenght();// NullpointerException
Krishna Tota
Ranch Hand

Joined: Mar 22, 2008
Posts: 40
Hello friens,

NullPointerException is the one that can occar any where in a program.

Please explain with another example in brief not with object reference.

Thanks,
Krishna.
Christopher Young
Ranch Hand

Joined: Nov 02, 2007
Posts: 63
What do you mean?

NullPointerExceptions happen when you try to reference an object that hasn't been instantiated.

And a lot of exceptions can occur anywhere in a program if something messes up (Such as ArrayIndex Out of bounds) due to user error or programmer error.

Are you looking for ways to keep the program running when exceptions occur? Use try-catch.


Technology can never substitute for knowledge.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Christopher Young is correct; the NPE (NullPointerException) is the most misunderstood exception of all, but it has definitely got to do with object references. Various ways to get an NPE:
  • Declare an object and forget to instantiate it (probably the commonest).
  • Mistakenly set an object to null.
  • Forgetting that null values are permissible (eg in a tree, or when closing a Reader). That is the one situation where if (x != null) . . . is appropriate.
  • Mistakenly passing null as a parameter.
  • Forgetting there are some methods (eg for getting members from the Queue interface) which permit null returns.
  • Losing a reference; I once did it by setting a field to transient and not reinitialising it after serialization.
  • Passing null to an operator (eg throw null).
  • Throwing an NPE somewhere (unusual).
  • There are bound to be more. In 99% of instances, sorting out an NPE means correcting an error in the code. Using try-catch or too many if (x != null) . . .s is liable to obscure the cause of the error.
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19697
        
      20

    Originally posted by Campbell Ritchie:
    Throwing an NPE somewhere (unusual).

    Actually it's quite usual, for checking parameters. Sometimes you don't need the value at the time of calling the parameter, but it is stored and you use it in a later method. You don't want it failing at that time because it'll be harder to find.

    In fact, there are 307 classes in the java and javax packages that explicitly throw a NPE.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by Rob Prime:


    In fact, there are 307 classes in the java and javax packages that explicitly throw a NPE.


    That's a great statistic info


    Everything has got its own deadline including one's EGO!
    [CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
    Raghavan Muthu
    Ranch Hand

    Joined: Apr 20, 2006
    Posts: 3344

    Originally posted by Krishna Tota:


    My quetion is "What is ment by NullPointerException" ?

    Please give the answer in a sentence .When this exception will occur with example.


    As we go by dissecting the term "Null-Pointer-Exception", you will get a correct meaning. of course, it is one of the beautiful features of Java - all classes are meaningful.

    In one line as you asked,


    "An exception occurred when trying to do an operation through a pointer to an object which is pointing nowhere in reality - i.e., null in programmatic terms"


    I guess fred rosenberger's example is the best one for this case!

    Hope it clears
     
    GeeCON Prague 2014
     
    subject: Quetion on RunTime Exception