2016-09-07 11 views
0

何らかの理由でフラットバッファから文字列を読み込むときにIndexOutOfBoundsExpetionが返されます。 私のスキーマ:テーブルから文字列を読み取るときのindexOutOfBoundExeption

namespace com.busalarmclock.flatbuffers; 

table Message { 
    routes:[Route]; 
    stops:[Stop]; 
    trips:[Trip]; 
} 

table Route { 
    route_id:string; 
    route_name:string; 
    route_description:string; 
    trips:[Trip]; 
} 

table Trip { 
    trip_id:string; 
    op_days:int; 
    stops:[TripStop]; 
} 

table Stop { 
    stop_id:int; 
    stop_name:string; 
    stop_lon:double; 
    stop_lat:double; 
} 

table TripStop { 
    stop:Stop; 
    arrival_time:long; 
    departure_time:long; 
    dropoff_type:short; 
} 

root_type Message; 

これは私が私のバッファを書いている方法です:

public static byte[] createStopMessage(TopDocs hits, IndexSearcher indexSearcher) throws IOException { 
    FlatBufferBuilder builder = new FlatBufferBuilder(1); 
    int[] stopData = new int[hits.totalHits]; 

    for (int i = 0; i < hits.totalHits; i++) 
     stopData[i] = createStopObject(indexSearcher.doc(hits.scoreDocs[i].doc), builder); 

    int stopsOffset = Message.createStopsVector(builder, stopData); 
    Message.startMessage(builder); 
    Message.addStops(builder, stopsOffset); 
    int root = Message.endMessage(builder); 
    builder.finish(root); 

    return builder.sizedByteArray(); 
} 

public static byte[] createTripStopsMessage(TripModel trip, IndexSearcher indexSearcher) { 
    FlatBufferBuilder builder = new FlatBufferBuilder(1); 
    int[] tripStopData = new int[trip.tripStopModels.length]; 

    for (int i = 0; i < trip.tripStopModels.length; i++) 
     tripStopData[i] = createTripStopObject(trip.tripStopModels[i], builder); 

    System.out.printf("tripId:%s", trip.tripId); 
    int tripIdOffset = builder.createString(trip.tripId); 
    int tripStopsOffset = Trip.createStopsVector(builder, tripStopData); 

    Trip.startTrip(builder); 
    Trip.addTripId(builder, tripIdOffset); 
    Trip.addStops(builder, tripStopsOffset); 
    int tripOffset = Trip.endTrip(builder); 

    Message.startMessage(builder); 
    Message.addTrips(builder, tripOffset); 
    int messageOffset = Message.endMessage(builder); 
    builder.finish(messageOffset); 

    return builder.sizedByteArray(); 
} 

public static int createTripStopObject(TripStopModel tripStopModel, FlatBufferBuilder builder) { 
    int stopOffset = createStopObject(tripStopModel.stop, builder); 
    return TripStop.createTripStop(builder, stopOffset, tripStopModel.arrivalTime, 
      tripStopModel.departureTime, tripStopModel.dropoffType); 
} 

、これらは私のモデルです:

public class TripModel { 
public String tripId; 
public int opDays; 
public TripStopModel[] tripStopModels; 

public TripModel() { 
} 

public TripModel(String tripId) { 
    this.tripId = tripId; 
} 

public TripModel(String tripId, TripStopModel[] tripStationHits) { 
    this.tripStopModels = tripStationHits; 
    this.tripId = tripId; 
} 

public TripModel(String tripId, int opDays, TripStopModel[] tripStationHits) { 
    this.tripId = tripId; 
    this.opDays = opDays; 
    this.tripStopModels = tripStationHits; 
} 

import org.apache.lucene.document.Document; 

/** 
* Created by User on 09/07/2016. 
*/ 
public class TripStopModel { 
    public long arrivalTime; 
    public long departureTime; 
    public short dropoffType; 
    public Document stop; 

    public TripStopModel() { 
    } 

    public TripStopModel(long arrivalTime, long departureTime, short dropoffType, Document stop) { 
     this.arrivalTime = arrivalTime; 
     this.departureTime = departureTime; 
     this.dropoffType = dropoffType; 
     this.stop = stop; 
    } 
} 

私はLuceneのデータベースを持っている、と私は思いますいくつかのデータをflatbufferメッセージに取得しようとしています。 バッファを作成するときにエラーは発生しませんが、最初のバッファからバッファを読み込むときにIndexOutOfBoundsExeptionが返されます。 私はチェックして、解析中にStringがnullでないことを確認しました。

答えて

0

バッファの作成方法に問題はありません。

IndexOutOfBoundsExceptionが表示された場合は、通常、バッファが作成されて読み込まれたときにバッファが破損していることを意味します。バッファを読み込む直前にチェックすることができます。バッファのサイズとバイト数は、作成したときと同じですか?バイナリファイルまたは転送プロトコルを使用していますか?

+0

バッファ書き込みで実際に何か問題がありました。トリップ配列ベクトルオフセットを作成して追加する代わりに、トリップオフセットを追加しました。とにかくありがとう:-) – itayrabin

0

はそれを固定:)

を私が誤ってオフセットtripVectorとしてtripOffsetを追加しました!

関連する問題