我正在java中导入csv文件并尝试存储数据以使用kenesis生成器库将其发送到aws kenesis。但是数据没有被存储,而是我得到了nullPointerException
这是代码
public static String[] row = null;
public static void main(String KenesisWrite[]) throws IOException {
String num="";
String csvFilename = "/tmp/nationalparks.csv";
CSVReader csvReader = new CSVReader(new FileReader(csvFilename));
int j=0;
while((row = csvReader.readNext()) != null) {
System.out.println(row[0]
// + " # " + row[1]
);
// result = result + row[0];
num+=row[j];
j++;
}
System.out.print(row);
List<PutRecordsRequestEntry> putRecordsRequestEntryList = new ArrayList<PutRecordsRequestEntry>(); //create a list to put all metadata and data
for (int i = 0; i < 100; i++) {
PutRecordsRequestEntry putRecordsRequestEntry = new PutRecordsRequestEntry();//for single record put all metadata
putRecordsRequestEntry.setData(ByteBuffer.wrap(num.getBytes()));
putRecordsRequestEntry.setPartitionKey(String.format("partitionKey-%d", i));
putRecordsRequestEntryList.add(putRecordsRequestEntry);
nationparks.csv文件
1,2,3 3,2 2,1 1,2,3 3,2 2,1
错误
"C:\Program Files\Java\jdk1.8.0_111\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.6\lib\idea_rt.jar=51049:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.2.6\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;C:\Users\Rushabh-PC\IdeaProjects\WriteDataToKenesis\target\classes;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\amazon-kinesis-client\1.7.5\amazon-kinesis-client-1.7.5.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\aws-java-sdk-dynamodb\1.11.115\aws-java-sdk-dynamodb-1.11.115.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\aws-java-sdk-s3\1.11.115\aws-java-sdk-s3-1.11.115.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\aws-java-sdk-kms\1.11.115\aws-java-sdk-kms-1.11.115.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\jmespath-java\1.11.115\jmespath-java-1.11.115.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\aws-java-sdk-kinesis\1.11.115\aws-java-sdk-kinesis-1.11.115.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\aws-java-sdk-cloudwatch\1.11.115\aws-java-sdk-cloudwatch-1.11.115.jar;C:\Users\Rushabh-PC\.m2\repository\com\google\guava\guava\18.0\guava-18.0.jar;C:\Users\Rushabh-PC\.m2\repository\com\google\protobuf\protobuf-java\2.6.1\protobuf-java-2.6.1.jar;C:\Users\Rushabh-PC\.m2\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;C:\Users\Rushabh-PC\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\amazon-kinesis-producer\0.12.6\amazon-kinesis-producer-0.12.6.jar;C:\Users\Rushabh-PC\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar;C:\Users\Rushabh-PC\.m2\repository\commons-io\commons-io\2.4\commons-io-2.4.jar;C:\Users\Rushabh-PC\.m2\repository\com\amazonaws\aws-java-sdk-core\1.11.128\aws-java-sdk-core-1.11.128.jar;C:\Users\Rushabh-PC\.m2\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;C:\Users\Rushabh-PC\.m2\repository\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar;C:\Users\Rushabh-PC\.m2\repository\commons-codec\commons-codec\1.9\commons-codec-1.9.jar;C:\Users\Rushabh-PC\.m2\repository\software\amazon\ion\ion-java\1.0.2\ion-java-1.0.2.jar;C:\Users\Rushabh-PC\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.6.6\jackson-databind-2.6.6.jar;C:\Users\Rushabh-PC\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.6.0\jackson-annotations-2.6.0.jar;C:\Users\Rushabh-PC\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.6.6\jackson-core-2.6.6.jar;C:\Users\Rushabh-PC\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-cbor\2.6.6\jackson-dataformat-cbor-2.6.6.jar;C:\Users\Rushabh-PC\.m2\repository\joda-time\joda-time\2.8.1\joda-time-2.8.1.jar;C:\Users\Rushabh-PC\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar;C:\Users\Rushabh-PC\.m2\repository\com\opencsv\opencsv\4.1\opencsv-4.1.jar;C:\Users\Rushabh-PC\.m2\repository\org\apache\commons\commons-lang3\3.6\commons-lang3-3.6.jar;C:\Users\Rushabh-PC\.m2\repository\org\apache\commons\commons-text\1.1\commons-text-1.1.jar;C:\Users\Rushabh-PC\.m2\repository\commons-beanutils\commons-beanutils\1.9.3\commons-beanutils-1.9.3.jar;C:\Users\Rushabh-PC\.m2\repository\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar" WriteDataToKenesis
Exception in thread "main" 1,2,3
3,2
java.lang.ArrayIndexOutOfBoundsException: 1
at WriteDataToKenesis.main(WriteDataToKenesis.java:38)
Process finished with exit code 1
答案 0 :(得分:0)
使用java StringBuilder 或 StringBuffer 附加字符串。 您的 num + = row 每次都会创建新的java不可变字符串,这可能会抛出ArrayIndexOutOfBoundsException。