on 03-06-2009 9:03 AM
Hi Everyone,
I am getting a 500 internal error "a Null pointer exception" at ".getV_UI_ItemText()" in the beow line.
wdContext
CurrentV_UI_MiscListElement()
.getV_UI_ItemText()
.length()
!= 0) {
Sometimes user might enter a data into this string or not but based on this I need to set some flag. but when i access .getV_UI_ItemText() I am getting 500 internal error.
Please help me how to handle this case?
Thanks,
Raj
x
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If you get a NPE at this line, this means that either
- wdContext.currentV_UI_MiscListElement(), or
- wdContext.currentV_UI_MiscListElement().getV_UI_ItemText() is NULL.
Just check this. Catching the NPE is bad programming style.
Armin
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
>try {
>
> if (wdContext.CurrentV_UI_MiscListElement().getV_UI_ItemText().length()> != 0)
>
> // do something
>
> } catch(NullPointerException ne) { // Catch Null pointer Exception & how you want to handle it
> // e.g, set flag
> }
When user doesn't enter data into given string, It'll go to catch block and just check if you can set a flag there!
Edited by: Anagha Jawalekar on Mar 6, 2009 2:52 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kandaraj,
Yes you are right!! You should always check for the null pointer exception seperately in the code. Checking the null pointer excpetionusing the try and catch block is not a good way of handling the null pointer. Instead use a simple If else loop.
Use this code:
if (wdContext.CurrentV_UI_MiscListElement() != null) {
if (wdContext.CurrentV_UI_MiscListElement().getV_UI_ItemText()!= null) {
int length = wdContext.CurrentV_UI_MiscListElement().getV_UI_ItemText().length();
}
}
I hope this solves you issue.
Thanks and Regards
Pravesh
Nopes, I dont think you can avoid exception handling, if this is what your doubt is.... You'll need to use try catch as you definately need to handle the null pointer exception. You can use the else condition in the try block as well but it will never go there in case of a Null pointer so will be of no use effectively.
The null pointer will be thrown as soon as you perform the ".getV_UI_ItemText()" operation, while you are evaluating the if condition. It will ideally not go to the "else" block but throw the NullPointerexception and end the program abruptly.
So, you must catch this exception and set the flag in the catch block, because this is where the control will go once exception has occurred.
Edited by: Anagha Jawalekar on Mar 6, 2009 4:21 PM
Hi,
That is not true as Raj said: "I ll get a 500 internal error at line 3", which means that he is gettig the exception due to the calculation of length().
If that would have been the case that it is giving error at:".getV_UI_ItemText()" then it should have given erro at line 2 itself. But it has gone inside the if loop, which clearly shows that the errornous line is line 3.
However Raj, you can do one thing insise the final if loop, just before the calculation of the length try and put the try catch of that solves the issue. However I am bit doubtful, whether that will work or not.
Just give it a try!!
Thanks and Regards
Pravesh
Hi,
are you sure that you are getting error at this point. Can you elaborate.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
92 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.