0

I am going to insert data into table which also includes image files. The json data I am sending from AJAX is:

enter image description here

The data is send by changing file type into base64 in javascript.

Some entity of my java class are:

Letter.java

public class Letter {

    private BigDecimal letterNo;
    private BigDecimal letterId;
    private String inout;
    private String inoutNo;
    private String inoutDate;
    private String letterIssuedSubBy;
    private String letterFile;
    private String representativeName;
    private String representativeNameEng;
    private BigDecimal selectionId;
    private BigDecimal assessmentNo;
    private String entryBy;
    private String rStatus;

    private byte[] imageFile;
    private String imageTitle;

//i ommited getters and setters
}

ProcessAssessmenttTwo.java which is my Dto class

public class ProcessAnexTwo {

    private String entryBy;
    private String rStatus;

    private BigDecimal selectionId;
    private String inout;
    private String letterReceivedBy;
    private String letterIssuedSubBy;
    private String representativeNameEng;
    private String inoutDate;
    private BigDecimal letterId;
    private BigDecimal assessmentNo;

    private String imageFiles;

    private byte[] imageFile;

    private String imageTitle;
}

The data is successfully coming to the api but while inserting into the database it is showing me error :

 @PostMapping("saveProcessAnexTwo")
        public ResponseEntity saveAnnexTwo(@RequestBody ProcessAnexTwo processAnexTwo,Model model) {

            System.out.println("entered in saveProcessAnex Two func");

            String imgByte=processAnexTwo.getImageFiles();
            System.out.println(imgByte);
            byte[] nnn=imgByte.getBytes();
            byte[] imageFile= Base64.getEncoder().encode(nnn);
            System.out.println(processAnexTwo.getImageTitle());

            Letter letter = new Letter(processAnexTwo.getLetterId(), processAnexTwo.getInout(),
                    processAnexTwo.getInoutDate(),processAnexTwo.getLetterIssuedSubBy(),null,processAnexTwo.getRepresentativeNameEng(),
                    null,processAnexTwo.getSelectionId(),processAnexTwo.getAssessmentNo(), "PCS",
                    null,imageFile,null);


            BigDecimal letterNo = letterService.insertLetterAnnexTwo(letter);
   return null;

        }

The LetterDaoImpl class to insert is:

@Override
    public BigDecimal saveLetterAnnexTwo(Letter letter) {
        try {
            System.out.println("hitted123 here");
            SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName("PCPR_ADD_LETTER");
            Map<String, Object> inParamMap = new HashMap<String, Object>();
            System.out.println(letter.getLetterId());
            inParamMap.put("P_LETTER_NO",null);
            inParamMap.put("P_LETTER_ID",letter.getLetterId());
            inParamMap.put("P_INOUT",letter.getInout());
            inParamMap.put("P_INOUT_NO",null);
            inParamMap.put("P_INOUT_DATE",letter.getInoutDate());
            inParamMap.put("P_LETTER_ISSUED_SUB_BY",letter.getLetterIssuedSubBy());
            inParamMap.put("P_LETTER_FILE",letter.getLetterFile());
            inParamMap.put("P_REPRESENTATIVE_NAME",letter.getRepresentativeName());
            inParamMap.put("P_REPRESENTATIVE_NAME_ENG",letter.getRepresentativeNameEng());
            inParamMap.put("P_SELECTION_ID",letter.getSelectionId());
            inParamMap.put("P_ASSESSMENT_NO",0);
            inParamMap.put("P_ENTRY_BY", letter.getEntryBy());
            inParamMap.put("P_R_STATUS",null);
            inParamMap.put("P_IMAGE_FILE",letter.getImageFile());
            inParamMap.put("P_IMAGE_TITLE",letter.getImageTitle());

            SqlParameterSource in = new MapSqlParameterSource(inParamMap);


            BigDecimal letterNO =  (BigDecimal) simpleJdbcCall.execute(in).get("P_LETTER_NO");


            return letterNO;
            } catch(Exception e) {
                e.printStackTrace();
            }
            return null;


    }

The error i am getting is :

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException at java.util.Base64$Decoder.decode(Base64.java:525) at com.pcs.spring.controller.api.FormApiController.saveAnnexTwo(FormApiController.java:222) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

ashwin karki
  • 643
  • 5
  • 19
  • 35
  • Your exception is showing `java.lang.NullPointerException at java.util.Base64$Decoder.decode` in method `saveAnnexTwo ` but your code for that method doesn't *have* a call to method `decode`. Instead your method has a call to method `encode`. So the code that you have posted is *not* the code that causes the error that you have posted! – Erwin Bolwidt Dec 04 '18 at 11:59
  • how can make the change so that i can decode it .. i m sending as string from ajax – ashwin karki Dec 04 '18 at 12:00
  • You should start by either posting the code that is causing the exception (not the code that you have posted) or by posting the exception that is actually causes by the code that you do have posted. – Erwin Bolwidt Dec 04 '18 at 12:02

0 Answers0