0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaでHashMapの初期化

Posted at

Map paramMap = new HashMap<>();

csvから番号のリストを読み込んで、その中からユニークなものだけに対して次の処理にすすませたいときにHashMapを使おうとしたが、、

Map paramMap = null;
while((record = reader.readNext()) != null){
if(paramMap.get(record[0]) != null {
continue;
}
paramMap.put(record[0],record[0]);
次の処理
}

とすると、paramMap.getの部分でnullpointer.exceptionでエラーが発生します
対処はわかると簡単で

Map paramMap = null;

これではpramMapが初期化できておらず、呼び出すとnull参照が起きてしまうので
ここを

Map paramMap = new HashMap<>();

とすると正常に動作します

他にユニークなレコードのうまい取り出し方があればおしえていただけたら嬉しいです^_^

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?