In this tutorial we are going to talk about "null" reference, In java, A variable is a reference to an object in memory null value indicates an unset reference or in other words a reference to nothing, So you can imagine variables as containers inside which you can put an object of a given type when the variable is null! That means your container is empty. The most common mistake that beginners make is calling methods on the null object that will cause null pointer or not reference Exception, So here I will explain how to deal with null reference exceptions And I will show you an example how to figure out what variable is null how to fix it, So first let's remove all code from the main method if you have done any activity before and let's declare few variables. Now you may notice in console "Exception in thread "main" java.lang.NullPointerException" That means our variable or our container is empty, We don't have a reference to any object, It is also pointing to the line where we are getting that no pointer exception. So if we press on the link (Demo.java: 13) it navigates cursor to line 13. So in this line, we're getting null pointer exception, And now let's print the person body by typing 'System.out.println("person=" +person); Now this will Prints "null" string
So this is the same as typing "String person = null" but we don't have to type it because by default it will be "null", String is not a primitive type, It is object type but with primitives for example with (int) static int lives =0;
We don't have to type equals zero because it is 0 by default. Now if we try to call some method on our persons "String" for example person.toUpperCase(); This will cause Null Pointer exception and you will notice there is a warning ("Result of 'String.toUpperCase()' is ignored"), but it doesn't say anything about null pointer exception, Let's run our code and see that we actually get a null pointer exception. And when we concatenate strings behind it check if the string is "NULL" if that is true then just return the "null" string, So person equals null will be printed on the console if we run our program. Notice how person = null, Now we can initialize a person to some for example a person = "john"; And now we can call methods on that object. So type person = person.toUpperCase(); to assign a new value which is the upper case of our original object, and then we can print the person with S.o.println("person = "+person), And these should print "john" in uppercase. So let's run the code and see the output. You will notice how it prints on all in uppercase "JOHN", now for the "lives" Since it is zero we can print (System.out.println("lives =" +lives ==0);) Now the question? is that true or false? So let's run the code again and see that lives == 0 is true, by default lives is initialized to zero, Now with local variables, we have to define or assign some var local variables need to have a value, and local variables are just variables inside a Method or inside the loop.
//local variables
String anotherPerson = ""; //empty string
COMPLETE CODES:
}
We can declare it without value even if we declare it without value if we try to print it System.out.println("anotherPerson =" +anotherPerson); Now this will cause compilation error because another person is not initialized, in this case "anotherPerson" is a local variable and local variable need to be initialized. We can initialize it to ("") Empty String, for example, Now let's check if our string is null or it is empty so System.out.println("anotherPerson null =" + (anotherPerson == null)); that will cause a warning because it is always "false". We are initializing to the empty string, let's write another line of System.out.println("anotherPerson empty =" +anotherPerson.isEmpty()); Now when we run the code this will print an empty string and then it will print "false" If you will notice how "anotherPerson =" empty string it doesn't print anything If we check if our person is "null" then it print "false" and if we check if our string is empty that means it doesn't contain any characters. That is true but we can initialize a local variable to "null" and now there is a problem because we are trying to call "isEmpty()" on another object, you will also notice that there is a warning ("Method invocation "isEmpty" may produce 'java.lang.NullPointerException'") So if we run the code "null" we will get "null pointer exception" on line '28' But the issue is because we declared our variable to be "null" so if you revert it back to (String anotherPerson ="";)empty, Now let's see how to get rid of null pointer exception and how to debug our code. As you can see calling methods on "null" object will cause "null pointer" or no reference exception It is just the same name for the exception. So how to deal when we get a "null pointer exceptions" Let's consider that we have a variable static string "static String pet;" and it is "null" by default, Now we are trying to convert the value to upper-case, Let's say String dog = pet.toUpperCase(); And now we want to print dog so System.out.println("dogs =" +dog); You may notice that there are no warning's or anything even that "pet" is null, So it is hard to know from the warning because there is no warning, We can't know if something is null, but when we run the code we will get null pointer exception, The first line will usually pointing us to the root of the problem which is then similar to this (Demo.java:32) So in this line something is null and what can be the "null" in the line is the variable that we are trying to use "pet.toUpperCase()"
So we are using a pet variable in this line automatically You can know the pet is "null" But first let's check if it is really "null" So we can print System.out.println("pet is null =" +(pet == null)); So if that prints true it means our "pet" is "null" And that is the problem in our code, Now lets run the code and see if its output "pet is null = true" notice it print's "pet is null = true" in that way we are really sure that our pet variable is null. So the main problem of this is we didn't initialize and we can just initialize to some value for example "pet = "Dog"; And now we will get a warning because Condition 'pet ==null' is always 'false' because it is initialized but we will not get null pointer exception, So let's run the code again and notice that we don't have any Pointer exception. And if you can see pet is null = false and now "dog" prints in Capital letters (dog = DOG) So always, When you have null pointer exception check the line what is in that line and what variable are you using, print every variable in that line and check if it is 'null' That way you can easily get used to null pointer exception, The null pointer exception is one of the most common exceptions that you will get while programming.
So this is the same as typing "String person = null" but we don't have to type it because by default it will be "null", String is not a primitive type, It is object type but with primitives for example with (int) static int lives =0;
We don't have to type equals zero because it is 0 by default. Now if we try to call some method on our persons "String" for example person.toUpperCase(); This will cause Null Pointer exception and you will notice there is a warning ("Result of 'String.toUpperCase()' is ignored"), but it doesn't say anything about null pointer exception, Let's run our code and see that we actually get a null pointer exception. And when we concatenate strings behind it check if the string is "NULL" if that is true then just return the "null" string, So person equals null will be printed on the console if we run our program. Notice how person = null, Now we can initialize a person to some for example a person = "john"; And now we can call methods on that object. So type person = person.toUpperCase(); to assign a new value which is the upper case of our original object, and then we can print the person with S.o.println("person = "+person), And these should print "john" in uppercase. So let's run the code and see the output. You will notice how it prints on all in uppercase "JOHN", now for the "lives" Since it is zero we can print (System.out.println("lives =" +lives ==0);) Now the question? is that true or false? So let's run the code again and see that lives == 0 is true, by default lives is initialized to zero, Now with local variables, we have to define or assign some var local variables need to have a value, and local variables are just variables inside a Method or inside the loop.
//local variables
String anotherPerson = ""; //empty string
COMPLETE CODES:
}
We can declare it without value even if we declare it without value if we try to print it System.out.println("anotherPerson =" +anotherPerson); Now this will cause compilation error because another person is not initialized, in this case "anotherPerson" is a local variable and local variable need to be initialized. We can initialize it to ("") Empty String, for example, Now let's check if our string is null or it is empty so System.out.println("anotherPerson null =" + (anotherPerson == null)); that will cause a warning because it is always "false". We are initializing to the empty string, let's write another line of System.out.println("anotherPerson empty =" +anotherPerson.isEmpty()); Now when we run the code this will print an empty string and then it will print "false" If you will notice how "anotherPerson =" empty string it doesn't print anything If we check if our person is "null" then it print "false" and if we check if our string is empty that means it doesn't contain any characters. That is true but we can initialize a local variable to "null" and now there is a problem because we are trying to call "isEmpty()" on another object, you will also notice that there is a warning ("Method invocation "isEmpty" may produce 'java.lang.NullPointerException'") So if we run the code "null" we will get "null pointer exception" on line '28' But the issue is because we declared our variable to be "null" so if you revert it back to (String anotherPerson ="";)empty, Now let's see how to get rid of null pointer exception and how to debug our code. As you can see calling methods on "null" object will cause "null pointer" or no reference exception It is just the same name for the exception. So how to deal when we get a "null pointer exceptions" Let's consider that we have a variable static string "static String pet;" and it is "null" by default, Now we are trying to convert the value to upper-case, Let's say String dog = pet.toUpperCase(); And now we want to print dog so System.out.println("dogs =" +dog); You may notice that there are no warning's or anything even that "pet" is null, So it is hard to know from the warning because there is no warning, We can't know if something is null, but when we run the code we will get null pointer exception, The first line will usually pointing us to the root of the problem which is then similar to this (Demo.java:32) So in this line something is null and what can be the "null" in the line is the variable that we are trying to use "pet.toUpperCase()"
So we are using a pet variable in this line automatically You can know the pet is "null" But first let's check if it is really "null" So we can print System.out.println("pet is null =" +(pet == null)); So if that prints true it means our "pet" is "null" And that is the problem in our code, Now lets run the code and see if its output "pet is null = true" notice it print's "pet is null = true" in that way we are really sure that our pet variable is null. So the main problem of this is we didn't initialize and we can just initialize to some value for example "pet = "Dog"; And now we will get a warning because Condition 'pet ==null' is always 'false' because it is initialized but we will not get null pointer exception, So let's run the code again and notice that we don't have any Pointer exception. And if you can see pet is null = false and now "dog" prints in Capital letters (dog = DOG) So always, When you have null pointer exception check the line what is in that line and what variable are you using, print every variable in that line and check if it is 'null' That way you can easily get used to null pointer exception, The null pointer exception is one of the most common exceptions that you will get while programming.
0 Comments