cancel
Showing results for 
Search instead for 
Did you mean: 

Null Pointer Exception

Former Member
0 Kudos

hi,

I have this problem,

I have the node structure as User with attributes id and name.

now in wdDoInit(),

i have initialised as


 public void wdDoInit()
  {
    //@@begin wdDoInit()
	wdContext.currentUserElement().setId("");
	   wdContext.currentUserElement().setName("");
    //@@end
  }

Now i have a a button which checks if there is any value or not.


 public void onActionsubmit(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionsubmit(ServerEvent)
    
   
    
    msg1 = wdComponentAPI.getMessageManager();
    msg2 = wdComponentAPI.getMessageManager();
    String val1 = wdContext.currentUserElement().getId();
    //String val2 = wdContext.currentUserElement().getName();
    
    int length = val1.length();       
 if(length==0)
    {
    	//msg1.reportContextAttributeMessage(wdContext.currentContextElement(),attr,IMessageCompNullpoint.ID,new Object[]{"id"},true);
    	msg1.raiseMessage(IMessageCompNullpoint.ID,new Object[] {""},true);
    }
    //}
    //@@end
  }

If in firsttime, i click the submit button, then error is displayed " Enter id"

Then i submit both the values and press "Submit", it works,

Now when i remove the data from the "id" attribute field, and press submit it gives me null pointer exception.

I have tried using if(firstTime)

{}

else

{

//created a method for checking whether the input field is empty or not, by using length.

}

But with no help.

Can anyone please help me.

regards,

pinki

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Instead of

 int length=val1.length();

try following:

int length = (val1 == null) ? 0 : val1.length();

Regards,

Gopal

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Pinki ,

Observe the difference here :

Case 1) No error for below

String s ="";

wdComponentAPI.getMessageManager().reportSuccess(s.length());

Case 2) Runtime error

s=null;

wdComponentAPI.getMessageManager().reportSuccess(s.length());

That means , if you assign null to string then , u should not try to get the length , it will throw null pointer exception.

So if you want to check , empty or not ...

instead of this code...

int length = val1.length();

if(length==0)

{

//msg1.reportContextAttributeMessage(wdContext.currentContextElement(),attr,IMessageCompNullpoint.ID,new Object[]{"id"},true);

msg1.raiseMessage(IMessageCompNullpoint.ID,new Object[] {""},true);

}

In your case u remove data means you are setting its value to null , if so ..use the following code

if (s==null || s.equals(""))

{

}

or use the Gopal's code ...its very simple.

Also test what happens and why it won't work if you write the code............

if( s.equals("") || s==null)

{

}

xxxxxxxxxxxxxxxxx

Thanks ,

Srini

Edited by: Armin Reichert on Mar 28, 2008 5:21 PM

Former Member
0 Kudos

I friend

You have to check the field of the next way:

I** FIELD VALUE

Object attributeValue = this.wdContext.currentContextElement().getAttributeValue(fieldName);

if (attributeValue instanceof String) {

if (((String) attributeValue).length() == 0) {

    • RAISE AN ERROR

}

}

I hope helps you

Regards me, if helps you

Joshua