I'm reading in a file and I need to skip any lines of length zero or starting with '#'. But I keep getting null pointer exceptions later in the code because it can never tokenize. What am I doing wrong? Thanks in advance.

Below is the relevant method:

static void readSrc(String fname) throws IOException
    {
     String buffer = null;
     String line;
         int token = 0;
        FileReader fr     = new FileReader(fname);
        BufferedReader br = new BufferedReader(fr);
           while ((buffer = br.readLine()) !=null){

       
        for (int i=0; i<27;i++)
        {
            if (buffer.equals(null)){buffer = br.readLine();i++;}
            else if (buffer.substring(0,1).equals("#")){buffer = br.readLine();i++;}
             else{  // tokenize string.  1st token is opcode, 2nd is operand
            StringTokenizer st = new StringTokenizer(buffer," ");
          token = st.countTokens();

             if (st.hasMoreTokens()){
                source[i][(0)] = (st.nextToken());
                source[i][(1)] = (st.nextToken());      
            }
            buffer = br.readLine();
         }
           }
        }
    }

Recommended Answers

All 3 Replies

First of all this: if (buffer.equals(null)) is totaly wrong. Not because it won't compile but because
If buffer is null you cannot call any methods. You will get a NullPointerException


The best way to check if something is null is the simplest: if (buffer==null) For all the other cases use .equals()

Also as for your code I prefer this simple solution:

String buffer = br.readLine();
int i=0;
while (buffer!=null){
  buffer = buffer.trim();
  if  ( (buffer.length!=0) && (buffer.charAt(0)!='#') ) {
       
          StringTokenizer st = new StringTokenizer(buffer," ");
          token = st.countTokens();

             if (st.hasMoreTokens()) {
                if (i==source.length) break;

                source[i][0] = (st.nextToken());
                source[i][1] = (st.nextToken());     
                i++; 
            }
  }
  buffer = br.readLine();
}

With your way you have 2 loops (while and for) but you use only ONE. At the end of the for, you read the next line and you continue with the for, so the while is not needed. BUT since increase the 'i' index in the for and the coninue, the array 'source' will have some elements missing.
Example, you will have source[0] have some value but as you increase the 'i' when you go to put again value, the next time might be source[3]. So those that are between will have null value.

With the above way, after the while has finished the 'i' will hold the number of lines actually put in the array. If only 4 lines much the requirements you will have values for:
source[0], source[1], source[2], source[3] and the rest will be null. So you can use the 'i' mentoned above to iterate.
But with your way, the lines will be scattered insed the array.

Of course you could use the for-loop but with much different implemantation and only the for-loop without the while

Thanks for the quick reply!

Also there is another error in the code:

StringTokenizer st = new StringTokenizer(buffer," ");
          token = st.countTokens();
if (st.hasMoreTokens()) {
.......               
                source[i][0] = (st.nextToken());
                source[i][1] = (st.nextToken());     
               .......
            }

With the 'if' you check if there are more tokens but NOT if there are 2 tokens. Then you call the nextToken twice without checking there are any more tokens.
Meaning the if the line has only one token:

# 2 tokens
aaaaaa bbbbbb
# ONE token
ccccccccc

At the line with the "ccccccccc" you will get an exception because you call the nextToken twice but there is only 1 token.

Better use this:

StringTokenizer st = new StringTokenizer(buffer," ");
          token = st.countTokens();
if (token==2) {
.......               
                source[i][0] = (st.nextToken());
                source[i][1] = (st.nextToken());     
               .......
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.