This exception is thrown when we try to access an element/object which is not present at all.
Access an element/object which is not present at all????? Not present???? where it is not present???
Well, we associate an index with a list,array or a vector. We use this index (index variable) to access directly a particular element in the array or a particular object in the list.
Remember?
...
int len=somestring.length()+1;
for(int i=0;i<=len;i++)
{
...
System.out.println("char="+somestring.charAt(i));
}
...
i is an index variable or the index for the string object which we use to get each character in the string, like wise for a list and a vector.
Now the value of this index is limited up to the length of the string or the list which we use.
And when we try to assign a value to this index(programatically) which is out of this length,i.e. OutOfBound then the jvm throws this exception.The bound is the length of the string object or the list or the vector.
In the above example we assign the length of the string to an int variable and add 1 to it and iterate through the loop for length + 1 times.The loop will print the characters in the string up to the length of the string but when it comes for the last iteration it will find that there is no element at the specified index because there is no such index at all in the given string and hence the exception.
Quote
Only he who has seen all the failure will get all the success.
Wednesday, August 20, 2008
Saturday, August 2, 2008
java.lang.NullPointerException
NullPointerException occurs because the program is trying to access a method or a field on null. Now what is a null, although the explanation of null may go beyond the scope of this blog but for the topic purpose we may say that any object which does not contain any memory reference is null. Object doesn't have any memory reference-the cause of this may be:
1] Object not initialised using the new operator.
2] Object may somewhere in the program have been assigned a null.
3] An unitialised array being subscripted, i.e. indexed.
For e.g.
Example for 1st cause:
A1] String str=null;
A2] str.equals("somestring");----------->here you will get NullPointerException because str has not been initialised
Example for 2nd cause:
B1] String str=new String("somestring");
B2] String str1=null;
B3] str=str1;
B4] str.equals("somestring");------------->here you will get NullPointerException because str no more contains any object reference though it was initialised to one(B1),it is assigned what is contained in str1(B3) and str1 is null(B2)
Example for 3rd cause:
C1] String str[]=new String[3];
C2] str=null;
C3] str[0]="somestring";---------------->here you will get NullPointerException because str is an array of strings which is not yet initialised and hence there is no memory for any of the elements and we are trying to assign a string to such an element of the string.
1] Object not initialised using the new operator.
2] Object may somewhere in the program have been assigned a null.
3] An unitialised array being subscripted, i.e. indexed.
For e.g.
Example for 1st cause:
A1] String str=null;
A2] str.equals("somestring");----------->here you will get NullPointerException because str has not been initialised
Example for 2nd cause:
B1] String str=new String("somestring");
B2] String str1=null;
B3] str=str1;
B4] str.equals("somestring");------------->here you will get NullPointerException because str no more contains any object reference though it was initialised to one(B1),it is assigned what is contained in str1(B3) and str1 is null(B2)
Example for 3rd cause:
C1] String str[]=new String[3];
C2] str=null;
C3] str[0]="somestring";---------------->here you will get NullPointerException because str is an array of strings which is not yet initialised and hence there is no memory for any of the elements and we are trying to assign a string to such an element of the string.
Subscribe to:
Posts (Atom)