title: Hibernate and Spring in Swing Application
body: <p>I'm a newbie in programming.I have problems in my Swing application.I suppose something wrong with session.I use Hibernate and configure it by means of Spring.When I press button I want to add info to database but I get NullPoinerException.Maybe I must code user interface another way?
  Need your help!Thanks.</p>
  
  <p>Here my code:</p>
  
  <p>MainFrame.java</p>
  
  <pre><code>public class MainFrame extends JFrame {
  
      public MainFrame(){
          setTitle("Title");
          setSize(300,300);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
          makeButtons();
          setVisible(true);
      }
      public void makeButtons(){
          JPanel panel=new JPanel();
          panel.add(makeLoginField());
          panel.add(makeLoginButton());
          panel.add(makePassField());
          panel.setVisible(true);
          this.add(panel);
      }
      public JButton makeLoginButton(){
          JButton loginButton=new JButton("Login");
          loginButton.addActionListener(new Action());
          return loginButton;
      }
      public JTextField makeLoginField(){
          JTextField loginField=new JTextField();
          loginField.setSize(new Dimension(134, 20));
          return loginField;
      }
      public JPasswordField makePassField(){
          JPasswordField passField=new JPasswordField();
          passField.setSize(new Dimension(134, 20));
          return passField;
      }
      public static void main(String[] args) {
           JFrame m=new MainFrame();   
      }   
  }
  </code></pre>
  
  <p>Action.java</p>
  
  <pre><code>class Action implements ActionListener{
      @Autowired
      private UserServiceInterface userService;
  
      public void setuserService(UserServiceInterface userService) {
          this.userService=userService;
      }
      public void actionPerformed (ActionEvent e){
          User u=new User();
          u.setName("HellofromGUI");      
          userService.addUser(u);
      }
  }
  </code></pre>
  
  <p>UserService.java</p>
  
  <pre><code>@Transactional
  public class UserService implements UserServiceInterface{
      @Autowired
      private UserDaoInterface dao;
  
      public void setDao(UserDaoInterface dao) {
          this.dao = dao;
      }
      public void addUser(User u){
      dao.insertRow(u);
      }
      public List getData(){
          return dao.getDBValues();
      }
  }
  </code></pre>
  
  <p>UserDao.java</p>
  
  <pre><code>public class UserDao implements UserDaoInterface{
      @Autowired
      private SessionFactory sessionFactory;
  
      public void insertRow(User user) {
          Session session = null;
          session = sessionFactory.getCurrentSession();
          session.save(user);
  
      }
      public void setSessionFactory(SessionFactory sessionFactory) {
          this.sessionFactory = sessionFactory;
      }
       public List getDBValues() {
              Session session = sessionFactory.getCurrentSession();
              List&lt;User&gt; users = session.createCriteria(User.class).list();
              return users;
      }
  }
  </code></pre>
  
  <p>beans.xml</p>
  
  <pre><code>&lt;beans&gt;
  
      &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /&gt;
  
      &lt;bean id="userdao" class="dao.UserDao"&gt;
          &lt;property name="sessionFactory"&gt;
              &lt;ref bean="sessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="userservice" class="service.UserService"&gt;
          &lt;property name="dao"&gt;
              &lt;ref bean="userdao" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="paymentdao" class="dao.PaymentDao"&gt;
          &lt;property name="sessionFactory"&gt;
              &lt;ref bean="sessionFactory" /&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
      &lt;bean id="paymentservice" class="service.PaymentService"&gt;
          &lt;property name="dao"&gt;
              &lt;ref bean="paymentdao" /&gt;
          &lt;/property&gt; 
      &lt;/bean&gt;
      &lt;bean id="usergui" class="ui.Action"&gt;
          &lt;property name="userService"&gt;
              &lt;ref bean="userservice" /&gt;
          &lt;/property&gt; 
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>