NullPointerException异常的ArrayList中时调用"新增"? [英] NullPointerException for ArrayList when calling "add"?

查看:145
本文介绍了NullPointerException异常的ArrayList中时调用"新增"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行下面的code它给了我 NullPointerException异常。虽然我检查,如果不为空再加入,但仍给这个eception.the 文件是一个纯文本(TXT)
可能是什么问题?

 的BufferedReader BR2 =新的BufferedReader(新的FileReader(文件));
ArrayList的<串GT; keArrayList = NULL;
的for(int i = 0;!br2.readLine()= NULL;我++)
{
  串字母= br2.readLine();
  如果(信!= NULL)
    keArrayList.add(I,字母);
}


解决方案

keArrayList

 的ArrayList<串GT; keArrayList = NULL;

更改为:

 列表<串GT; keArrayList =新的ArrayList<串GT;();

whenever i run the following code it gives me NullPointerException.although i checking if not null then add, but still giving that eception.the file is a plain text(.txt) what could be wrong?

BufferedReader  br2 = new BufferedReader(new FileReader(file));
ArrayList<String> keArrayList=null;
for(int i=0;br2.readLine()!=null;i++)
{
  String  letter= br2.readLine();
  if (letter!=null)
    keArrayList.add(i,letter);
}

解决方案

keArrayList is null:

ArrayList<String> keArrayList=null;

Change to:

List<String> keArrayList=new ArrayList<String>();

这篇关于NullPointerException异常的ArrayList中时调用&quot;新增&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆