开发者

NullPointerException on "list.clear()" [duplicate]

This question already has answers here: What is a NullPointerException, and how do I fix it? (12 answers) Closed 8 years ago.

What I'm doing is working with a text document that I'm pulling numbers from a saved text document, each line has its own number, and it ignores any lines starting with a !, so, when using these codes, I am getting a NullPointerException and I'm not sure why, Its not filling the ArrayList because of this, how come?

try{

               File f = new Fil开发者_C百科e(extStorageDirectory+"/data.txt");

               FileInputStream fileIS = new FileInputStream(f);

               BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));

               String readString = new String();

               integers.clear();
               char check;
               while((readString = buf.readLine())!= null){
                   check = readString.charAt(0);
                  if(check == '!'){

                  }
                  else{
                      //integers.add(0,Integer.parseInt(readString));
                      if(check == 0){
                          integers.add(0);
                      } else {
                          if(check == 1) {
                              integers.add(1);
                          } else {
                              if(check == 2){
                                  integers.add(2);
                              }
                          }
                      }
                  }
               }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),"Find Data: "+e.toString(),Toast.LENGTH_LONG).show();
            }


Without a stacktrace indicating what line number the NullPointerException occurs on, we can only guess. Looking at the posted code, I don't see integers being initialized anywhere. So integers.clear() could potentially be throwing a NullPointerException. Perhaps you're missing a line like:

integers = new ArrayList<Integer>();

Put is somewhere where it executes prior to the call to integers.clear();.


In your current code snippet

integers

is never initialized...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜