0

I am having an issue in getting the data in the below loop ,even though the size is not zero i am getting null in the sysout 'data is'.What is wrong there ?

List<Long> dd = domainItemMapper.getIsSearchable(34372);
    System.out.println("the test is-" + dd.size());
    for (int i = 0; i < dd.size(); i++) {
        Long isSearch = dd.get(i);
        System.out.println("data is"+dd.get(i));
        if (isSearch.equals(0)) {
            isSearchValue = false;
        } else
            isSearchValue = true;
    }

The call to database is a mybatis call as below interface

List<Long> getIsSearchable(@Param("parentFieldId") long parentFieldId);

impl

<mapper namespace="com.ge.dbt.common.persistence.IFormValidatorMapper">

        <select id="getIsSearchable" statementType="CALLABLE"
        resultType="Long">
        select is_searchable from t_field where parent_field_id=#{parentFieldId}
    </select>

</mapper>
6
  • 6
    A list with null records.
    – Nishant
    Sep 27, 2012 at 9:09
  • 1
    please provide some code of domainItemMapper.getIsSearchable(34372) Sep 27, 2012 at 9:10
  • 1
    List<Long> is allowed to contain null elements. Apparently getIsSearchable returns such a list.
    – Dims
    Sep 27, 2012 at 9:10
  • Code is edited for getIsSearchable
    – pars
    Sep 27, 2012 at 9:14
  • A table with NULL in is_searchable column.
    – Nishant
    Sep 27, 2012 at 9:15

4 Answers 4

0

I guess your whole code can be converted in to two lines.

if(dd!= null && !dd.isEmpty())
 return dd.contains(0);//Contains Guard you in such cases because equals check
                       //happen on passed element ie *0*

Default value of Long in java is null. So you will need additional check for null in your case.

0

Enclose your isSearch.equals check in a null check

if(isSearch != null)
{
    if (isSearch.equals(0)) 
    {             
        isSearchValue = false;         
    } 
    else             
    {
       isSearchValue = true;
    }
}

However it'll be better to modify code for domainItemMapper.getIsSearchable(34372); method so that it doesn't fill the list with null at all.

0

Seems to be your list contains null literals. And List Supports null as a value.

0

based on your this comment

The data has null,0 and 1 data.I want to return data only for 0 and 1.

You need to fix your query like this

 select is_searchable from t_field where parent_field_id=#{parentFieldId} and is_searchable is not NULL;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.