Results 1 to 11 of 11
  1. #1
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default won't go into else-if???

    Hello all, lately I've been having difficulty with this section of code. Basically what I am doing is reading a file and trying to say "if the file has text in it on a specific row, display it's contents in the Jlabel" and "if there is no text then display " no further instructions"".


    currently I keep getting an out of bounds exception if in the file there is nothing in a specific row in my csv file. I am using the opencsv library so if anything looks strange, that's why.


    Java Code:
     initComponents();
            templateFile = openFile();
            
            
            if(templateFile == null){ // if template file has nothing in it
              System.exit(0);  // quit application
            }
            File file = fileSelect.getSelectedFile();  //
            CSVReader reader = new CSVReader(new FileReader(file));
            String [] nextLine;
         // int returnVal = chooser.showOpenDialog(parent);
       // if(fileSelect.APPROVE_OPTION == 1) {
        //   System.out.println("You chose to open this file: " +fileSelect.getSelectedFile().getName());
        
        
            while ((nextLine = reader.readNext()) != null) { 
            if(nextLine[blah]!=null){  // if there are comments 
                jLabel11.setForeground(Color.red); //turn jlabel red
                jLabel11.setText(nextLine[blah]);  // show the instructions
                System.out.println("if blah isn't null");
            }
            else {
                if(nextLine[blah] == null){ //if there is no text in the column
               System.out.println("it's going into null");  //just here to let the console show me it's going into the else statement
                jLabel11.setForeground(Color.green); //set jlabel green
            jLabel11.setText("No Additional Instructions"); //display "No Additional Instructions
            System.out.println("made it to second if");
            }
            }
         }
    Just outside the loop I set
    Java Code:
     int blah = 29;
    so the code is understanding to go to row 29 and it succeeds when text is there and displays that text in the jlabel. However if there is no text I keep getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 29
    at evansinterface.mainWindow.<init>(mainWindow.java:5 7)
    at evansinterface.EvansInterface.main(EvansInterface. java:29)



    Line 57 is line 17 in this code.


    What I am not understanding is why is the code not going to the else statement? I told it if null set the text to "no additional instructions"

    as far as I'm aware the console confirms to me that the program never goes into the else-if. despite not meeting the "if" condition
    Last edited by innovo; 09-11-2015 at 02:12 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: won't go into else-if???

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 29
    at evansinterface.mainWindow.<init>(mainWindow.java:5 7)
    At line 57 the code uses an index (29) past the end of the array. Remember valid array indexes range from 0 to the array length-1.
    if there is no text
    The code should test that the index is not past the end of the array before using it. Use the array's .length field to test the length of the array.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: won't go into else-if???

    You are telling the code to go to row 30. Arrays have a zero-based index. Writing blah[29] will only work if your array has 30 items.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: won't go into else-if???

    Quote Originally Posted by Norm View Post
    At line 57 the code uses an index (29) past the end of the array. Remember valid array indexes range from 0 to the array length-1.

    The code should test that the index is not past the end of the array before using it. Use the array's .length field to test the length of the array.
    as it is now, the application works when there is text there. So I'm not getting how that's out of bounds. Shouldn't it be an array but with nothing in the last part of it?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: won't go into else-if???

    You haven't provided enough information. Is nextLine supposed to be an array? Is that what reader.readNext returns? Please provide a short executable example that demonstrates the problem.

    Regards,
    Jim
    Last edited by jim829; 09-11-2015 at 02:16 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: won't go into else-if???

    Quote Originally Posted by jim829 View Post
    You haven't provided enough information. Is nextLine supposed to be an array? Is that what reader.readNext returns? Please provide a short executable example that demonstrates the problem.

    Regards,
    Jim
    yes, it is supposed to be an array. Blah indicates what part of the array to go to.

  7. #7
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: won't go into else-if???

    Quote Originally Posted by SurfMan View Post
    You are telling the code to go to row 30. Arrays have a zero-based index. Writing blah[29] will only work if your array has 30 items.
    I am aware of this, but what do I do if it only has 28 since the 29th may not be used??

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: won't go into else-if???

    Assuming that you get a new array for nextLine each time, why not use nextLine.length. Then you can handle any number of returned values. This means that the readNext method must not return a fixed length array but one that changes based on number of items per line.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    innovo is offline Member
    Join Date
    Jul 2015
    Posts
    84
    Rep Power
    0

    Default Re: won't go into else-if???

    FIXED!!! I solved the problem by doing this instead

    Java Code:
     
            while ((nextLine = reader.readNext()) != null) { 
           if(nextLine.length==30){
                //if(nextLine[blah]!=null){  // if there are comments 
                jLabel11.setForeground(Color.red); //turn jlabel red
                jLabel11.setText(nextLine[blah]);  // show the instructions
                System.out.println("if blah isn't null");
           // }
           }
            
                if(nextLine.length<30){
               System.out.println("it's going into null"); 
                jLabel11.setForeground(Color.green);
            jLabel11.setText("No Instructions");
            System.out.println("made it to second if");
            }
            
            
         }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: won't go into else-if???

    Java Code:
    nextLine.length==30
    The code uses a hardcoded value (30) vs the contents of the variable: blah.
    If the value of blah is ever changed, the code might not work.
    The code would be better if it used the value of blah instead of the hardcoded 30:
    Java Code:
    nextLine.length>blah
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: won't go into else-if???

    So in other words, if the line contains less that 30 csv's, it is not considered a valid line.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Posting Permissions

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