Results 1 to 8 of 8
  1. #1
    Navin Israni is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default probably the WEIRDEST error you would ever see in java

    ok..so I am doing a program on NLP..it uses function eliminateStopWords()..this function reads from a 2D array "sentTokens" (of detected tokens). In the code below, index i is sentence number, j is for each token in the ith sentence.

    now, what my eliminateStopWords() does is this:

    it reads stop words from a text file and stores them in a TreeSet

    reads tokens from sentTokens array and checks them for stop words. If they are collocations, then they should not be checked for stop words, they are just dumped into a finalTokens array...if they are not a collcation, then they are individually checked for stop words and are added to finalTokens array only if they are not stop words..

    the problem comes in the loop of this step 2..here is some code of it:

    Java Code:
    private void eliminateStopWords() {
    
        try {
    
            // Loading TreeSet for stopwords from the file.
            stopWords = new TreeSet<String> ();
            fin = new File("stopwords.txt");
            fScan = new Scanner(fin);
            while (fScan.hasNextLine()) 
                stopWords.add(fScan.nextLine());
    
            fScan.close();
    
            /* Test code to print all read stopwords
            iter2 = stopWords.iterator();
            while (iter2.hasNext())
                System.out.println(iter2.next()); */
    
            int k=0,m=0;    // additional indices for finalTokens array
            System.out.println(NO_OF_SENTENCES);
    
     newSentence: for(i=0; i < NO_OF_SENTENCES; i++)
              {
    
            System.out.println("i = " + i);
                for (j=0; j < sentTokens[i].length; j+=2)
                {
    
            System.out.println("j = " + j);
    
                    // otherwsise, get two successive tokens
                        String currToken = sentTokens[i][j];
                        String nextToken = sentTokens[i][j+1];
                        System.out.println("i = " + i);
                        System.out.println(currToken + " " + nextToken);
                        if ( isCollocation(currToken, nextToken) ) {    
    // if the current and next tokens form a bigram collocation, they are not checked for stop words
                            // but are directly dumped into finalTokens array
                            finalTokens[k][m] = currToken; m++;
                            finalTokens[k][m] = nextToken; m++;
                        }
    
                        if ( !stopWords.contains(currToken) )
                        {   finalTokens[k][m] = currToken; m++;  }
    
                        if ( !stopWords.contains(nextToken) )
                        {       finalTokens[k][m] = nextToken; m++; }
    
    
                    // if current token is the last in the sentence, do not check for collocations, only check for stop words
                    // this is done to avoid ArrayIndexOutOfBounds Exception in sentences with odd number of tokens
    
    [COLOR="Red"]// HERE COMES THE ERROR
    
                        System.out.println("i = " + i);
    
                        if ( j==sentTokens[i].length - 2) {
    [/COLOR]                    String lastToken = sentTokens [i][++j];
                        if (!stopWords.contains(lastToken))
                        {  finalTokens[k][m] = lastToken; m++; }
    
                        // after analyzing last token, move to analyzing the next sentence
    
                        continue newSentence;
    
                        }
                }
    
                k++;    // next sentence in finalTokens array
            }
    
            // Test code to print finalTokens array
               for(i=0; i < NO_OF_SENTENCES; i++) {
                   for (j=0; j < finalTokens[i].length; j++) 
                       System.out.print( finalTokens[i][j] + " " );
    
                   System.out.println();
               }
    
    
    
        }
            catch (Exception e) {
                e.printStackTrace();
            }
    }
    i have printed the indices i & j at the entry of their respective for loops...it all works fine for the first iteration of the loop..but when the loop is about to reach its end...i have printed again the value of 'i'..this time it comes out as 14...

    it starts the first iteration with 0...
    does not get manipulated anywhere in the loop...
    and just by the end of (only) first iteration, it prints the value as 14

    i mean this is seriously the WEIRDEST error i have come across ever while working with Java...it throws up an ArrayIndexOutOfBounds Exception just before the final if block..its like MAGIC....you do nothing on the variable in the code, still the value changes...HOW CAN THIS HAPPEN?? have u had a similar error before? can u help me solve it? plz..my project is stuck bcoz of this stupid error...

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    Sorry, but that's too much code for me to read. Call us lazy, but we don't always have a ton of time to devote to a single post.

    I'd suggest you throw together an SSCCE, perhaps just a main method with the bare minimum required to show us what's going on.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    runedog48 is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Edit: nver mind
    Last edited by runedog48; 04-14-2011 at 02:31 PM. Reason: Bad suggestion.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    The first thing I notice is that i and j are not declared in eliminateStopWords, which means they must be declared at a higher scope. My bet is you are reusing them elsewhere. Is there a reason you don't want to declare them locally, like this?

    Java Code:
    for([B]int[/B] i=0; i<...

  5. #5
    Navin Israni is offline Member
    Join Date
    Feb 2011
    Posts
    11
    Rep Power
    0

    Default

    @toadaly YES!! that was the error actually...the loop counters i,j were declared globally and the isCollocation method called another method loadMapFromfile() which manipulates them..

    and as it turns out..i learnt a good lesson in programming today,..never ever declare loop counters global..always keep them local...and I already made the changes in the entire program

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Not just loop counters. Declare ALL variables in the tightest possible scope.

    db

  7. #7
    latriactank is offline Member
    Join Date
    Apr 2011
    Location
    Saudi Arabia
    Posts
    1
    Rep Power
    0

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Indeed, and also declare them as restrictively as possible, keeping in mind that in Java, the default class level declaration allows any class in the package to directly alter contents, which is almost never a good a practice.

Similar Threads

  1. Replies: 0
    Last Post: 01-21-2011, 12:43 PM
  2. Replies: 5
    Last Post: 08-13-2010, 10:04 AM
  3. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  5. Replies: 1
    Last Post: 10-26-2009, 04:38 PM

Posting Permissions

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