Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:07:35+00:00 2026-06-14T23:07:35+00:00

Please pardon me since I am a Spring beginner. I was following the example

  • 0

Please pardon me since I am a Spring beginner. I was following the example in the book but I only changed the database from Derby to MySQL.

My Main class is fairly simple:

public class Main {

public static void main(String [] args)
{
    ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");

    VehicleDAO dao= (VehicleDAO) context.getBean("vehicleDao");
    Vehicle vehicle = new Vehicle("TEM0001", "Red", 4, 4);
    dao.insert(vehicle);
}

}

It creates a Data Access Object and try to add the new Vehicle object into the the DAO.The Vehicle class is very straightforward, an object class with four fields.

The VehicleDAO class is pasted here:

http://pastebin.com/ekB2Rb40

and my bean file is like:

<bean id="dataSource"
 class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="org.gjt.mm.mysql.Driver" />
   <property name="url"
   value="jdbc:mysql://localhost:3306/vehicle" />
  <property name="username" value="root" />
  <property name="password" value="3324911" />
  <property name="initialSize" value="2" />
  <property name="maxActive" value="5" />
</bean>
<bean id="vehicleDao"
  class="com.apress.springrecipes.vehicle.JdbcVehicleDao">
  <property name="dataSource" ref="dataSource" />
</bean>

As you can see above, I am always getting a null pointer error at line

conn= dataSource.getConnection();

So I suspect something is wrong with the MySQL connection, but I already included “mysql-connector-java-bin.jar” in the classpath in my Eclipse.

Thanks for any help in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:07:36+00:00Added an answer on June 14, 2026 at 11:07 pm

    You have a typo in your setter

      public void setDataSource(DataSource datasource)
            {
                    this.dataSource=dataSource;
            }
    

    So when Spring injects the datasource, its value is not saved, I wonder if your IDE checks the source code for this kind of errors.

    P.S. You should not open database connections manually in your DAO classes we have JdbcTemplate in Spring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

please pardon since I am a UNIX beginner. I wanna write a shell script
Please pardon this Spring beginner question. I am reading chapter 4 of first edition
Please pardon me for being a complete newbie, but I'm following a tutorial (
Please look the following query: SELECT ID, START, END FROM TABLEA the result is:
Please pardon me if this has been asked before, but I'm wondering if there
Please pardon me asking this rudimentary question, but let me know if anyone knows
I am an iOS newbie, so please pardon me if this is a beginner
Please pardon me for a n00bish question. Please consider the following code: public class
I am a beginner to JUnit and design patterns, so please pardon me. I
Please pardon the beginner question.I met this code fragment on the internet: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.