此错误正引发在substring方法上,我发现许多线程正在处理此问题,但我遇到的问题似乎有所不同。我知道,如果您的字符串小于子字符串(开头,结尾)的大小,它将引发此错误,但是在将任何内容传递给方法调用之前,都会引发此错误。

我正在运行休眠标准:

    public static List<Object[]> baseQuery() throws HibernateException{
    Session session = getSession();
    Criteria criteria = session.createCriteria(CsvDataEntity.class);
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("clearanceMainCat"));
    projectionList.add(Projections.property("clearanceDt"));
    projectionList.add(Projections.property("vertRadians"));
    projectionList.add(Projections.property("latitude"));
    criteria.setProjection(projectionList);

    List<Object[]> list = criteria.list();
    return list;
    }


然后我在这里使用查询:

    List<Object[]> fullList = baseQuery();
    String address = "400 Pine St, Seattle, WA 98101";
    String distance = "1";
    String years = "8";

    //create 'context' for GoogleMaps GeoCode API, geocode user-defined address
    GeoApiContext context = new GeoApiContext().setApiKey("Foobar");
    GeocodingResult[] result = geocode(context, address).await();
    Geometry coordinates = result[0].geometry;

    //store user request lat & lng from address supplied from form
    double longitude = coordinates.location.lng;
    double latitude = coordinates.location.lat;

    //user requested distance
    double requestedDistanceInMiles = Double.parseDouble(distance);

    //list to store all the crimes within the specified distance
    List<CrimeModel> crimeList = new ArrayList<>();

    for (Object[] record : fullList) {

        //verifying crime was commmitted within user-defined distance
        Boolean withinDistance = false;
        double rowLng = Double.parseDouble((String)record[2]);
        double rowLat = Double.parseDouble((String)record[3]);
        double milesBetween =     (haversineDistance(latitude,rowLat,longitude,rowLng,0,0)) / 1609.34;

        if (milesBetween <= requestedDistanceInMiles) {
            withinDistance = true;
        }

        //verifying crime was committed within user-defined time-range
        Boolean withinTimeRange =false;
        double requestedYears = Double.parseDouble(years);
        long requestedDays = Math.round(requestedYears * 365);
        String objectDate = (String) record[1];
        String recordDateAsString = objectDate.substring(0, 10);


该查询有效,并且它返回的日期+时间字符串肯定超过10个字符。奇怪的是,我在第4行放置了一个断点,进行了调试,查询运行良好,但是随后它在我发布的调用substring()的代码段的底部抛出了错误。换句话说,我的String应该具有必要的长度,但在此错误之前甚至没有传入。有谁知道为什么甚至不执行它就会被抛出?我正在Spring MVC中运行此程序,这是我唯一想到的另一件事,它会导致某些奇怪的情况发生。谢谢。

最佳答案

就像@David Wallace提到的那样,由于数据库太大,因此偶尔会丢失记录,并且在从查询中填充List后,将引发错误。我对它们做了简单的处理以跳过它们(由于缺少记录,需要处理查询中的所有四列),并且一切正常。

            //skip null records in database
            if (objectDate.length() < 10) {
                continue;
            } else {
                recordDateAsString = objectDate.substring(0, 10);
            }

关于java - 模糊的“StringIndexOutOfBoundsException”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39482328/

10-15 11:16