wood burning stoves 2.0*
The moose likes Spring and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Exception in thread "main" java.lang.NullPointerException (cannot connect to database)" Watch "Exception in thread "main" java.lang.NullPointerException (cannot connect to database)" New topic
Author

Exception in thread "main" java.lang.NullPointerException (cannot connect to database)

khamhielle gabriel
Greenhorn

Joined: Aug 15, 2011
Posts: 19
Hi guys

would you kno what i am missing?

i am just new to spring and i was ask first to connect to the database using bean.
found a helpful tutorial about it but it still seems not working with me.

im getting this error :




would appreciate your help.

Thank you and God Bless


here is my java ..

DashboardDetailsDaoImpl .java




now here is the ApplicationContext in which i place the connection in a bean.

ApplicationContext .xml



thanks so much
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Ok so this is a stand alone application? I see you are using Spring 2.5? Is that a requirement? If it is not then upgrade to the newest. Currently 3.1.2

This is sufficient



get rid of this



I would use JDBCTemplate you are not gaining any of the advantages of exception translation or other benefits that Spring provides.

You are not bootstrapping your application correctly. @Autowired only happens in Spring managed beans. You are actually not even bootstrapping Spring at all.

Your main method needs to have something more like this.



Also your transaction manager is not going to work outside of a JEE container.

try this:



Also be aware that DriverManagerDataSource is fine for testing but it should not be used in production.


I think you may need to take a few steps back, buy a book or start with some beginner tutorials and work your way up to what you are trying to do here.

You can start with some of the suggestions I gave but you will find there is a lot to learn, so I would start with the basics like dependency injection and then start adding things as your understanding of what Spring is and how it works increases.

Good Luck!


[How To Ask Questions][Read before you PM me]
khamhielle gabriel
Greenhorn

Joined: Aug 15, 2011
Posts: 19
Hi Bill


Thank you for your reply
i admit im not yet familiar with what Spring can truly offer.
so i kind off, find it difficult still, to grasp some functionality.

anyways, i have tried to implement your suggestion like this.






then

applicationContext.xml



fyi applicationContext.xml resides under target/DasboardDetails/WEB-INF



but now im having this error :






would again appreciate your help ..
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

In the stack trace it says

Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist

It is not finding applicationContext.xml.

in the code you posted it looks like you are loading Spring.xml not applicationContext.xml. What is actually the name of the file? Also is the file directly on the class path? i.e src/main/resources? If it is under a subdirectory then you must specify this when you load it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.NullPointerException (cannot connect to database)
 
Similar Threads
spring wont inject the entitymanager
Is there a tool to get source file list?
Spring AOP aspect not working.
AOP not getting invoked on my Target Bean Method --> Singer.perform() method
spring security 3.1 login not working.