Page 1 of 3 123 LastLast
Results 1 to 16 of 34

Thread: Java fun!

  1. #1
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts

    Java fun!

    I'm getting this error:

    Code:
    Exception in thread "main" java.lang.NullPointerException
        at DBConn.<init>(DBConn.java:36)
        at Test.main(Test.java:23)
    Java Result: 1
    Line 36 in DBConn is the following

    stmt = con.createStatement();

    Which just creates a statement from con and assigns it to statement (created from the ADT "Statement")

    On line 23 of main I have the following:

    DBConn connection = new DBConn(db, dbname, username, pwd);

    As you do. No idea what's wrong though, any ideas?

  2. #2
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Not the way i'd do it.
    To err is human. To really foul things up ... you need a computer.

  3. #3
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    Post all your code mate!
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  4. #4
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    The code is huge atm, there's much going on in this class. I'll post the constructor and everything in main as it must be down to that:

    Code:
        public DBConn(String remoteLocation, String database, String userName, String password) {
            this.remoteLocation = remoteLocation;
            this.database = database;
            this.userName = userName;
            this.password = password;
            try {
                stmt = con.createStatement();
            }
            catch (SQLException ex) {
                System.out.println("You ****");
            }
        }
    and the instance variables

    Code:
        private String remoteLocation;
        private String database;
        private String userName;
        private String password;
        private Connection con;
        private Statement stmt;
    and main

    Code:
    public class Test {
    
        public static void main(String[] args) {
    
            String db = "****";
            String dbname = "****";
            String username = "****";
            String pwd = "****";
            
            DBConn connection = new DBConn(db, dbname, username, pwd);
            
            connection.loadDriver();
            connection.initialise();
            connection.createDB();
            
            System.out.print("Table created!");
            
            connection.shutdown();
            
            System.out.print("Connection shutdown");
            
        }
        
    }

  5. #5
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    I forgot to mention, i'm connecting to a remote database :/

  6. #6
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    What's on line 36 in file DBConn.java? That's where your null object reference is...

    Also, I'd seriously recommend using an IDE like Eclipse (which is free) and learning to use the debugger. Working on Java without a debugger is like, I dunno, trying to paint a room with a blindfold on. You'd get there in the end, but it would take a lot longer than it needs to.
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  7. #7
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Line 36 was stmt = con.createStatement(); in the constructor. I think it may be down to the drivers. As far as i know Connector/J is native to JDK versions from 1.2 which is what i'm using. I'm clueless, i even tried examples off the net, direct copy and paste and still had no luck! I'm just going to have to go through the documentation again and see what i'm doing wrong

  8. #8
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    So you are sure your connection string is correct?
    To err is human. To really foul things up ... you need a computer.

  9. #9
    Goat Boy
    Join Date
    Jul 2003
    Location
    Alexandra Park, London
    Posts
    2,428
    Thanks
    0
    Thanked
    0 times in 0 posts
    Where do you define con? Looks like that is where the NPE is...
    "All our beliefs are being challenged now, and rightfully so, they're stupid." - Bill Hicks

  10. #10
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    con = DriverManager.getConnection("jdbc:mysql:///" + remoteLocation +
    "/" + database + "?user=" + userName + "&password=" + password);

    That's it, in the initialise() method to create a connection. I've tried entering in everything manually and made sure everything is perfect but still no luck The connection string is perfect everytime afaik

  11. #11
    DsW
    DsW is offline
    Senior Member
    Join Date
    Aug 2003
    Location
    Glasgow
    Posts
    292
    Thanks
    0
    Thanked
    0 times in 0 posts
    The Connection object will be NULL in the constructor since initialise() is called after the DBConn constructor.

    You need to create the Connection before you can use it!

  12. #12
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Yeh i know, hence why it was moved to the initialise method beforehand. Still doesn't work though

  13. #13
    DsW
    DsW is offline
    Senior Member
    Join Date
    Aug 2003
    Location
    Glasgow
    Posts
    292
    Thanks
    0
    Thanked
    0 times in 0 posts
    Sorry - I didn't realise you had moved it.

    It's a bit difficult to debug when we can only see small snippets of code and with you changing it behind the scenes!

    As DaBeenster says - your best bet is debugging using something like Eclipse. That'll let you see how all the variables are assigned when the NPE occurs. In all likelyhood it'll be something like your driver not being on the classpath or something.

    Otherwise we are just guessing how your code is writen.

  14. #14
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Ok well i debugged it and both exceptions were there which were that it couldn't load the driver and it couldn't connect to the database (probably due to the wrong/missing driver)

    I'm using the driver name "com.mysql.jdbc.Driver" which was what it stated in the mySQL documentation. That driver is native to JDK versions 1.2 and up so it should work fine.

    EDIT: http://dev.mysql.com/doc/connector/j...classname.html

    That's the link
    Last edited by Kezzer; 09-03-2005 at 09:55 AM.

  15. #15
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Code:
    import java.sql.Connection;
    import java.sql.DriverManager; 
    import java.sql.SQLException;
    
    public class DBConn { 
    
        private String remoteLocation;
        private String database;
        private String userName;
        private String password;
        private Connection con = null;
        
        /** Constructor
         *  @param remoteLocation The URL of the Website
         *  @param database The name of the database to connect to
         *  @param userName The Username
         *  @param password The password
         */
        public DBConn(String remoteLocation, String database, String userName, String password) {
            this.remoteLocation = remoteLocation;
            this.database = database;
            this.userName = userName;
            this.password = password;
        }
        
        /** intialise the connection to the remote database */
        public void initialise() {
            try {   
              con = DriverManager.getConnection("jdbc:mysql://" + remoteLocation + 
                 "/" + database + "?user=" + userName + "&password=" + password);
            }
            catch (SQLException ex) {
                System.out.println("Cannot connect to database");
            }
        }
        
        /** use ResourceBundle for this, add driver to DBConn.properties file
         */
        public void loadDriver() {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            }
            catch (Exception ex) {
                System.out.println("Cannot load driver");
            }
        }
    }
    And main

    Code:
    public class Test {
    
        public static void main(String[] args) {
    
            String db = "test";
            String dbname = "test";
            String username = "test";
            String pwd = "test";
            
            DBConn connection = new DBConn(db, dbname, username, pwd);
            
            connection.loadDriver();
            connection.initialise();
            
            System.out.print("Table created!");
    
            System.out.print("Connection shutdown");
            
        }
        
    }
    And i get the following output :

    Cannot load driver
    Cannot connect to database
    Table created!Connection shutdown

    Don't worry about what it says, you can just see what it's doing. But basically it's obviously having big problems as it can't even load the driver That driver is native to JDK versions 1.2 and up apparently but if it's not being loaded then obviously not Any ideas?

  16. #16
    DsW
    DsW is offline
    Senior Member
    Join Date
    Aug 2003
    Location
    Glasgow
    Posts
    292
    Thanks
    0
    Thanked
    0 times in 0 posts
    Quote Originally Posted by KeZZeR
    That driver is native to JDK versions 1.2 and up apparently but if it's not being loaded then obviously not Any ideas?
    The class "com.mysql.jdbc.Driver" does not come with the standard JDK (not sure where you got that idea from?).

    Try typing "com.mysql.jdbc." into your IDE then trying to use auto complete (Ctrl-Space) and you'll see that it can't resolve the package.

    Basically you need to put the MySQL driver jar (Connector/J) on your classpath so your code can resolve this class.

Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Best book to learn JAVA 2?
    By Taz in forum Software
    Replies: 16
    Last Post: 27-08-2005, 08:54 PM
  2. Replies: 12
    Last Post: 14-03-2005, 09:49 PM
  3. JAVA BYTEVER and FEMAD (virus type thing)
    By Kumagoro in forum Software
    Replies: 5
    Last Post: 28-12-2004, 04:56 PM
  4. Potential HUGE source of free Java games / apps
    By PriestJPN in forum Smartphones and Tablets
    Replies: 4
    Last Post: 03-11-2004, 01:59 AM
  5. Java Problems
    By Applecrusher in forum Software
    Replies: 4
    Last Post: 27-04-2004, 01:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •