这个错误在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);
查询有效,它返回的日期+时间字符串肯定超过十个字符。奇怪的是,我在第4行放置一个断点,调试,查询运行正常,但然后它在我发布的调用substring()的片段的底部抛出错误。换句话说,我的String应该具有必要的长度,但在此错误之前它甚至没有被传入。有没有人知道为什么在它还没有被执行时会被抛出?我在Spring MVC中运行它,这是我能想到的唯一可能导致一些奇怪情况发生的事情。感谢。
答案 0 :(得分:1)
就像@David Wallace提到的那样,因为数据库太大了,偶尔会丢失记录,并且在从查询中填充List之后,会抛出错误。我对这些进行了简单的处理以跳过它们(由于缺少记录,查询中的所有四列都需要处理),一切正常。
SELECT L.LocationDesc
,FI.LocationName
,FI.CreatedDate
,FI.FileName
FROM dbo.FileImport FI
LEFT OUTER JOIN dbo.Location L ON FI.LocationName = L.LocationDesc
AND CAST(FI.CreatedDate AS DATE) = '2016-09-14'
WHERE L.LocationDesc IS NULL