Java NullPointer Exception
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi guys, i tried this below code
String left=null;
if (left.equals("Kumar"))
System.out.println("Checked");
if ("kumar".equals(left))
System.out.println("Checked");
In the first if condition i am getting nullpointer exception. Can you please tell me why this error occurs.
String left=null;
if (left.equals("Kumar"))
System.out.println("Checked");
if ("kumar".equals(left))
System.out.println("Checked");
In the first if condition i am getting nullpointer exception. Can you please tell me why this error occurs.
To err is human,
To forgive is not company policy
posted 15 years ago
You already know the answer. Think about it
What does the NullPointerException indicate?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
String left=null;
i am getting nullpointer exception
You already know the answer. Think about it
What does the NullPointerException indicate?
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hi guys, thank you but i understand the fact that null cannot be used in the place of an object.
But the equals() method takes as a parameter an object. but in the second if statement i placed null then it must also show nullpointer exception right. why is that case(why that line is alone executing)
But the equals() method takes as a parameter an object. but in the second if statement i placed null then it must also show nullpointer exception right. why is that case(why that line is alone executing)
To err is human,
To forgive is not company policy
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Because in the first case, you are trying to invoke a method on a null object, but in the second you are trying to figure out if your existing object is null.
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
if equals()'s argument is null then, it return false as per the String equals method
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
From java.sun.com...
NullPointerException is thrown when an application attempts to use null in a case where an object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.
NullPointerException is thrown when an application attempts to use null in a case where an object is required. These include:
* Calling the instance method of a null object.
* Accessing or modifying the field of a null object.
* Taking the length of null as if it were an array.
* Accessing or modifying the slots of null as if it were an array.
* Throwing null as if it were a Throwable value.
- Chinna
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Also from the API website: this link implies that the equals method will accept null as an argument.
| A teeny tiny vulgar attempt to get you to buy our stuff The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |








