0

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 File(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();
            }
user2864740
  • 60,010
  • 15
  • 145
  • 220
Samuel
  • 4,337
  • 3
  • 29
  • 35

2 Answers2

1

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();.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • I'm sorry, I'm going to admit I forgot to check the stacktrace, But yes, you can assume everything has been initialized and working through the entire application, let me check – Samuel Dec 06 '10 at 03:59
  • It was initialized, but in the wrong order, I accidentally initialized it after i called the method, thanks for putting me in the right direction, rookie mistake – Samuel Dec 06 '10 at 04:09
  • No problem. Sometimes you just need a 2nd pair of eyes on the code. :) – Asaph Dec 06 '10 at 04:24
  • 1
    `But yes, you can assume everything has been initialized and working through the entire application` That's a horrible assumption when you get a null pointer exception – Falmarri Dec 06 '10 at 05:13
1

In your current code snippet

integers

is never initialized...

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112