java.lang.NullPointerException始终在第三个循环周期

时间:2015-10-06 13:53:35

标签: java string loops bufferedreader

我这里有一个奇怪的情况。我有一个简单的代码循环通过文本文件来获取邮件ID。如果没有可用的邮件ID,则代码将休眠 - >继续 - >开始的时候。 每次我检查我的字符串中是否有任何东西。我不会再次睡觉再试一次。 我检查str.length()< 5(覆盖空和null)/我试过str.isEmpty + str.equals(null)。 在第3个周期中的所有时间都会发生异常。

代码

 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();
        }
    }

异常堆栈跟踪

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)

0 个答案:

没有答案