Q:

Null Pointer Exception in JDBCTemplateObject.query() in listFriends method in JDBCTemplate class

I'm trying the retrieve the data from Database and display it on the UI. There's a null pointer exception in FriendJDBCTemplate class. Can anyone please help me with this. 
FriendJDBCTemplate.java:
package com.initial.hellocontroller;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class FriendJDBCTemplate implements StudentDAO {
    private DataSource dataSource;
    private JdbcTemplate jdbcTemplateObject;

    public void setDataSource(DataSource dataSource) {
       this.dataSource = dataSource;
       JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
    }
    public void create(String name, String Email) {
        String SQL = "insert into Friend (name, Email) values (?, ?)";
        jdbcTemplateObject.update( SQL, name, Email);
        System.out.println("Created Record Name = " + name + " Email = " + Email);
        return;
    }
    public Friend getFriend(String Telephone) {
        String SQL = "select * from Friend where Telephone = ?";
        Friend friend = jdbcTemplateObject.queryForObject(SQL, new Object[] {Telephone}, new FriendMapper());
        return friend;
    }
    public List<Friend> listFriends() {
        String SQL = "select * from Friend";
        List <Friend> friends = jdbcTemplateObject.query(SQL, new FriendMapper());
        return friends;
    }
    public void delete(String Telephone) {
        String SQL = "delete from Friend where Telephone = ?";
        jdbcTemplateObject.update(SQL, Telephone);
        System.out.println("Deleted Record with ID = " + Telephone);
        return;
    }
    public void update(String Telephone, String name){
        String SQL = "update Friend set name = ? where Telephone = ?";
        jdbcTemplateObject.update(SQL, name, Telephone);
        System.out.println("Updated Record with Telephone = " + Telephone );
        return;
    } 
}

FriendMapper.java
package com.initial.hellocontroller;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;

public class FriendMapper implements RowMapper<Friend> {
    public Friend mapRow(ResultSet rs, int rowNum) throws SQLException{
        Friend friend = new Friend();
        friend.settelephone(rs.getString("Telephone"));
        friend.setname(rs.getString("name"));
        friend.setemail(rs.getString("Email"));
        return friend;
    }
}

Friend.java
package com.initial.hellocontroller;
import java.util.List; 

public class Friend implements Listall {
    private String name; 
    private String Email; 
    private String Telephone; 

    public void setname(String name){
        this.name= name; 
    }
    public String getname(){
        return name;
    }

    public void setemail(String Email){
        this.Email = Email; 
    }
    public String getemail(){
        return Email; 
    }
    public void settelephone(String Telephone){
        this.Telephone = Telephone;
    }
    public String gettelephone(){
        return Telephone; 
    }

    public List<Friend> Listallfriends(){
        FriendJDBCTemplate listall = new FriendJDBCTemplate();
        return listall.listFriends(); 
    }
}

HelloController.java
package com.initial.hellocontroller; 
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import org.springframework.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/* 
public class HelloController extends AbstractController 
{

    @override
    protected ModelAndView handleRequestInternal(HttpServletRequest   request, HttpServletResponse response) throws Exception 
    {
        ModelAndView modelandview = new ModelAndView("Hellopage");
        modelandview.addObject("welcomeMessage","Hi User, Welcome to the first  spring MVC tutorial");

        return modelandview; 
    }
}

*/

@Controller
public class HelloController {

    List<Friend> templist = new ArrayList<Friend>();
    Friend temp = new Friend();
    @RequestMapping(value = "/admissionForm",method = RequestMethod.GET)
    public ModelAndView getAdmissionForm(){
        ModelAndView model = new ModelAndView("Portfolio"); 
        return model; 
    }

    @RequestMapping(value="/submitAdmissionForm", method = RequestMethod.POST)
    public ModelAndView submitAdmissionForm(@RequestParam Map<String,String> reqPar){
        String name = reqPar.get("name"); 
        String email =reqPar.get("Email"); 
        String telephone =reqPar.get("Telephone");

        // taking the input from the form to update the data from database. 
        Friend friend = new Friend();
        friend.setname(name); 
        friend.setemail(email);
        friend.settelephone(telephone);

        ModelAndView model = new ModelAndView("HelloPage");
        templist = temp.Listallfriends(); 
        for (Friend items : templist){
            model.addObject("msg", "your total friends" +items);
        }
        return model;
    }
}

Dispatch-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.initial.hellocontroller"/>
    <mvc:annotation-driven/>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/</value>
        </property>
        <property name="suffix"> 
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="dataSource" 
          class =  "org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name = "driverClassName" value = "oracle.jdbc.OracleDriver"/>
        <property name = "url" value = "jdbc:oracle:thin:@localhost:1521:ORCL"/>
        <property name = "username" value = "system"/>
        <property name = "password" value = "hello123"/>
    </bean>
</beans>

error trace:
java.lang.NullPointerException
    com.initial.hellocontroller.FriendJDBCTemplate.listFriends(FriendJDBCTemplate.java:28)
    com.initial.hellocontroller.Friend.Listallfriends(Friend.java:37)
    com.initial.hellocontroller.HelloController.submitAdmissionForm(HelloController.java:63)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:498)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

A:

Do you have two jdbcTemplateObject with same name. How is it even compiling.
Change JdbcTemplate jdbcTemplateObject = new JdbcTemplate(dataSource);
To 
this.jdbcTemplateObject  = new JdbcTemplate(dataSource); in setDataSource.
You have your dataSource bean defined in your spring config, but you are not injecting it to FriendsJDBCTemplate anywhere. You need to either use annotation "@Autowired" or manually wire beans in your spring xml. Just declaring beans won't work. You have to tell Spring container where they should be injected.
@Autowired
private DataSource dataSource;

