Page 1 of 2 12 LastLast
Results 1 to 20 of 36
  1. #1
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default ARGH! Annoyed with my MySql driver.

    Howdy all, I was wondering what you thought about this. It compiles ok and everything. I am trying to output the contents of my database table studentInfo from the database Students. However, when I run it it throws a Null Pointer exception. and breaks on the underlined statement:

    Java Code:
    import java.sql.*;
    
    public class ListStudentInfo
    {
        public static void main(String[] args)
        {
            ResultSet students = getStudentResultSet();
            
            try
            {
                while (students.next())
                {
                    String msg; 
                    Student st = getStudent(students);
                    
                    msg = st.name + " ";
                    msg += st.email + " ";
                    System.out.println(msg);
                }
                
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            
        }
        
        private static ResultSet getStudentResultSet()
        {
            Connection con = getConnection();
            try
            {
                [U]Statement s = con.createStatement();[/U]
                String select = "select name, email from studentInfo order by name;";
                ResultSet rows;
                rows = s.executeQuery(select);
                return rows;
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        
        private static Connection getConnection()
        {
            Connection con = null;
            try
            {
                Class.forName("com.mysql.jdbc.Driver");
                String url = "jdbc:mysql://localhost/Students";
                String user = "root";
                String pw = "chicote";
                con = DriverManager.getConnection(url, user, pw);
            }
            
            catch (ClassNotFoundException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            return con;
        }
        
        private static Student getStudent(ResultSet students)
        {
            try
            {
                String name = students.getString("name");
                String email = students.getString("email");
                return new Student(name, email);
            }
            catch (SQLException e)
            {
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            return null;
        }
        
        private static class Student
        {
            public String name;
            public String email;
            public Student(String name, String email)
            {
                this.name = name;
                this.email = email;
            }
        }
    }
    Any thoughts?
    ~MSP430 Lover~

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Probably the con variable is null. Back track to see why it does not have a valid value. Add printlns as needed to see its value.
    I assume you can see the console and there are no error messages being printed there.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Joel View Post
    Any thoughts?
    Were there any other stack trace printouts before the null pointer exception stopped the entire show?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    You know what I didnt notice the stack trace output. In the Terminal window, it says com.mysql.jdbc.Driver and the stack trace says:

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at ListStudentInfo.getConnection(ListStudentInfo.java :54)
    at ListStudentInfo.getStudentResultSet(ListStudentInf o.java:32)
    at ListStudentInfo.main(ListStudentInfo.java:7)
    at __SHELL0.run(__SHELL0.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774 )
    java.lang.NullPointerException
    at ListStudentInfo.getStudentResultSet(ListStudentInf o.java:35)
    at ListStudentInfo.main(ListStudentInfo.java:7)
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:186)
    at ListStudentInfo.getConnection(ListStudentInfo.java :54)
    at ListStudentInfo.getStudentResultSet(ListStudentInf o.java:32)
    at ListStudentInfo.main(ListStudentInfo.java:7)
    at __SHELL1.run(__SHELL1.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at bluej.runtime.ExecServer$3.run(ExecServer.java:774 )
    java.lang.NullPointerException
    at ListStudentInfo.getStudentResultSet(ListStudentInf o.java:35)
    at ListStudentInfo.main(ListStudentInfo.java:7)

    But that can't be as I have already put c:\mysql-connector-java-5.1.17-bin.jar on the Classpath. Not sure...
    ~MSP430 Lover~

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Double check the spelling of the classname and look in the jar file to be sure the class file is there and it has the spelling you are using.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Joel View Post
    You know what I didnt notice the stack trace output. In the Terminal window, it says com.mysql.jdbc.Driver and the stack trace says:

    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    But that can't be as I have already put c:\mysql-connector-java-5.1.17-bin.jar on the Classpath. Not sure...
    You can be sure that the class isn't found by a class loader because the stack trace says so. Check your classpath value(s).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Hello Norm my old friend thanks again for your help. Upon inspecting the jar file, the path of the driver is ok EXCEPT the name of the actual file is Driver.class, not just Driver. I went into the code and added the .class extension to the String, but that still didn't solve the problem. Appears the spelling is correct as well as the path. Hm.
    ~MSP430 Lover~

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Joel View Post
    Hello Norm my old friend thanks again for your help. Upon inspecting the jar file, the path of the driver is ok EXCEPT the name of the actual file is Driver.class, not just Driver. I went into the code and added the .class extension to the String, but that still didn't solve the problem. Appears the spelling is correct as well as the path. Hm.
    The ".class" suffix is only there for URL class loaders. Try to change "127.0.0.1" for "localhost" in your db URL and see if that helps.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    I don' think the method uses a filename. Check the API doc to see for sure.

    Somewhere there must be a misspelling or misplacement
    Try running the java command from the command prompt with -cp option pointing to the jar file.
    Copy the whole console window contents here.

  10. #10
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    C:\>java -cp c:\mysql-connector-java-5.1.17-bin.jar
    Usage: java [-options] class [args...]
    (to execute a class)
    or java [-options] -jar jarfile [args...]
    (to execute a jar file)
    where options include:
    -d32 use a 32-bit data model if available
    -d64 use a 64-bit data model if available
    -client to select the "client" VM
    -server to select the "server" VM
    -hotspot is a synonym for the "client" VM [deprecated]
    The default VM is client.

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    A ; separated list of directories, JAR archives,
    and ZIP archives to search for class files.
    -D<name>=<value>
    set a system property
    -verbose[:class|gc|jni]
    enable verbose output
    -version print product version and exit
    -version:<value>
    require the specified version to run
    -showversion print product version and continue
    -jre-restrict-search | -no-jre-restrict-search
    include/exclude user private JREs in the version search
    -? -help print this help message
    -X print help on non-standard options
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
    enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
    disable assertions with specified granularity
    -esa | -enablesystemassertions
    enable system assertions
    -dsa | -disablesystemassertions
    disable system assertions
    -agentlib:<libname>[=<options>]
    load native agent library <libname>, e.g. -agentlib:hprof
    see also, -agentlib:jdwp=help and -agentlib:hprof=help
    -agentpath:<pathname>[=<options>]
    load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
    load Java programming language agent, see java.lang.instr

    -splash:<imagepath>
    show splash screen with specified image
    See Java SE Documentation at a Glance for more details.

    C:\>

    looks like all it did was run through the menu options. :(
    ~MSP430 Lover~

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Joel View Post
    C:\>java -cp c:\mysql-connector-java-5.1.17-bin.jar

    looks like all it did was run through the menu options. :(
    You didn´t give it anything to run; all you did was specify a classpath value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You left off the classname.
    java -cp THEJARFILESHERE.jar YOURCLASSNAMEHERE

  13. #13
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The ".class" suffix is only there for URL class loaders. Try to change "127.0.0.1" for "localhost" in your db URL and see if that helps.

    kind regards,

    Jos
    Let's see...you mean in the code? my URL already has localhost, but I changed it 127.0.0.1 just to see what would happen and same error.
    ~MSP430 Lover~

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    same error
    Which one? You have posted more than one error.

  15. #15
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    C:\>java -cp c:\mysql-connector-java-5.1.17-bin.jar ListStudentInfo
    Error: Could not find or load main class ListStudentInfo

    C:\>
    ~MSP430 Lover~

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Where is the ListStudentInfo.class file?

  17. #17
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    ListStudentInfo is located on the path here. I tried several different variations of the command:

    C:\>java -cp c:\mysql-connector-java-5.1.17-bin.jar C:\Documents and Settings\Jo
    el\Desktop\JDBCTester082411\ListStudentInfo
    Error: Could not find or load main class C:\Documents

    C:\>java -cp c:\mysql-connector-java-5.1.17-bin.jar "C:\Documents and Settings\J
    oel\Desktop\JDBCTester082411\ListStudentInfo"
    Error: Could not find or load main class C:\Documents and Settings\Joel\Desktop\
    JDBCTester082411\ListStudentInfo

    C:\>java -cp c:\mysql-connector-java-5.1.17-bin.jar "C:\Documents and Settings\J
    oel\Desktop\JDBCTester082411\ListStudentInfo.class "
    Error: Could not find or load main class C:\Documents and Settings\Joel\Desktop\
    JDBCTester082411\ListStudentInfo.class

    C:\>java -cp c:\mysql-connector-java-5.1.17-bin.jar "C:\Documents and Settings\J
    oel\Desktop\JDBCTester082411\ListStudentInfo.java"
    Error: Could not find or load main class C:\Documents and Settings\Joel\Desktop\
    JDBCTester082411\ListStudentInfo.java

    C:\>
    ~MSP430 Lover~

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    For simpler testing, create a folder to hold all the parts to this and move/copy all the files into that folder.
    then open a command prompt, go to that folder and enter the following two commands:
    dir
    java -cp mysql-connector-java-5.1.17-bin.jar;. ListStudentInfo

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Now you're talking about quite another error: the JVM can't find your ListStudentInfo class; it isn't whining about not being able to open a Connection for you. Fix your classpath value. b.t.w. don't suffix your class names with ".class"; it is not needed and not wanted by the class loaders.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.

    C:\Documents and Settings\Joel>cd\

    C:\>cd\Normssuggestion

    C:\NormsSuggestion>dir
    Volume in drive C has no label.
    Volume Serial Number is E47E-AFDF

    Directory of C:\NormsSuggestion

    08/29/2011 01:26 PM <DIR> .
    08/29/2011 01:26 PM <DIR> ..
    08/29/2011 12:30 PM 2,897 ListStudentInfo.class
    08/29/2011 12:30 PM 2,583 ListStudentInfo.java
    2 File(s) 5,480 bytes
    2 Dir(s) 3,530,829,824 bytes free

    C:\NormsSuggestion>java -cp mysql-connector-java-5.1.17-bin.jar;. ListStudentInf
    o
    com.mysql.jdbc.Driver
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at ListStudentInfo.getConnection(ListStudentInfo.java :54)
    at ListStudentInfo.getStudentResultSet(ListStudentInf o.java:32)
    at ListStudentInfo.main(ListStudentInfo.java:7)
    Exception in thread "main" java.lang.NullPointerException
    at ListStudentInfo.getStudentResultSet(ListStudentInf o.java:35)
    at ListStudentInfo.main(ListStudentInfo.java:7)

    C:\NormsSuggestion>
    ~MSP430 Lover~

Page 1 of 2 12 LastLast

Similar Threads

  1. ClassNotFoundException: com.mysql.jdbc.Driver
    By tBKwtWS in forum New To Java
    Replies: 9
    Last Post: 06-15-2011, 08:01 PM
  2. com.mysql.jdbc.Driver
    By uthpalaw in forum Eclipse
    Replies: 2
    Last Post: 10-14-2010, 06:09 AM
  3. Problems with MySQL Driver
    By islan in forum JDBC
    Replies: 7
    Last Post: 08-06-2009, 05:47 PM
  4. can't register a MySQL driver
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-04-2008, 12:13 AM
  5. mysql driver problem
    By mokingsu in forum JDBC
    Replies: 4
    Last Post: 01-17-2008, 06:27 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
  •