将对象发送到Service

时间:2018-09-09 15:49:23

标签: spring hibernate bufferedreader stringbuffer

嗨,

我实际上正在开发一个应用程序,该应用程序可以读取文件中的字段并将其使用spring,J2EE,Hibernate和Mysql记录到数据库中,但是在尝试将检索到的字段(对象)发送到服务时遇到了问题我收到java.lang.NullPointerException错误。

这是文件阅读器代码:

    import Infradashboard.Infradashboard.Entities.Call;
    import Infradashboard.Infradashboard.Services.CallService;
    import org.springframework.beans.factory.annotation.Autowired;

    import java.io.BufferedReader;
    import java.io.FileReader;

    public class FileOperationCdr {


    @Autowired
    CallService callService;

    public void loadCdrValues() throws Exception {

    //Read CDR File
      BufferedReader in = new BufferedReader(new FileReader(
            "/home/rida/IdeaProjects/CDR_SM/S000A4B-D40E-180805-05_47"));

    //Buffer to store file contents
    StringBuffer fileContentBuffer = new StringBuffer();

    String str;
    while ((str = in.readLine()) != null) {
        //If empty line, replace it with comma in buffer
        if (str.equals("")) {
            fileContentBuffer.append(",");
        }

        //Add line to file buffer
        fileContentBuffer.append(str);
    }

    //Split file contents with comma delimiter and store in an array
    //This provides each CDR record as array element
    String[] contentArray = fileContentBuffer.toString().split(",");

    //Main object to be stored in db
    //This will contain start, stop and interim-update details

    //Iterate through CDR content
    for (int i = 0; i < contentArray.length; i++) {
        if (contentArray[i].length() > 0) {
            //Split the content array with "TAB" delimiter to get all values of the data
            String[] innerArray = contentArray[i].split("\t");

            //Local object to store data values
            Call innerObject = new Call();

            String timedayminutes = (innerArray[0].substring(innerArray[0].lastIndexOf(":") + 1));
            String datemounth = (innerArray[0].split("/")[0]).toString();
            String dateday = (innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1));
            String durationhours = (((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(6,7));
            String durationminutes = (((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(7,9));
            String durationsecondes = (((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(9,11));
            String durationmilliseconds = (((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(11,14));
            String conditioncode = (((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(14,15));
            String dialednumber = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(16))).split(" ")[0];
            String callingnumber = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(32))).split(" ")[0];
            String destinationsip = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(48))).split(" ")[0];
            String orginatingsip = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(81))).split(" ")[0];
            String feautureflag = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(114))).split(" ")[0];
            String BCC = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(116,117)));
            String MAUUI = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(117,118)));
            String resourceflag = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(118,119)));
            String bandwidth = (((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(120,122)))).replaceAll("\\D+","");
            String avgsid = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(123))).split(" ")[0];
            String callingipadresse = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(160,174))).split(" ")[0];
            String calledipadresse = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(176,190)));
            String voicecall = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(192,193)));
            String videocall = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(193,194)));
            String faxcall = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(194,195)));
            String textcall = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(195,196)));
            String othercall = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(196,197)));
            String codec = ((((innerArray[0].substring(innerArray[0].lastIndexOf("/") + 1))).substring(198,200))).replaceAll("\\D+","");
            String callingpartyid = "No";
            String calledpartyid = "No";
            String carriagereturn = "NO";



            innerObject.setTimeofdayhours(Integer.parseInt((innerArray[0].split(":")[0]).toString()));
            innerObject.setTimeofdayminutes(Integer.parseInt((timedayminutes.split(" ")[0]).toString()));
            innerObject.setDateofyearmounth(Integer.parseInt((datemounth.substring(datemounth.lastIndexOf(" ") + 1)).toString()));
            innerObject.setDateofyearday(Integer.parseInt((dateday.substring(0, 2)).toString()));
            innerObject.setDurationhours(Integer.parseInt(durationhours.toString()));
            innerObject.setDurationminutes(Integer.parseInt(durationminutes.toString()));
            innerObject.setDurationseconds(Integer.parseInt(durationsecondes.toString()));
            innerObject.setDurationmilliseconds(Integer.parseInt(durationmilliseconds.toString()));
            innerObject.setConditioncode(conditioncode.toString());
            innerObject.setDialednumber(dialednumber.toString());
            innerObject.setCallingnumber(callingnumber.toString());
            innerObject.setTerminatingSIPEntity(destinationsip.toString());
            innerObject.setOriginatingSIPEntity(orginatingsip.toString());
            innerObject.setFeatureflag(feautureflag.toString());
            innerObject.setBCC(BCC.toString());
            innerObject.setMAUUI(MAUUI.toString());
            innerObject.setResourceflag(resourceflag.toString());
            innerObject.setBandwidth(Integer.parseInt(bandwidth.toString()));
            innerObject.setAvGSID(avgsid.toString());
            innerObject.setCallingPartyIP(callingipadresse.toString());
            innerObject.setCalledPartyIP(calledipadresse.toString());
            innerObject.setCallusagevoice(voicecall.toString());
            innerObject.setCallusagevideo(videocall.toString());
            innerObject.setCallusagefax(faxcall.toString());
            innerObject.setCallusagetext(textcall.toString());
            innerObject.setCallusageother(othercall.toString());
            if (codec.equals(""))
            {
                innerObject.setCodec(Integer.parseInt("0"));
            }
            else
            {

            innerObject.setCodec(Integer.parseInt(codec.toString()));
            }
            innerObject.setCallingpartyid(callingpartyid.toString());
            innerObject.setCalledpartyid(calledpartyid.toString());
            innerObject.setCarriagereturn(carriagereturn.toString());

                    callService.createCall(innerObject);
     }
    }
    }

服务:

    import Infradashboard.Infradashboard.Entities.Call;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Service;
    import java.util.List;

    @Service
    public interface CallService {
        List<Call> getAllCalls();

        Call getCallById(Integer callId);

        Call createCall(Call call);

        Call updateCall(Integer callId, Call callDetails);

    }

服务实现:

    import Infradashboard.Infradashboard.Entities.Call;
    import Infradashboard.Infradashboard.Repository.CallRepository;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Service;
    import java.util.List;

    @Service
    public class CallServiceImp implements CallService {

        @Autowired
        CallRepository callRepository;

        @Override
        public List<Call> getAllCalls() {
            return callRepository.findAll();
        }

        @Override
        public Call getCallById(Integer callId) {
            return callRepository.findById(callId)
                         .orElseThrow(() -> new ResourceNotFoundException( "Call" , "Id" , callId));
        }

        @Override
        public Call createCall(Call call) {
            return callRepository.save(call);
        }

        @Override
        public Call updateCall(Integer callId, Call callDetails) {
       }
       } 

文件内容:

    05:41 08/05
    054100000000N 0275600000      0505455134      ACM                              SBCE                4 M00  1 75f9e080-9861-11e8-abe2-0050569600f2 10.0.0.5      0.0.0.0         YNNNN       

生成的错误:

     java.lang.NullPointerException
 at Infradashboard.Infradashboard.DAO.FileOperationCdr.loadCdrValues(FileOperationCdr.java:124)
 at Infradashboard.Infradashboard.InfradashboardApplication.main(InfradashboardApplication.java:17)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.base/java.lang.reflect.Method.invoke(Method.java:564)
 at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

如果我不使用callservice.createcall(innerobject);代码可以正常工作; 有没有另一种方法可以在不使用新的call(),initializer的情况下创建调用innerobject?

0 个答案:

没有答案