cancel
Showing results for 
Search instead for 
Did you mean: 

Java: String issue

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

x

Former Member
0 Kudos

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

Former Member
0 Kudos

>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

Former Member
0 Kudos

i have a if else staement for setting of flag, i hope I cannot use try n catch here

pravesh_verma
Active Contributor
0 Kudos

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

Former Member
0 Kudos

This is the way the code is written except the length caluclation in put in a if, in this code, i ll get a 500 internal error at line 3

int length = wdContext.CurrentV_UI_MiscListElement().getV_UI_ItemText().length();

pravesh_verma
Active Contributor
0 Kudos

Hi,

What is the datatype of attribute: "V_UI_ItemText" ?

I find no reason for Null Pointer exception in this case. Please check the data type and make sure that you are getting the error due to this line only. Or is it something else which is giving this exception.

Regards

Pravesh

Former Member
0 Kudos

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

pravesh_verma
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi,

are you sure that you are getting error at this point. Can you elaborate.

former_member185086
Active Contributor
0 Kudos

Hi

Because current value of CurrentV_UI_MiscListElement() is null.

Best Regards

'Satish Kumar