cancel
Showing results for 
Search instead for 
Did you mean: 

Business method on EJB

Former Member
0 Kudos

Hi all,

I have this business method into my EJB, than I'm using a web service in order to view the table records from a Web Dynpro. But I'm not able to see <i>result</i> array (see code). I think all is correct, have you got any idea on the possible cause? I'm sure that the data are retrived from this method. Thanks for your help.

/**

  • Business Method.

*/

public ModelBridge[] viewActiveModel() throws ModelException {

ArrayList modellings = new ArrayList();

try {

Collection active = modelHome.findByStatus(HelperClasses.STATUS_ACTIVE);

for (Iterator iterator = active.iterator(); iterator.hasNext();) {

modellings.add(getModelBridge((ModelloEntityLocal) iterator.next()));

}

} catch (FinderException e){

e.printStackTrace();

throw new ModelException(e.getMessage());

}

ModelBridge[] result = new ModelBridge[modellings.size()];

modellings.toArray(result);

return result;

}

Vito P.

Accepted Solutions (0)

Answers (4)

Answers (4)

Former Member
0 Kudos

Hi,

I guess it works if you write:

ModelBridge[] result = new ModelBridge[modellings.size()];
result = modellings.toArray(result);
return result;

or better

return (ModelBridge[] )modellings.toArray();

If you check the API of ArrayList.toArray(...) you will understand the cause of the problem.

Regards, Astrid

Message was edited by: Astrid Krause

Former Member
0 Kudos

Astrid,

your solution generate an exception... it doesn't works.

public void viewActiveModel() {

try {

ActiveModelList = modelloSessionLocal.viewActiveModel();

}catch (

Exception e2

) {

e2.printStackTrace();

}

}

-

-


public ModelBridge[] viewActiveModel() throws ModelException {

ArrayList modellings = new ArrayList();

try {

Collection active = modelHome.findByStatus(HelperClasses.STATUS_ACTIVE);

for (Iterator iterator = active.iterator(); iterator.hasNext();) {

modellings.add(getModelBridge((ModelloEntityLocal) iterator.next()));

}

} catch (FinderException e){

e.printStackTrace();

throw new ModelException(e.getMessage());

}

ModelBridge[] result = new ModelBridge[modellings.size()];

//Result è un array di Model Bridge che contiene tutti gli elementi dell'ArrayList

//modellings.toArray(result);

return (ModelBridge[] )modellings.toArray();

//return result;

}

Any ideas? thk.

Vito

Former Member
0 Kudos

Vito,

what type of exception, stacktrace available ?

Regards, astrid

Former Member
0 Kudos

I have two exception, the first is:

java.lang.ClassCastException, on this code:

public MyCommandBean() throws CreateException {

// looks up the session bean and creates the Home interface

try {

InitialContext ctx = new InitialContext();

home = (ModelloSessionLocalHome)ctx.lookup("localejbs/MySessionBean");

modelloSessionLocal = home.create();

} catch (Exception namingException) {

namingException.printStackTrace();

}

}

the second is java.lang.NullPointerException on this code:

public Collection viewAll() {

Collection bonuses = new ArrayList();

try {

Collection active = modelHome.findAll();

ModelloEntityLocal currModelloEntityLocal;

for (Iterator iterator = active.iterator(); iterator.hasNext();) {

currModelloEntityLocal = (ModelloEntityLocal) iterator.next();

if (currModelloEntityLocal!=null){

bonuses.add(getModelBridge(currModelloEntityLocal));

}

}

} catch (FinderException e) {

e.printStackTrace();

}

return bonuses;

Former Member
0 Kudos

hello vito,

could you post the whole codes?

for the exceptions:

> java.lang.ClassCastException, on this code:


home = (ModelloSessionLocalHome)ctx.lookup("localejbs/MySessionBean");

what is mapped as your localejbs/MySessionBean in the

descriptor?

> the second is java.lang.NullPointerException on this

> code:

Collection active = modelHome.findAll();

it is either your modelHome is null or the method

modelHome.findAll returned a null object. might be

caused by the prior exception.

regards

jo

Former Member
0 Kudos

Jo, consideration on these code lines:

<i>

private ModelloEntityLocalHome modelHome;

....

public Collection viewActiveModel() throws ModelException {

Collection bonuses = new ArrayList();

try {

Collection active = modelHome.findByStatus();

ModelloEntityLocal currBonusLocal;

for (Iterator iterator = active.iterator(); iterator.hasNext();) {

currBonusLocal = (ModelloEntityLocal) iterator.next();

if (currBonusLocal!=null){

bonuses.add(getModelBridge(currBonusLocal));

}

}

} catch (FinderException e) {

e.printStackTrace();

}

return bonuses;

}</i>

findByStatus method use the EJB QL statement: "select Object(b) from ModelloEntity b"

EntityBean are:

ModelloEntity

ModelloEntityBean

ModelloEntityHome

ModelloEntityLocal

ModelloEntityLocalHome

SessionBean are:

ModelloSession

ModelloSessionBean

ModelloSessionHome

ModelloSessionLocal

ModelloSessionLocalHome

What means that modelHome is null?

Former Member
0 Kudos

hello vito,

>What means that modelHome is null?

in your code:

try {
Collection active = modelHome.findAll();
ModelloEntityLocal currModelloEntityLocal;
for (Iterator iterator = active.iterator(); iterator.hasNext();) {
currModelloEntityLocal = (ModelloEntityLocal) iterator.next();
if (currModelloEntityLocal!=null){
bonuses.add(getModelBridge(currModelloEntityLocal));
}
}
} catch (FinderException e) {
e.printStackTrace();
}

there are only 2 places that will cause the

NullPointerException.

one is modelHome.findAll() method and second is

active.iterator().

however, this is assuming that the exception was

caught in this method and not inside other called

methods like getModelBridge.

jg

former_member184385
Active Participant
0 Kudos

Hi Vito,

just a general remark:

- do you really need to work with EJBs and WSes just to get few records from a db to your WD app?

- starting with WD, i got the impression, i have to create web services for all data access, though i kind of back of from the idea to create my own DACBs (data access command beans)

- but creating DACBs is just some work and not difficult at all

Think about it.

Regards

Gregor

Former Member
0 Kudos

Hi Vito,

maybe there are attributes with "_" like "cust_id" in your ModelBridge class.

In my implementations this attributes doesn`t work with web services.

Karsten

Former Member
0 Kudos

hello vito,

i could see no problem with the coding...

try adding a check on you Collection instance

and see what you get.

<b>if(active.isEmpty())</b>

System.out.println("No data retrieved!");

regards

jo