6 Replies Latest reply: Feb 15, 2007 1:39 AM by 807599 RSS

    Stream problem or loop problem

    807599
      hello friends!
      i tried this program from one of the reference book
      while executing with for loop, its working fine
      but with do while loop, its not working fine , giving null pointer exception
      plz help me

      import java.io.*;

      class BRReadLine
      {
           public static void main(String[] args) throws IOException,NullPointerException
           {   String str[] = new String[100];
                BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
                System.out.println("Enter the line of text");
                System.out.println("Enter stop to quit");
                //int i=0;
                /*do
                {
                str=br.readLine();
                i++;
                }while(str[i].equals("stop"));*/
      for(int i=0;i<100;i++)
                {
                     str[i]=br.readLine();
                     if(str[i].equals("stop")) break;
                }
      //int i=0;
                System.out.println("The line u entered:");
                /*do
                {
                System.out.println(str[i]);
                i++;
                }while(str[i].equals("stop"));*/
      for(int i=0;i<100;i++)
                {
                     if(str[i].equals("stop")) break;
                     System.out.println(str[i]);
                     }
      }
      }
        • 1. Re: Stream problem or loop problem
          807599
          That code doesn't even compile. You cannot assign a String to a String[].

          The reason why you get a nullpointer is that you increment your index and then go check if the value in the array is not stop. If you enter "something" in your array you have:
          0                1             2          3 ...
          something       null          null        null
          You increment i and call equals on str[1] which is null... Either check for
          !str[i++].equals("stop");
          and remove the i++ from within your loop or check for
          !str[i-1].equals("stop");
          Also notice the '!' in front of the equals test also notice that you have to use str[ i ] instead of just str...
          • 2. Re: Stream problem or loop problem
            807599
            import java.io.*;

            class BRReadLine
            {
            public static void main(String[] args) throws
            s IOException,NullPointerException
                 {   String str[] = new String[100];
            BufferedReader br = new BufferedReader( new
            ew InputStreamReader( System.in));
                      System.out.println("Enter the line of text");
                      System.out.println("Enter stop to quit");
                      //int i=0;
                      /*do
                      {
                      str=br.readLine();
                      i++;

            U increment i here
                      }while(str.equals("stop"));*/
            here u print str[i ] but i is incremented now and str[i] is null. that causes the exception
            try with str[i-1]
            for(int i=0;i<100;i++)
            str=br.readLine();
                           if(str[i].equals("stop")) break;
                      }
            //int i=0;
            two variables with same name in the same scope
            use j or something
            m.out.println("The line u entered:");
                      /*do
                      {
                      System.out.println(str);
                      i++;
                      }while(str[i].equals("stop"));*/
            for(int i=0;i<100;i++)
            (str[i].equals("stop")) break;
                           System.out.println(str[i]);
                           }
            }
            }

            u have done another mistakes here
            while(str.equals("stop"))
            it should be
            while(!str.equals("stop"))

            This should work
            public static void main(String[] args) throws IOException,NullPointerException
                 { String str[] = new String[100];
                 BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
                 System.out.println("Enter the line of text");
                 System.out.println("Enter stop to quit");
                 int i=0,j=0;
                 do
                 {
                 str=br.readLine();
                 i++;
                 }while(!str[i-1].equals("stop"));
                 /*for(int i=0;i<100;i++)
                 {
                 str[i]=br.readLine();
                 if(str[i].equals("stop")) break;
                 }*/

                 System.out.println("The line u entered:");
                 do
                 {
                 System.out.println(str[j]);
                 j++;
                 }while(!str[j].equals("stop"));
                 /*for(int i=0;i<100;i++)
                 {
                 if(str[i].equals("stop")) break;
                 System.out.println(str);
                 }*/
                 }
            • 3. Re: Stream problem or loop problem
              807599
              first of all the code has to use the str , also the while loop checks for the array element after the index is incremented...and it will be true only if the input is "stop"...so a negation has to be added there...
              import java.io.*;
              
              class BRReadLine
              {
              public static void main(String[] args) throws IOException,NullPointerException
              { String str[] = new String[100];
              BufferedReader br = new BufferedReader( new InputStreamReader( System.in));
              System.out.println("Enter the line of text");
              System.out.println("Enter stop to quit");
              int i=0;
              do
              {
              str=br.readLine();
              i++;
              }while(!str[i-1].equals("stop"));
              /*for(int i=0;i<100;i++)
              {
              str[i]=br.readLine();
              if(str[i].equals("stop")) break;
              }*/
              i=0;
              System.out.println("The line u entered:");
              do
              {
              System.out.println(str[i]);
              i++;
              }while(!str[i-1].equals("stop"));
              /*
              for(int i=0;i<100;i++)
              {
              if(str[i].equals("stop")) break;
              System.out.println(str[i]);
              }*/
              }
              }
              • 4. Re: Stream problem or loop problem
                807599
                You'll also have an IndexOutOfBoundsException if your user enters more than 100 strings before entering stop. You should also check this in your while loop test.
                • 5. Re: Stream problem or loop problem
                  807599
                  out of the topic , i just noticed something... if I type something like str [ i ] without spaces and without the [ code ] [ /code ] tag...it displays only the str does not display the [ i ] in my text...it will be helpful whenever someone posts a piece of code, enclose them within the code tags...
                  • 6. Re: Stream problem or loop problem
                    807599
                    notice that you have to use str[ i ] instead of just
                    str...
                    actually he has not used str instead of
                    str. I saw it when I quoted the original. it was in his code and 
                     is missing in the post, 

                    however Vasu please use code tags when u post codes