0

I have a strange situation here. I have a simple code looping through a text file for getting mail id's. If there is no mail id available then the code will sleep -> continue -> start of while. Every time i check whether i have anything in my string.IF no i will sleep again and try again. I have check on str.length()<5 (covering empty and null)/ i tried str.isEmpty + str.equals(null). All the time in the 3rd cycle the exceptions happens.

Code

 public void sendButtonActionPerformed(java.awt.event.ActionEvent evt)
    {
        /* reading from text file at specific path*/
         String str; 

         String attachment= "";
         //String[] info = new String[2];
        // int size = 0 ,i =0;
         try{
         FileReader fr=new FileReader("D:/data.txt");
         BufferedReader br=new BufferedReader(fr);
          ///read line from the file upto null

         while(true)
         {
            str=br.readLine();
            System.out.println(str);


        /*  if( str.equals(null) )
            {
                 System.out.println("sleeping1");
                 Thread.sleep(2000);    
                 System.out.println("woke1");
                 continue;
            }*/
            if (str.length()<5)
            {
             System.out.println("sleeping");
             Thread.sleep(2000);    
             System.out.println("woke");
            continue;

            }
            globalMailCount++;

            String[] info = str.split(";");
            toAddress = info[0];
            attachment = info[1]+ "";
          //  System.out.println("Email:"+toAddress+"image:"+attachment);

            //System.out.println(info[i]);
            ccAddress = "navaljosh@gmail.com";
            subject = "Thanks from Jabong today";
            message = "Hi, Thanks for being part of the event";
            receipients = toAddress + "," + ccAddress;

            receipientsList = receipients.split(",");

            SendMailUsingAuthentication mailUsingAuthentication =
                    new SendMailUsingAuthentication();
            try {
                mailUsingAuthentication.postMail(receipientsList,
                        subject, message, "navaljosh@gmail.com", "sanmacs88", attachment);
            } catch (MessagingException messagingException) {
                messagingException.printStackTrace();
            }

          }
      //   br.close();
         }catch(IOException e)
         {

         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Exception stack trace

sleeping
woke

sleeping
woke

sleeping
woke
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
null
    at mailSent.MailSender.sendButtonActionPerformed(MailSender.java:169)
    at mailSent.MailSender$1.actionPerformed(MailSender.java:65)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Naval Joshi
  • 11
  • 1
  • 7
  • WHere does the exception happen? – StephaneM Oct 06 '15 at 13:56
  • 3
    `str.equals(null)` The only situation where this _could_ be true is, if `str` is `null` and do you think you could call `equals` on `str` when it is `null`? So please do a bit of research to find the correct way to check if a variable is `null` or not. – Tom Oct 06 '15 at 13:56
  • Also what is in your input file? – Sybren Oct 06 '15 at 13:56
  • Exception happened on this check "if (str.length()<5)" – Naval Joshi Oct 06 '15 at 14:01
  • INPUT FILE : data.txt IN DRIVE d: – Naval Joshi Oct 06 '15 at 14:01
  • 2
    Then `str` is null... which we can already see, because when you printed out str, it printed out "null". What did you expect to happen when your reader reached the end of the input? – Jon Skeet Oct 06 '15 at 14:01
  • It happens, because `br.readLine()` returns `null` if there is nothing more to read, so you _need_ to _correctly_ check for `str` being `null`, before you check the length. – Tom Oct 06 '15 at 14:02
  • i tried str== null same issue and if br.readline returns NULL at the end of file. It should not have gone into sleep check for once. It goes to sleep twice before crashing.Thats what i want to check when i dont get a string in retrun sleep and try again. – Naval Joshi Oct 06 '15 at 14:04
  • If you really tried `str==null`, then something else is wrong. *"if br.readline returns NULL at the end of file. It should not have gone into sleep check for once"* Makes no sense to me. It goes into these sleep checks, because the read line _wasn't_ `null`, the length was just below 5 characters. – Tom Oct 06 '15 at 14:08
  • Thanks got it. The sequence was wrong. :( – Naval Joshi Oct 06 '15 at 14:11

0 Answers0