gpt4 book ai didi

java - 模糊 'StringIndexOutOfBoundsException"

转载 作者:行者123 更新时间:2023-12-01 09:30:21 26 4
gpt4 key购买 nike

这个错误是在子字符串方法上抛出的,我发现很多线程都在处理这个问题,但我遇到的问题似乎有所不同。我知道如果您的字符串短于子字符串(开始,结束)大小,它会抛出此错误,但在任何内容传递到方法调用之前都会抛出此错误。

我正在运行 hibernate 标准:

    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);

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

最佳答案

就像@David Wallace提到的,由于数据库太大,偶尔会丢失记录,并且在从查询填充列表后,抛出错误。我对这些进行了简单的处理以跳过它们(由于缺少记录,需要处理查询中的所有四列),并且一切正常。

            //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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com