Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

NullPointerException from a method in an EJB

Hello, experts,

I get a NullPointerException when I call method "save" and "view" from an EJB.

Please help me!Here is my code :

package com.sap.examples.telbookentry;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Date;

import java.util.Iterator;

import javax.ejb.CreateException;

import javax.ejb.FinderException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import com.sap.engine.examples.util.TelBookException;

import com.sap.engine.examples.util.TelBookModel;

/**

  • @ejbHome <{com.sap.examples.telbookentry.TelBookEntryProcessorHome}>

  • @ejbLocal <{com.sap.examples.telbookentry.TelBookEntryProcessorLocal}>

  • @ejbLocalHome <{com.sap.examples.telbookentry.TelBookEntryProcessorLocalHome}>

  • @ejbRemote <{com.sap.examples.telbookentry.TelBookEntryProcessor}>

  • @stateless

  • @transactionType Container

*/

public class TelBookEntryProcessorBean implements SessionBean {

private TelBookEntryLocalHome entryHome;

public void ejbRemove() {

}

public void ejbActivate() {

}

public void ejbPassivate() {

}

public void setSessionContext(SessionContext context) {

myContext = context;

}

private SessionContext myContext;

/**

  • Business Method.

*/

public TelBookModel save(String name, String telnumber) throws TelBookException {

TelBookEntryLocal entry = null;

java.lang.String Id = generateId();

try {

entry =

entryHome.create(

Id, name, telnumber

);

} catch (CreateException e) {

e.printStackTrace();

throw new TelBookException(e.getMessage());

}

return getTelBookModel(entry);

}

/**

  • Business Method.

*/

public TelBookModel delete(String name, String telnumber) {

// TODO : Implement

return null;

}

/**

  • Business Method.

*/

public TelBookModel[] view() throws TelBookException {

ArrayList entries = new ArrayList();

try {

Collection present = entryHome.findMethod();

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

entries.add(

getTelBookModel((TelBookEntryLocal) iterator.next()));

}

} catch (FinderException e) {

e.printStackTrace();

throw new TelBookException(e.getMessage());

}

TelBookModel[] result = new TelBookModel[entries.size()];

entries.toArray(result);

return result;

}

/**

  • Business Method.

*/

public TelBookModel change() {

// TODO : Implement

return null;

}

/**

  • Create Method.

*/

public void ejbCreate() throws CreateException {

try {

Context ctx = new InitialContext();

entryHome =

(TelBookEntryLocalHome) ctx.lookup(

"java:comp/env/TelBookEntry/TelBookEntryBean");

} catch (NamingException e) {

throw new CreateException(e.getMessage());

}

}

private String generateId() {

Date temp = new Date();

String stemp = String.valueOf(temp.hashCode());

if (stemp.length() < 10)

return stemp.substring(1, stemp.length());

else

return stemp.substring(1, 9);

}

private TelBookModel getTelBookModel(TelBookEntryLocal entry) {

TelBookModel data = new TelBookModel();

data.setId(entry.getId());

data.setName(entry.getName());

data.setTelnumber(entry.getTelnumber());

return data;

}

/**

  • Business Method.

*/

}

}

Not what you were looking for? View more on this topic or Ask a question