1

I'm trying to learn Spring MVC and Hibernate. When I try to run tests I have "Exception in thread "main" java.lang.NullPointerException".

public interface VacancyDAO

package pro.asfert.jobparser.dao;
public interface VacancyDAO {
void LoadDataBase(String query);
void FindVacancy(String queries);}

public class VacancyDAOImpl

package pro.asfert.jobparser.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class VacancyDAOImpl implements VacancyDAO {

@Autowired
private SessionFactory sessionFactory;

public void LoadDataBase(String query) {
    sessionFactory.getCurrentSession().createQuery(query);
}

public void FindVacancy(String queries) {
    if (queries.contains("По вашему запросу: ")) {
        System.out.print(queries);
    } else {
        sessionFactory.getCurrentSession().createQuery(queries);
    }
}
}

public interface VacancyService

    package pro.asfert.jobparser.service;
    public interface VacancyService {
    void LoadDataBase();

    void FindVacancy(String queries);
    }

**public class VacancyServiceImpl**

    package pro.asfert.jobparser.service;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    import pro.asfert.jobparser.dao.VacancyDAO;
    import pro.asfert.jobparser.domain.Parser;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;

    @Service
    public class VacancyServiceImpl implements VacancyService {

    @Autowired
    private VacancyDAO VacancyDAO;

    public static void main(String[] args) {
        VacancyServiceImpl vacancyService = new VacancyServiceImpl();
        vacancyService.FindVacancy("test");
    }


    /*  deleted  */
    @Transactional
    public void LoadDataBase() {
         VacancyDAO.LoadDataBase(query);
    }

    @Transactional
    public void FindVacancy(String queries) {
        VacancyDAO.FindVacancy(sqlQuery);
    }

application-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/cache
       http://www.springframework.org/schema/cache/spring-cache.xsd ">

    <context:annotation-config />
    <context:property-placeholder location="classpath:jdbc.properties" system-properties-mode="ENVIRONMENT"/>
    <context:component-scan base-package="pro.asfert.jobparser.dao"/>
    <context:component-scan base-package="pro.asfert.jobparser.service"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory">

        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value = "${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.databaseurl}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref = "dataSource"/>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.connection.charSet">UTF-8</prop>
            </props>
        </property>
    </bean>
</beans>

hibernate.cfg.xml

 <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <session-factory>
        <mapping class = "pro.asfert.jobparser.domain.Vacancy"></mapping>
    </session-factory>
    </hibernate-configuration>

Vacancy

    package pro.asfert.jobparser.domain;
import javax.persistence.*;


@Entity
@Table(name = "Vacancies")
public class Vacancy {
    public Vacancy() {
    }

    @Id
    @Column(name = "id")
    @GeneratedValue
    private Integer id;

    @Column (name = "vacancy")
    private String vacancy;

    @Column (name = "salary")
    private String salary;

    @Column (name = "experience")
    private String experience;

    @Column (name = "education")
    private String education;

    @Column (name = "employer")
    private String employer;

    @Column (name = "details")
    private String details;

    @Column (name = "hr")
    private String hr;

    @Column (name = "url")
    private String url;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getVacancy() {
        return vacancy;
    }

    public void setVacancy(String vacancy) {
        this.vacancy = vacancy;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }

    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public String getEmployer() {
        return employer;
    }

    public void setEmployer(String employer) {
        this.employer = employer;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public String getHr() {
        return hr;
    }

    public void setHr(String hr) {
        this.hr = hr;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    }
ath dare
  • 11
  • 1
  • 2
    Full stack trace please, along with line which is troubling you with NPE. and how are you running your application? – SMA Mar 22 '15 at 15:47
  • Exception in thread "main" java.lang.NullPointerException at pro.asfert.jobparser.service.VacancyServiceImpl.FindVacancy(VacancyServiceImpl.java:100) at pro.asfert.jobparser.service.VacancyServiceImpl.main(VacancyServiceImpl.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) – ath dare Mar 22 '15 at 16:07
  • I Run VacancyServiceImpl main() – ath dare Mar 22 '15 at 16:08
  • And where are you creating application context? – SMA Mar 22 '15 at 16:24

1 Answers1

1

You're creating the service with new here:

public static void main(String[] args) {
        VacancyServiceImpl vacancyService = new VacancyServiceImpl();
        vacancyService.FindVacancy("test");
    }

That way Spring is not involved and does not know anything about this object.

How to fix it:

register your dao and service as spring beans. initialize the context and get the service from the context and call your method.

ApplicationContext ctx = new ClassPathXmlApplicationContext("application-context.xml");
VacancyServiceImpl serv = ctx.getBean(VacancyServiceImpl.class);
serv.FindVacancy("test");
Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145