Results 1 to 8 of 8
  1. #1
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Question null pointer exception in string comparison

    hi..
    I want to compare the contents of text files. I have to display the mis matched strings and the number of mis matched words. Here i have my code below. but i get null pointer exception after the display of the mismatched words. the number of mis matched words is not getting displayed. can anyone fix the bug.
    thanks..
    Java Code:
     public static void main(String[] args) {
            // TODO code application logic here
             String [] str1 = new String[800];
             String [] str2 = new String[800];
             String [] str3 = new String[800];
             int cnt3=0;
            File f1=new File("d:/temp/test1.txt");
            File f2=new File("d:/temp/test2.txt");
            if((!f1.exists())&&(!f2.exists()))
            {System.out.println("file not found");
            }
    
     else
            {
            try
            {
           // FileInputStream in1 = new FileInputStream(f1);
    
            FileReader file1 = new FileReader(f1);
          StreamTokenizer streamTokenizer1 = new StreamTokenizer(file1);
            FileReader file2 = new FileReader(f2);
          StreamTokenizer streamTokenizer2 = new StreamTokenizer(file2);
    
            int c1=0;
            int i=0;
          int numberOfTokensGenerated1 = 0;
          while(i != StreamTokenizer.TT_EOF){
                  str1[c1]=streamTokenizer1.toString();
                   c1++;
              i = streamTokenizer1.nextToken();
            numberOfTokensGenerated1++;
          }
    
     //System.out.println("total mispelled words " +c1);
            int c2=0;
            int j=0;
          int numberOfTokensGenerated2 = 0;
          while(j != StreamTokenizer.TT_EOF){
                  str2[c2]=streamTokenizer2.toString();
    			  String w = streamTokenizer2.toString();
    			  // System.out.println(w);
                   c2++;
              j = streamTokenizer2.nextToken();
            numberOfTokensGenerated2++;
          }
    
                   int q=0;
                     int k;
                    if(c1==c2) 
                    {
            for( k=0;k<=c1;k++)
            {   
    		     if(str1[k].compareTo(str2[k])==0)
    			 {  System.out.println("this string is eq" +str1[k]);
    			  }
    			  else
    			  { str3[q]=str2[k];
    			  q++;
    			  
    			  }
            } 
            
            System.out.println("\n Total Mispelled words" +q);
            System.out.println("\n Mispelled words");
    
            for(i=0;i<q;i++)
            {
             System.out.println(str3[i]);
    
            }
    
                }
             
                }catch(IOException e)
            {  e.printStackTrace();
    
                }
    
                
    
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    please fix your indentations so others can read your code. Also it will help to let us know which line is causing the exception.

  3. #3
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Default

    the code shows null pointer exception in

    if(str1[k].compareTo(str2[k])==0).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, please edit the code in your first post so that the indentations are correct. As posted it's near impossible to read. If you do this, you will likely get better help quicker.

  5. #5
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I was to lazy to run it, but at a first look:
    Your c1 and c2 are augmented in a while loop and point past the last arrayitem after the loop. So if the for-loop runs to k<= c1 it points into the air (null).
    In general, when traversing an array it's safer to use the arraylength in the stopping-criterium:
    for( k=0;k<=c1;k++) --> for( k=0; k<str1.length; k++ )

    Correction: you wanted to stop earlier: but use < instead of <=
    Last edited by Jodokus; 03-26-2011 at 02:40 PM.

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    i agree with Jodokus but you could also do this:
    Java Code:
    int length = Math.min(c1, str1.length);
    for (k=0; k<=length; k++);

    that way you traverse it the correct number of times but Not past the array length, avoiding null pointer exception

  7. #7
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Default

    i tried with the for loop using like

    Java Code:
    for( k=0;k<str1.length;k++)
            {   
    		     if(str1[k].compareTo(str2[k])==0)
    			 {  System.out.println("this string is eq" +str1[k]);
    			  }
    			  else
    			  { str3[q]=str2[k];
    			  q++;
    			  
    			  }
            }
    but still i get the same null pointer exception in line ... str1[k].compareTo(str2[k])==0

    :( pls guide me..

  8. #8
    Saran185 is offline Member
    Join Date
    Jan 2011
    Posts
    36
    Rep Power
    0

    Default

    hi ..
    my bug is fixed..i used the for loop as

    for( k=1;k<c1;k++)

    now am getting the expected output.. thank u all..:)

Similar Threads

  1. Null Pointer Exception
    By jonytek in forum New To Java
    Replies: 5
    Last Post: 03-02-2011, 07:16 AM
  2. Null pointer exception
    By Domo230 in forum New To Java
    Replies: 4
    Last Post: 02-28-2011, 10:21 AM
  3. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 02:25 PM
  4. Null pointer exception?
    By coffee in forum New To Java
    Replies: 4
    Last Post: 08-03-2009, 03:22 AM
  5. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •