简体   繁体   中英

How to get data fron beanclass in springMVC?

How to get data from beanclass in springMVC? I am trying but i got an error.

In Spring MVC I didnt get data from bean class

This is my bean class

    public class EmailBean
    {
        private Long id;
        private String from;
        private String to;
        private String subject;
        private String content;
        private String status;

        //getter and setter
    }

This is my Email sender

    public class EmailSender extends Thread
    {
        private EmailBean eb;

        public EmailBean getEb()
        {
            return eb;
        }

        public void setEb(EmailBean eb)
        {
            this.eb = eb;
        }

        public void run()
        {
            sendSingleEmail(eb);
        }

        public static void sendSingleEmail(EmailBean eb)
        {

            System.out.println(eb.getTo());
            System.out.println(eb.getSubject());
            System.out.println(eb.getContent());

        }
    }

This is my main class

    public class TestMail {

        public static void main(String[] args)
        {

            try
            {
                EmailBean eb=new EmailBean();
                eb.setFrom("xxx@gmail.com");
                eb.setTo("yyyyy@gmail.com");
                eb.setSubject("Testing Email Subject");
                eb.setContent("Testing Email Content");
                eb.setId(1L);
                eb.setStatus("100");

                EmailSender es=new EmailSender();

                es.start();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }   
    }

When I run this TestMail it wont work why? I git an error

    Exception in thread "Thread-0" java.lang.NullPointerException
        at com.candidjava.springmvc.service.EmailSender.sendSingleEmail(EmailSender.java:36)
        at com.candidjava.springmvc.service.EmailSender.run(EmailSender.java:30)

Just a simple mistake you made in main method, before starting the thread just call the setter method to set the EmailBean information, find the below correct code

 public class TestMail {

    public static void main(String[] args)
    {

        try
        {
            EmailBean eb=new EmailBean();
            eb.setFrom("xxx@gmail.com");
            eb.setTo("yyyyy@gmail.com");
            eb.setSubject("Testing Email Subject");
            eb.setContent("Testing Email Content");
            **eb.setId(1L);**
            eb.setStatus("100");

            EmailSender es=new EmailSender();
            es.setEb(eb);
            es.start();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }   
}

If you are new to spring mvc just try some simple spring hello world example to understand the basic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM