Getting Stuck on a Programming Problem

by Trevor Page on November 16, 2012

We’ve all been there before. We’re compiling or running some code and it’s just not working. You could be seeing things like Java exceptions in your logs or maybe your IDE is telling you there’s something wrong with your code and it’s being very cryptic.

How on earth are we supposed to go on from this point? If you’re fairly new to programming, you’re likely going to be dead in the water until you get some help.

So where can you go to get that help? That’s what I’m going to talk about today. I’ve had tons of questions come in from beginner Java programmers who get stuck in a certain spot and have no idea where to turn for help. Here are the places I would turn to for help:

How to Program with Java.com

If you are following along with this site when learning the Java language, I would love it if you would ask your questions in the comments section on any of these posts. Asking your questions here helps in the following ways:

  1. I just sincerely enjoy helping people out.
  2. All the other readers of this site will benefit from seeing the answer to your question, as they likely have the SAME question too!
  3. Gives me ideas on other topics that I can cover if I notice the same questions coming in.

Stackoverflow.com

Whenever I have been REALLY stuck on a complex programming problem, I have turned to this site with great success. You just ask your question, and there are literally TONS of programmers at your disposal who will try to help you get to the bottom of your problem. Many times I’ve had developers solve my problems by making me realize that the overall design of my solution needs to be tweaked. To me, this is the best solution to any problem, because if I had just tried to “hack” in a solution to a poor design, I would have WAY more problems in the future. Better to solve the root of the problem instead of the symptoms right?

So, overall, this website is extremely helpful as it can “crowdsource” the proper solution.

Google

This seems like a no-brainer, and for the most part it is. But here’s a few tricks that I use whenever I’m trying to solve a problem. I typically solve run-time errors in this fashion:

  1. Isolate the exception in your logs/console
  2. In your logs/console, find out if you can see the words “Caused By:”
  3. Copy/paste the line of words after “Caused By:” into Google and search

This tactic is good for finding related articles on people who have had the same problems as you, and seeing how they went about solving them.

Note that if you don’t any search results, it’s very likely that you have some information in the line that you copy/pasted that’s specific to YOUR program. See if you can isolate any words that are specific to your program and remove just those words, then do the search again.

What if you don’t see “Caused By:”

Well, no worries, the trick I do here is I can the exception until I can see a Class that I recognize. Let’s take a look at the following example stack trace:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
	at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
	at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
	at src.dao.UsersDao.getUser(UsersDao.java:67)
	at src.service.UsersService.getUserForUserDetails(UsersService.java:89)
	at src.service.UserDetailsServiceImpl.loadUserByUsername(UserDetailsServiceImpl.java:35)
	at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:81)
	at org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
	at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
	at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
	at org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
	at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:194)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
	at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
	at java.lang.Thread.run(Thread.java:662)

This is a problem I recently had with one of the websites I created, and here’s how I went about solving it.

  1. If you look at the stacktrace error as a whole, it seems very intimidating, so I like to simplify it by just looking at the blue underlined text.
  2. I scan them one by one from top to bottom until I find a Class name that I recognize
  3. Note: The first blue underlined text says org.hibernate.HibernateException so this means that the problem has to do with the Hibernate framework… good to know

  4. SpringSessionContext.java – Means nothing to me, so I keep looking
  5. SessionFactoryImpl.java – Nope
  6. UsersDao.java – Aha! I recognize that class, as I created it to save the information for the Users that belong to my program.
  7. Now I look at the actual error: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
  8. So I take that error and copy/paste it into Google armed with the knowledge that it has to do with the Hibernate framework and that it’s happening in my UsersDao class, which I use to save user information
  9. The first result is a question from stackoverflow click here if you want to see it. And what do you know, I scroll down in the stackoverflow question until I see an answer that was given by a user and it has a green checkmark to the left of it. This denotes that the user’s problem was solved by this answer. The person answering the question says that you likely need to include the @Transactional annotation on your class.
  10. I put the @Transactional annotation on my UsersDao class to test and see if this fixes my problem… and what do you know, my problem is SOLVED!!!

Trial and Error

I generally go through a few iterations of the Google approach by using a trial and error approach. If the first proposed solution I find on Google doesn’t work, I try a few more. If I’ve been stuck on the same problem for hours, I do something interesting:

I step away from the problem!

I can’t stress this technique enough. I have wasted so many hours of my life by hammering away at a problem with no success, only to take a break or go to bed, and then come back to the problem and solve it in 5 minutes. It’s just the way we’re wired as humans, as I’ve done the same thing with learning to play a song on the guitar or piano, you can practice your butt off and never really play the song very well… but after you take a break and come back, you’ve made surprising progress. Coding is no different, so take a break!

And finally, if I haven’t been able to solve the problem over the course of many days, I’ll post the question on stackoverflow and always get a solution from someone there… that’s just my approach, your mileage may vary, but your secret weapon is that you have ME to help you out should you be frustrated and pulling your hair out!

I hope that helps to shed some light on some troubleshooting techniques, now if you’re really serious about learning how to program with Java, I just launched a new website that aims at teaching you Java (as well as other technologies needed to be a real website developer). This tutorial series is very detailed and uses videos as its medium. Unfortunately a subscription to this website is not free, as I had to spend a fair bit of extra money on the hosting of both the website and the videos, and I purchased a neat tablet that allows me to write on the screen using a pen/tablet combination… this gives a real “classroom” feel to the tutorials :)

So if you’re interested in learning more (and learning with videos) check out javatutorialvideos.net and sign up for a free trial. Or, if you desperately want in, and you don’t want to pay, I’m running a fun contest to win a subscription to the site! Visit javavideotutorials.net/contest.html for more information :)

Take care everyone, and happy learning!

{ 15 comments… read them below or add one }

Leave a Comment

Powered by sweet Captcha

Previous post:

Next post: