aspose file tools*
The moose likes JSF and the fly likes Please Help....NullPointerException... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Please Help....NullPointerException..." Watch "Please Help....NullPointerException..." New topic
Author

Please Help....NullPointerException...

Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
Please help, I have banged my head on this one… Why am I getting a NullPointerException below (in bold)? It is the same code accessing PlyService as PosService….

plyModel = plyService.getPly(“000001”); <- not getting it on this statement
posModel = posService.testPos(“QB”); <- getting it here


<code>

I have the following beans defined in my applicationContext.xml:

<bean id="posDao" class="com.highmark.fm.dao.PosJpaDaoImpl" />
<bean id="plyDao" class="com.highmark.fm.dao.PlyJpaDaoImpl" />

<bean id="posService"
class="com.highmark.fm.service.PosJpaServiceImpl">
<property name="posDao">
<ref local="posDao" />
</property>
</bean>

<bean id="plyService"
class="com.highmark.fm.service.PlyJpaServiceImpl">
<property name="plyDao">
<ref local="plyDao" />
</property>
</bean>

In the PlyHander.class I am using the following:

package com.highmark.fm.handler;

//import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import javax.persistence.Transient;
import java.lang.SuppressWarnings;

import org.apache.commons.lang.StringUtils;

//import com.highmark.fm.exceptions.PosNotFoundException;
import com.highmark.fm.model.PlyModel;
import com.highmark.fm.service.PlyService;
import com.highmark.fm.service.PosJpaServiceImpl;
import com.highmark.fm.service.PosService;
import com.highmark.clmcmn.bean.BaseBean;

import com.highmark.fm.model.PosModel;

//******************************************************************
// PlyHandler Class *
//******************************************************************
@SuppressWarnings("all")
public class PlyHandler extends BaseBean {

private PlyService plyService;

private PosService posService;

private PlyModel plyModel = null;

private PosModel posModel = null;

public String search() {

plyModel = new PlyModel();
posModel = new PosModel();


plyModel = plyService.getPly(“000001”);
posModel = posService.testPos(“QB”);

}
}

PlyService.java

package com.highmark.fm.service;

import com.highmark.fm.model.PlyModel;

//**************************************************************
// PlyService *
//**************************************************************
public interface PlyService {

public PlyModel savePly(PlyModel ply);
public PlyModel getPly(String plyID);
public void delPly(String plyID);

}

PlyJpaServiceImpl.java

package com.highmark.fm.service;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.highmark.fm.dao.PlyDao;
import com.highmark.fm.model.PlyModel;

//******************************************************************
// PlyJpaServiceImpl *
//******************************************************************
public class PlyJpaServiceImpl implements PlyService {

private PlyDao plyDao = null;

//******************************************************************
// savePly *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public PlyModel savePly(PlyModel ply){
return plyDao.savePly(ply);
}

//******************************************************************
// getPly *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public PlyModel getPly(String plyID){
if(plyID == null ) {
throw new IllegalArgumentException("Plyision ID is null");
}
return plyDao.getPly(plyID);
}

//******************************************************************
// delPly *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public void delPly(String plyID){
plyDao.removePly(plyID);
}

//******************************************************************
// Getters & Setters *
//******************************************************************
public PlyDao getPlyDao() {
return this.plyDao;
}

public void setPlyDao(PlyDao plyDao) {
this.plyDao = plyDao;
}
}

PlyDao.java:

package com.highmark.fm.dao;

import com.highmark.fm.model.PlyModel;

//******************************************************************
// PlyDao Interface *
//******************************************************************
public interface PlyDao {

public PlyModel getPly(String plyID);
public PlyModel savePly(PlyModel ply);
public void removePly(String plyID);

}


PlyJpaDaoImpl.java:
/**
*
*/
package com.highmark.fm.dao;

import com.highmark.base.dao.BaseDAO;
import com.highmark.fm.model.PlyModel;
import javax.persistence.Query;
import com.highmark.fm.exceptions.*;

//******************************************************************
// PlyJpaDaoImpl Class *
//******************************************************************
public class PlyJpaDaoImpl extends BaseDAO implements PlyDao{

//******************************************************************
// savePly *
//******************************************************************
public PlyModel savePly(PlyModel Ply) {
/*if (log.isDebugEnabled()) {
log.debug("Saving Automated Letter Generator object.");
}*/
PlyModel newPly = entityManager.merge(Ply);
return newPly;
}

//******************************************************************
// getPly *
//******************************************************************
public PlyModel getPly(String PlyID) {
PlyModel Ply = entityManager.find(PlyModel.class,
PlyID);
return Ply;
}

//******************************************************************
// removePly *
//******************************************************************
public void removePly(String plyID) {

try {
System.out.println("delete a");
Query query = entityManager.createNamedQuery("deletePlyModel");
System.out.println("delete b");
query.setParameter("plyID", plyID);
System.out.println("delete c");

int number = query.executeUpdate();
//* failure on delete
if(number <= 0) {
throw new PlyNotFoundException();
}

} catch (RuntimeException runtimeException) {
throw new PlyDatabaseException("EntityManager exception", runtimeException);
} catch (PlyNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

PosService.java

package com.highmark.fm.service;

//import java.util.List;
import java.util.ArrayList;

import com.highmark.fm.model.PosModel;

//**************************************************************
// PosService *
//**************************************************************
public interface PosService {

public PosModel savePos(PosModel pos);
public PosModel getPos(String posID);
public void delPos(String posID);
public ArrayList getAllPos();
public PosModel testPos(String posID);
}


PosJpaServiceImpl.java

package com.highmark.fm.service;

//import java.util.List;
import java.util.ArrayList;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.highmark.fm.dao.PosDao;
import com.highmark.fm.dao.PosJpaDaoImpl;
import com.highmark.fm.model.PosModel;

//******************************************************************
// PosJpaServiceImpl *
//******************************************************************
public class PosJpaServiceImpl implements PosService {

private PosDao posDao = null;

//******************************************************************
// savePos *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public PosModel savePos(PosModel pos){
return posDao.savePos(pos);
}

//******************************************************************
// getPos *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public PosModel getPos(String posID){
if(posID == null ) {
throw new IllegalArgumentException("Position ID is null");
}
return posDao.getPos(posID);
}

//******************************************************************
// delPos *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public void delPos(String posID){
posDao.removePos(posID);
}

//******************************************************************
// getAllPos *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public ArrayList getAllPos(){

PosDao posDao = new PosJpaDaoImpl();

return posDao.getAllPos();

}

//******************************************************************
// testPos *
//******************************************************************
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT)
public PosModel testPos(String posID){
if(posID == null ) {
throw new IllegalArgumentException("Position ID is null");
}
return posDao.testPos(posID);
}

//******************************************************************
// Getters & Setters *
//******************************************************************
public PosDao getPosDao() {
return this.posDao;
}

public void setPosDao(PosDao posDao) {
this.posDao = posDao;
}
}

PosDao.java

package com.highmark.fm.dao;

//import java.util.List;
import java.util.ArrayList;

import com.highmark.fm.model.PosModel;

//******************************************************************
// PosDao Interface *
//******************************************************************
public interface PosDao {

public PosModel getPos(String posID);
public PosModel savePos(PosModel pos);
public void removePos(String posID);
public ArrayList getAllPos();
public PosModel testPos(String posID);

}


PosJpaDaoImpl.java

/**
*
*/
package com.highmark.fm.dao;

//import java.util.Arrays;
//import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;


import com.highmark.base.dao.BaseDAO;
import com.highmark.fm.model.PosModel;
import javax.persistence.Query;
//import java.util.SessionFactory;
//import javax.persistence.NamedQuery;
import com.highmark.fm.exceptions.*;
//import javax.persistence.EntityManager;
//import javax.persistence.EntityManagerFactory;

//******************************************************************
// PosJpaDaoImpl Class *
//******************************************************************
public class PosJpaDaoImpl extends BaseDAO implements PosDao{

//******************************************************************
// savePos *
//******************************************************************
public PosModel savePos(PosModel pos) {
/*if (log.isDebugEnabled()) {
log.debug("Saving Automated Letter Generator object.");
}*/
PosModel newPos = entityManager.merge(pos);
return newPos;
}

//******************************************************************
// getPos *
//******************************************************************
public PosModel getPos(String posID) {

try {

PosModel pos = entityManager.find(PosModel.class,
posID);
return pos;

} catch (RuntimeException runtimeException) {
System.out.println("Error XXXXX getResultList");
throw new PosDatabaseException("EntityManager exception", runtimeException);

}


}

//******************************************************************
// getAllPos *
//******************************************************************
public ArrayList getAllPos() {


//List posList;
ArrayList<String> posList = new ArrayList<String>(Arrays.asList("QB","RB", "WR"));

//List posList = new ArrayList();
String posID = "QB";

System.out.println("Before Executing getAllPos");
//Query query = entityManager.createQuery("select posmdl from FM.POS");

entityManager.clear();

try {
//Query query = entityManager.createQuery("select posmdl from FM.POS");
//query.setParameter("posID", posID);
//posList = query.getResultList();
//PosModel pos = entityManager.find(PosModel.class, posID);
PosModel pos = entityManager.find(PosModel.class, posID);

System.out.println(pos);
//System.out.println(pos);

// System.out.println(query.getResultList());
// Query query = entityManager.createNamedQuery("getAllPos");
// posList = query.getResultList();


} catch (RuntimeException runtimeException) {
System.out.println("Error XXXXX getResultList");
throw new PosDatabaseException("EntityManager exception", runtimeException);
}

//return (ArrayList)pos;
return (ArrayList)posList;
//return posList;
}
//******************************************************************
//testPos *
//******************************************************************
public PosModel testPos(String posID) {

try {

PosModel pos = entityManager.find(PosModel.class,
posID);
return pos;

} catch (RuntimeException runtimeException) {
System.out.println("Error XXXXX getResultList");
throw new PosDatabaseException("EntityManager exception", runtimeException);

}


}

//******************************************************************
// removePos *
//******************************************************************
public void removePos(String posID) {

try {
Query query = entityManager.createNamedQuery("deletePosModel");
query.setParameter("posID", posID);

int number = query.executeUpdate();
//* failure on delete
if(number <= 0) {
throw new PosNotFoundException();
}

} catch (RuntimeException runtimeException) {
throw new PosDatabaseException("EntityManager exception", runtimeException);
} catch (PosNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}




</code>
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags and attempt to reduce the code snippets to the absolute essentials--500 lines is a lot of code to wade through to try to find a problem.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

As David says, 500 lines of code is too much to look through to find a problem... at least for a person. If you remove the @SuppressWarnings("all") tag you have, the editor/IDE you are using might just point out the problem for you. Personally, I believe suppressing all warnings to be a risky behavior. If there are some specific warnings you want to suppress, do such individually with comments as to why you have chosen to ignore them.
Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
sorry guys...I will try to minimize it to this:

see this code below in BOLD....Cannot figure out why I am getting the NullPointerException...

plyModel = plyService.getPly(“000001”); <- not getting it on this statement
posModel = posService.testPos(“QB”); <- getting the Null Pointer exception here

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags (correctly).

Where's the config for the entity that uses these services? Are you certain that it's the posService that's null?
Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
David...This is new to me...When you say config, what do you mean?...some configuration .xml file?
Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
David...here is my faces-config.xml files...

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Is this the class in which the NPE is occurring? How do you set posService? If it's null it's likely it's not being set. If this is how you're configuring the class that uses plyService successfully and fails when using posService it seems like you should be configuring a posService as well.

In any case, this is hardly appropriate for the beginner's forum, and if you're using JSF it belong either there or the Spring forum.
Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
David going forward, I will post in the appropriate forums you suggest...At this point I do not want to dupe post....Let me ask this... I am defining posService as well(below)....PosHandler uses PosService successfully...PlyHandler use PlyService successfully, but needs to use PosService as well....Do I need a third config in my faces-config for this and what must it contain?...(so I do not receive the NullPointerException)....

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No clue--I've never used JSF.

Can you just add another <managed-property> element?

This is why posts need to be made in the right place.
Mirrell Knabe
Ranch Hand

Joined: Oct 13, 2008
Posts: 32
Yeah...there's nothing to stop me....What do you suggest I do about this post?.....(i.e. getting it over there)....
W. Joe Smith
Ranch Hand

Joined: Feb 10, 2009
Posts: 710
Mirrell Knabe wrote:Yeah...there's nothing to stop me....What do you suggest I do about this post?.....(i.e. getting it over there)....


Just wait, one of the bartenders or sheriffs will handle it.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Help....NullPointerException...
 
Similar Threads
Proper Syntax for a Named Query
Help -> NullPointerException
javamail: method blocks instead of throwing exception
Transaction rollback in a multithreaded scenario
Rollback not working with @Transactional