0

I am getting simple Null pointer exception which I am unable to resolve. Have tried debugging the application but it just wont go in the dao class where i want it to go. It shows : Unable to Install breakpoint and Absent Line control message. But as i learned from other questions on stack overflow this message was to be ignored ,so that I did.

Details for issue:

Exception :

in test controller

in create Service

at com.service.DesignerService.create(DesignerService.java:20) at com.controller.HomeController.test(HomeController.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)in create service java.lang.NullPointerException

My controller method :

@GetMapping("/test")
    public String test() {
        System.out.println( "in test controller");
        DesignerService obj = new DesignerService();
        try {
            obj.create();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }

My service calling :

public void create (){
        System.out.println("in create service");
        Designer designer = getDesigner();
        designerDao.createDesigner(designer);
    }

And Dao where the debugger never reaches

public void createDesigner(Designer designer) {
        Session session = null;
        try {
            session = sessionFactory.openSession();
            session.beginTransaction();
            String id = String.valueOf(session.save(designer));
            System.out.println("Designer ID :"+id);
            session.getTransaction().commit();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

Please help me run this project . Many thanks in advance

Sahil Gupta
  • 73
  • 1
  • 3
  • 12
  • Sahil, you are missing details that are required to properly answer this question. The null pointer exception is because designerDao is null, but without seeing how the designerDao is instantiated or injected it's impossible to provide a suitable answer. – lane.maxwell Aug 01 '18 at 17:32
  • Additionally, don't write classes like `DesignerDao`. Spring Data can autogenerate them for you from a `CrudRepository` interface. – chrylis -cautiouslyoptimistic- Aug 01 '18 at 17:43

1 Answers1

1

Instead of creating a object why don't you Autowire your service using

@Autowired private DesignerService obj

and where did you initialise this object designerDao

KNDheeraj
  • 834
  • 8
  • 23