@Controller("/control/product/type/list")
public class ProductTestAction extends Action {
@Resource(name="productTypeServiceBean")
private  ProductTypeService productTypeService;


@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
    productType type=productTypeService.find(productType.class, 3);
            System.out.print(type.getName());
            request.setAttribute("productType", type);
    return mapping.findForward("list");
}
在jsp页面
 产品名称:${productType.name } 不出来内容,运行这句空System.out.print(type.getName());就报空指针错误在junit.test测试中能出来
public class productTest {
private static ApplicationContext cxt;
private static ProductTypeService productTypeService;  
@BeforeClass
public static void setUpBeforeClass() throws Exception{
try {
cxt=new ClassPathXmlApplicationContext("beans.xml");
productTypeService=(ProductTypeService) cxt.getBean    ("productTypeServiceBean");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testFind(){
productType type=productTypeService.find(productType.class, 3);
System.out.print(type.getName());
}谁知道这个问题怎么解决吗?@Resource空指针错

解决方案 »

  1.   

    在junit 里面测试直接用 @Autowired
    private UserService userService;就行了,如果跑起来是空的话,那你去看看你的service的声明是不是有。
      

  2.   

    不好意思,没看清楚题,你把异常贴出来看看吧,或者到 productType type=productTypeService.find(productType.class, 3); 下个断点看看,怎么 productType  这个类名用小写开头。。
      

  3.   

    public interface ProductTypeService extends DAO {

    }@Service
    @Transactional
    public class ProductTypeServiceBean extends DaoSupport  implements ProductTypeService {service 层是这样声明的
    @Autowired 这个注解要用什么包吗,我引用不了。
      

  4.   

        productType type=productTypeService.find(productType.class, 3);
                System.out.print(type.getName());
    这两个是执行哪句时报错,如果执行productType type=productTypeService.find(productType.class, 3);报错,那么productTypeService,没有注册进来,你跟进去看productTypeService实现类里,看每有什么问题,因为看的控制类没什么不对,。如果productTypeService实现类没有什么问题 你可以去掉resource的name,试试
    如果System.out.print(type.getName());报错,那么是执行查询方法没有查询到数据
      

  5.   

    后台不报错了,jsp页面${productType.name } 没有内容出来
      

  6.   

    去掉resource注解的name不行,报错,name不能去掉。
      

  7.   

    你后台 System.out.print(type.getName());打印出了名称?而页面没有是么?如果后台有页面没有你把${productType.name } 改成 ${productType} 出来的应该是一串内存地址 。你看看有没有
      

  8.   

                request.setAttribute("productType", type);
        return mapping.findForward("list");
    这个你放session试试,你跳转了,request没有了,要不用重定向
      

  9.   

    用main函数跑了一下
    public static void main(String[] args) {
      ProductTypeService productTypeService=new ProductTypeServiceBean();
    productType type=productTypeService.find(productType.class, 5);
    System.out.println(type);
    }
    报这个错误
    Exception in thread "main" java.lang.NullPointerException
    at com.itcast.service.base.DaoSupport.find(DaoSupport.java:48)
    at com.itcast.web.action.product.ProductTestAction.main(ProductTestAction.java:34)这是DaoSupport48行报的错误:
    @Transactional
    public abstract class DaoSupport implements DAO{
    @Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
    public <T> T find(Class<T> entityClass, Object entityid) {

    return em.find(entityClass, entityid);  //这是48行
    }
    是什么原因错的呢
      

  10.   

    肯定不能用Main直接跑啊。用单元测试
      

  11.   

    知道原因,原来是tomcat7.0不支持.MyEclipse Tomcat 就支持了
      

  12.   

    如果查询方法里面在调用mapper里面的方法 ,从controller到service可以通的,但service到mapper指针为空
      

  13.   

    (单元测试)如果查询方法里面在调用mapper里面的方法 ,从controller到service可以通的,但service到mapper指针为空 ?这种情况怎么办