• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Quetion on RunTime Exception

 
Krishna Tota
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
 
deenbandhu prasad
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 20493
    54
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3381
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic