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 }

Priti November 17, 2012 at 8:09 am

Hello , Want more details abt java programming and how to develop logical idea??? plz help me in this

Reply

Trevor Page November 17, 2012 at 9:02 am

Hi Priti,

Well this website is a great resource for learning about java programming, you can shoot over to my first tutorial and read through them all if you like. Just go here: http://howtoprogramwithjava.com/start-here

As for the second part of your question, I’m not sure I understand what you mean by “develop logical idea”. Can you elaborate on that?

Reply

tomek November 18, 2012 at 4:27 am

Likely annotation you used is in the wrong place it should be on the service layer.

Reply

fraser graham November 20, 2012 at 11:54 am

Hi, trevor im doing a java course from dundee university using BlueJ im completely new to java programming, I have recently bought your book how to program with java and downloaded it to my pc using a kindle app for my laptop. you have helped me out quite alot with your explanations on java. I am a bit stuck tho im using BlueJ that the program we where told to use, there is a section in your book that talks about packages and imports, you import a file to work along side a program you have written so it can read the information from. Im wondering if you know how to do this on bluej for example public class Vehicle.java file to import to the program how would i do this. cheers fraser graham

Reply

Trevor Page November 20, 2012 at 2:36 pm

Hi Fraser,

Great to hear that you’re so dedicated to learning the Java programming language! I’ll admit that I’ve never heard of BlueJ before, but I had a look at some of the documentation for this product. If you take a look at this pdf: http://www.bluej.org/tutorial/tutorial-201.pdf, and have a read around page 35 of the PDF, there’s a section that defines “10.2 Adding existing classes to your project”. I’m not positive, but this sounds like what you need. This way you can include the “Vehicle.java” class file into your existing project inside of BlueJ.

Give that a shot and let me know if it works out for ya.

Cheers mate

Reply

fraser graham November 20, 2012 at 3:06 pm

Hi trevor thanks for helping me out still a bit stuck i know how to import a package using bluej i would import a package for example HumanBeing.java you say you would import it to the main coded program how would you do this for example do u have to write code in 1 program then import it to the main program im a bit lost sorry.

cheers

Reply

Trevor Page November 20, 2012 at 3:52 pm

Hi again Fraser,

I’m not exactly sure what you’re asking… could you try re-wording your question?

One thing I’m noticing about your question, is that you say you know how to import a package. That’s actually a bit misleading, as you don’t actually import packages, you import Files within a package. Are you asking how to import a File? Or are you asking how to WORK with a file that you’ve already imported inside of another File?

Reply

fraser graham November 20, 2012 at 4:01 pm

sorry trevor yeah import a file within a package how does it work do you write code within the file then import it to the package do you have any examples how this works as i have never tried it before.

cheers

fraser graham November 21, 2012 at 7:05 am

How does .java files or .jar files work do you import them into a package do you write code in these files to work alongside the main code I have been trying to so some of the examples in your book but i cant do them cause of these files.

cheers

Reply

Trevor Page November 21, 2012 at 8:15 am

I’ll hopefully answer your other question as well as this one with the same answer.

When you create “Class” files, you are creating a “.java” file. This file belongs in a package, which belongs to one particular program. If you want to use THIS particular “.java” file in the SAME project, you would just put in an import statement in the top portion (just under the package declaration) of the file you wish to use it in. This is as easy as it gets with using code/resources from one file inside of another file.

The more complex workflow, is when you want to use a particular file’s code/resources inside of an entirely different program/project. What you would need to do here, is turn your original program (the one you want to use elsewhere) into a library/archive. This is done by turning your whole project into a “JAR” file (or, Java Archive). Once you have the entire project “compressed” into a JAR file, you can then bring that whole JAR file into your second project by adding the JAR file to your build path (classpath). Once that is done, THEN you can use the import method I describe above to import the file into your second project and use its code/resources.

Let me know if that helps clear up the workflow, if not, I’d be happy to record a video for you to draw out the situations :)

Reply

fraser graham November 21, 2012 at 8:52 am

Yeah that has explained a lot that trevor just wasn’t sure if you had to code the .java then import it to the main package.

Cheers

well nearly done with unit 4 on this java course im doing i have to write 2 programs if its ok with you if i get stuck is it alright if you can help me with it. The stuff you have written in your book is starting to sink in cheers really helped me.

Reply

fraser graham November 24, 2012 at 11:03 am

Hi Trevor i was wondering if you could write a program for me a drink program this is what i have to do but im completley stuck on writing it been trying to get it to work for about 2 weeks now i give up :(

I’ve to create a new project with the three classes Beverage, MilkBeverage, AlcoholicBeverage.

A) Class Beverage with attributes name, cost, and size, and a PrintAttributes() method that simply prints the value of members variables.

B) Create a subclass MilkBeverage that is a subclass (or inherits) of the beverage SuperClass. MilkBeverage should contain a String variable called “flavour”.

C) Create a subclass AlcoholicBeverage that is a subclass (or inherits) of the beverage SuperClass. AlcoholicBeverage should contain a String variable called “strength”.

D) Ensure that the user can select or enter the value for a beverage and then see the details printed to screen, for example they may select a small banana flavoured milk beverage, then the cost would be printed to the screen. For this you will need to create a beverage and initialise the variables, and call the PrintAttributes() method to display details about the Beverage object (ensure fields from the SuperClass and sub class are printed!).

ps if you can that would be great if not its cool i will just have to use what I have got. frazman2008@yahoo.co.uk

Reply

Trevor Page November 24, 2012 at 5:36 pm

I can certainly help you out with this, but I am extremely busy with work at this point and can’t really afford to do one-on-one work pro bono.

If you do need this kind of one on one help, I would be glad to provide it if you signed up for my javavideotutorials.net “advanced” level course. I would be able to hold a webinar with you and go over every one of the requirements for this program and explain exactly what the code would look like and exactly WHY I chose to do it that way.

The good news is that I have the Black Friday deal this weekend, so you can save 50% for life. Just click here and sign up for the advanced plan, you should see the coupon code automatically applied.

I look forward to working with you :)

Reply

fraser graham November 25, 2012 at 7:16 am

Hi trevor thanks i have until tuesday at 12pm to hand in what i have i will have a look at the advance course level javavideotutorials.net

cheers

Reply

Trevor Page November 27, 2012 at 9:19 pm

I trust all went well?

Reply

Leave a Comment

Powered by sweet Captcha

Previous post:

Next post: