java.lang.NullPointerException
Thrown when an application attempts to use null in a case where an object is required.
These include:
1. Calling the instance method of a null object.
2. Accessing or modifying the field of a null object.
3. Taking the length of null as if it were an array.
4. Accessing or modifying the slots of null as if it were an array.
5. Throwing null as if it were a Throwable value.
Implemented Interfaces:
Serializable
Declaration
public class NullPointerException extends RuntimeException {@java.io.Serialprivate static final long serialVersionUID = 5162710183389028792L;public NullPointerException() {super();}public NullPointerException(String s) {super(s);}private transient int extendedMessageState;private transient String extendedMessage;public synchronized Throwable fillInStackTrace() {// If the stack trace is changed the extended NPE algorithm// will compute a wrong message. So compute it beforehand.if (extendedMessageState == 0) {extendedMessageState = 1;} else if (extendedMessageState == 1) {extendedMessage = getExtendedNPEMessage();extendedMessageState = 2;}return super.fillInStackTrace();}public String getMessage() {String message = super.getMessage();if (message == null) {synchronized (this) {if (extendedMessageState == 1) {// Only the original stack trace was filled in. Message will// compute correctly.extendedMessage = getExtendedNPEMessage();extendedMessageState = 2;}return extendedMessage;}}return message;}private native String getExtendedNPEMessage();}
Methods
1. NullPointerException()
java.lang.NullPointerException.NullPointerException()
Constructs a NullPointerException with no detailed message.
Approach
Java
public class NullPointerException1 {public static void main(String[] args) {NullPointerException nullPointerException =new NullPointerException();System.out.println(nullPointerException);}}
Output:
java.lang.NullPointerException
2. NullPointerException(String s)
java.lang.NullPointerException.NullPointerException(String s)
Constructs a NullPointerException with the specified detail message.
Parameters: s the detail message.
Approach
Java
public class NullPointerException2 {public static void main(String[] args) {String s = "Hello";NullPointerException nullPointerException =new NullPointerException(s);System.out.println(nullPointerException);}}
Output:
java.lang.NullPointerException: Hello
3. getMessage()
String java.lang.NullPointerException.getMessage()
Returns the detailed message string of this throwable.
If a non-null message was supplied in a constructor it is returned. Otherwise, an implementation-specific message or null is returned.
Returns: the detail message string, which may be null.
Approach
Java
public class NullPointerException2 {public static void main(String[] args) {String s = "Hello";NullPointerException nullPointerException =new NullPointerException(s);System.out.println(nullPointerException.getMessage());}}
Output:
Hello
4. fillInStackTrace()
Throwable java.lang.NullPointerException.fillInStackTrace()
Fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread.
If the stack trace of this Throwable is not writable, calling this method has no effect.
Returns: a reference to this Throwable instance.
Approach
Java
public class NullPointerException2 {public static void main(String[] args) {String s = "Hello";NullPointerException nullPointerException =new NullPointerException(s);System.out.println(nullPointerException.fillInStackTrace());}}
Output:
java.lang.NullPointerException: Hello
Example:
Approach
Java
package com.NullPointerException;public class NullPointerExceptionExample {@SuppressWarnings("null")public static void main(String[] args) {String str = null;System.out.println(str.charAt(0));}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.charAt(int)" because "str" is null at com.NullPointerException.NullPointerExceptionExample.main(NullPointerExceptionExample.java:9)
Some other Errors/Exceptions
AbstractMethodError: Thrown when an application tries to call an abstract method.
ArithmeticException: Thrown when an exceptional arithmetic condition has occurred.
ArrayIndexOutOfBoundsException: Thrown to indicate that an array has been accessed with an illegal index.
ArrayStoreException: Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects.
AssertionError: Thrown to indicate that an assertion has failed.
BootstrapMethodError: Thrown to indicate that an invoke dynamic instruction or a dynamic constant failed to resolve its bootstrap method and arguments, or invoke dynamic instruction the bootstrap method has failed to provide a call site with a target of the correct method type, or for a dynamic constant, the bootstrap method has failed to provide a constant value of the required type.
ClassCastException: Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.
ClassCircularityError: Thrown when the Java Virtual Machine detects a circularity in the superclass hierarchy of a class is loaded.
ClassFormatError: Thrown when the Java Virtual Machine attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file.
ClassNotFoundException: Thrown when an application tries to load in a class through its string name using: The forName method in class Class. The findSystemClass method in class ClassLoader. The loadClass method in class ClassLoader.
CloneNotSupportedException: Thrown to indicate that the clone method in class Object has been called to clone an object, but the object's class does not implement the Cloneable interface.
Error: An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.
Exception: The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
ExceptionInInitializerError: Signals that an unexpected exception has occurred in a static initializer.
IllegalAccessError: Thrown if an application attempts to access or modify a field, or to call a method that it does not have access to.
IllegalAccessException: An IllegalAccessException is thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.
IllegalArgumentException: Thrown to indicate that a method has been passed an illegal or inappropriate argument.
IllegalCallerException: Thrown to indicate that a method has been called by an inappropriate caller.
IllegalMonitorStateException: Thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object monitor without owning the specified monitor.
IllegalStateException: Signals that a method has been invoked at an illegal or inappropriate time.
IllegalThreadStateException: Thrown to indicate that a thread is not in an appropriate state for the requested operation.
IncompatibleClassChangeError: Thrown when an incompatible class change has occurred to some class definition.
IndexOutOfBoundsException: Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.
InstantiationError: Thrown when an application tries to use the Java new construct to instantiate an abstract class or an interface.
InstantiationException: Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated.
InternalError: This is thrown to indicate some unexpected internal error has occurred in the Java Virtual Machine.
InterruptedException: Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity.
LayerInstantiationException: Thrown when creating a module layer fails.
LinkageError: Subclasses of LinkageError indicate that a class has some dependency on another class; however, the latter class has incompatibly changed after the compilation of the former class.
NegativeArraySizeException: Thrown if an application tries to create an array with a negative size.
NoClassDefFoundError: Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression)and no definition of the class could be found.
NoSuchFieldError: Thrown if an application tries to access or modify a specified field of an object, and that object no longer has that field.
NoSuchFieldException: Signals that the class doesn't have a field of a specified name.
NoSuchMethodError: Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.
NoSuchMethodException: Thrown when a particular method cannot be found.
NullPointerException: Thrown when an application attempts to use null in a case where an object is required.
NumberFormatException: Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
OutOfMemoryError: Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
ReflectiveOperationException: Common superclass of exceptions thrown by reflective operations in core reflection.
RuntimeException: RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
SecurityException: Thrown by the security manager to indicate a security violation.
StackOverflowError: Thrown when a stack overflow occurs because an application recurses too deeply.
StringIndexOutOfBoundsException: Thrown by String methods to indicate that an index is either negative or greater than the size of the string.
TypeNotPresentException: Thrown when an application tries to access a type using a string representing the type's name, but no definition for the type with the specified name can be found.
UnknownError: Thrown when an unknown but serious exception has occurred in theJava Virtual Machine.
UnsatisfiedLinkError: Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native.
UnsupportedClassVersionError: Thrown when the Java Virtual Machine attempts to read a class file and determines that the major and minor version numbers in the file are not supported.
UnsupportedOperationException: Thrown to indicate that the requested operation is not supported.
VerifyError: Thrown when the "verifier" detects that a class file, though well-formed, contains some sort of internal inconsistency or security problem.
VirtualMachineError: This indicates that the Java Virtual Machine is broken or has run out of resources necessary for it to continue operating.
No comments:
Post a Comment