This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Static methods and nullpointerexceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static methods and nullpointerexceptions" Watch "Static methods and nullpointerexceptions" New topic
Author

Static methods and nullpointerexceptions

Ravaa Bal
Ranch Hand

Joined: Apr 15, 2009
Posts: 31
Hi,



I'm getting the error "non-static method connect cannot be referenced from a static context" in the main method, so I tried something like this:



This fixed that error, but then when ran I got this:

Exception in thread "main" java.lang.NullPointerException
at Connection.connecter(Connection.java:84)
at Connection.<init>(Connection.java:35)
at Connection.main(Connection.java:22)

Any insight on what I could be doing incorrectly would be welcome, thank you.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
Connection.connect is somewhere you have somehow sent a null reference to something.
That has got there via Connection<init> which means the Connector constructor.
The line numbers don't seem to match the code you have supplied.

Most likely cause:
You have managed to pass something null to the Connection constructor. Make sure every argument you pass actually has an object attached to it.
Ravaa Bal
Ranch Hand

Joined: Apr 15, 2009
Posts: 31
Yatta!

Thanks Campbell Ritchie, I did something silly and returned a null after a try catch block without having a return *inside* the try catch block.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
You're welcome

Everybody does that sort of thing occasionally.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static methods and nullpointerexceptions