How to check null for getter methods in java

I have a POJO class.

class Activity{ private String name; public String getName() return name; } public void setName(String name){ this.name=name; } } 

im having belw.it conditions that do not comply with the rule in these conditions, respectively

  if(stlmtTransRequestVO.getStlmtTransId()!=null && stlmtTransRequestVO.getPaymentTransId()!=null){ stlmtTransDtlsList = (List<StlmtTransResponseVO>) queryForList( "GET_STLMTPAY_TRANSACTIONS", stlmtTransRequestVO); }else if(stlmtTransRequestVO.getAgentId()!=null && stlmtTransRequestVO.getAgencyId()==null){ stlmtTransDtlsList = (List<StlmtTransResponseVO>) queryForList( "GET_AGENT_TRANSACTIONS", stlmtTransRequestVO); }else if(stlmtTransRequestVO.getAgencyId()!=null && stlmtTransRequestVO.getAgentId()==null){ stlmtTransDtlsList = (List<StlmtTransResponseVO>) queryForList( "GET_AGENCY_TRANSACTIONS", stlmtTransRequestVO); }else if(stlmtTransRequestVO.getAgencyId()!=null && stlmtTransRequestVO.getAgentId()!=null){ } 

How to check this getter method for data or not?

I tried the scripts below but didn't work

  1) obj.getName()!=null 2) obj.getName().isEmpty() 
+4
source share
6 answers
 if( obj.getName() != null && !"".equals(obj.getName()) ){ //getName is valid } 

The above checks that this name is not null , nor is it empty . Also, ". Equals (obj.getName ()) is considered a better approach than obj.getName (). Equals (" ") .

+5
source

obj.getName() != null is correct. But it depends on your definition of "data availability." It could be: object.getName() != null && !obj.getName().isEmpty() .

There are utilities to facilitate this, for example apache commons-lang StringUtils.isNotEmpty(..)

+10
source

Initialize as follows:

 private String name = ""; 

Then you can check this with:

 obj.getName().isEmpty(); 
+2
source

Your first check is correct. Change your second check as follows:

 obj.getName().equals(""); 
+1
source

The name is a String object to check if the data has done it:

 obj.getName() != null; obj.getName.equals(""); 
+1
source

Always use StringUtils.isEmpty in case of string

+1
source

All Articles