Data loading with JSF

Hi

I have a link when I click on that link I am opening a popup in that popup I have displayed the text fromDatabase.

But when first time I click on link I am getting error asNullpointerException.

But second time when I click on link I am able to displaytext from Database in that popup.

The popup i.e JSP I am opening on link click contains code as,

<%

Beanbean=(Bean)session.getAttribute("bean name from faces-config");

Collection col= bean.getmethod().getB2bErrorTxnCollection();

%>

Very first time I am getting Nullpointer Exception i.e. Mybean is not loaded uptill that time.

But second time my bean gets loaded & I am able to display text.

Why this happening ?

Thanks

Sandip

[830 byte] By [sandip_03_jsfa] at [2007-11-26 15:08:03]
# 1

What's happening? Well, a NullPointerException means that some object is not instantiated yet (object == null) ..

So in this case the bean or even the result of getMethod() might be null. Just do a nullcheck and instantiate the object where needed. You can do this using the new keyword.

For example:if (object == null) {

object = new Object();

}

This is basic Java knowledge, do you know?

BalusCa at 2007-7-8 8:58:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
I have checked the null condition also.I think I need to write what exactly I am needed .Actually what happening might be session scope my bean is not loading first time .So is the any way sothat bean gets loaded as soon as I click on link ?Sandip
sandip_03_jsfa at 2007-7-8 8:58:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

Well, like I explained, just add a null check and instantiate the object if needed.

<%

Bean bean = (Bean) session.getAttribute("bean name from faces-config");

if (bean == null) {

bean = new Bean();

}

Collection col = bean.getmethod().getB2bErrorTxnCollection();

%>

BalusCa at 2007-7-8 8:58:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thanks a lotactually I tried that way already .i.e checking null condition.Still error there.Is this related to any scope i.e. seesion,application ?Sandip
sandip_03_jsfa at 2007-7-8 8:58:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Maybe the getMethod() returns null? Just run in debug mode and see when and where the NullPointerException occurs.
BalusCa at 2007-7-8 8:58:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hi BalusC

I agree that getMethod() returns null

but only first time link click its returning null second time link click its not retuning null

Ok let me write my code here

A.jsp

<t:commandLink action="#{bean.viewerror}" onclick="window.open('B.faces',100,100)"

Bean.java

so my viewerror() method is,

public void viewerror() {

lot = (B2bLotCanonicalV) getErrorRecordsDataTable().getRowData();

Collection col=lot.getB2bErrorTxnCollection();

FacesContext facesContext = FacesContext.getCurrentInstance();

HttpSession session=(HttpSession)facesContext.getExternalContext().getSession(true);

}

}

and B.jsp i m writing as,

><%

Bean bean=(Bean)session.getAttribute("bean name from faces-config");

Collection mycol= bean.getB2bLotCanonical_V().getB2bErrorTxnCollection();

%>

Sandip

sandip_03_jsfa at 2007-7-8 8:58:29 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...