Results 1 to 20 of 36
- 08-29-2011, 06:39 PM #1
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:
Any thoughts?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; } } }~MSP430 Lover~
- 08-29-2011, 07:03 PM #2
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.
- 08-29-2011, 07:04 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-29-2011, 08:06 PM #4
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~
- 08-29-2011, 08:10 PM #5
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.
- 08-29-2011, 08:11 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-29-2011, 08:16 PM #7
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~
- 08-29-2011, 08:24 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-29-2011, 08:26 PM #9
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.
- 08-29-2011, 09:15 PM #10
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~
- 08-29-2011, 09:19 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 08-29-2011, 09:19 PM #12
You left off the classname.
java -cp THEJARFILESHERE.jar YOURCLASSNAMEHERE
- 08-29-2011, 09:26 PM #13
- 08-29-2011, 09:27 PM #14
Which one? You have posted more than one error.same error
- 08-29-2011, 09:32 PM #15
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~
- 08-29-2011, 09:37 PM #16
Where is the ListStudentInfo.class file?
- 08-29-2011, 09:47 PM #17
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~
- 08-29-2011, 09:51 PM #18
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
- 08-29-2011, 10:00 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-29-2011, 10:22 PM #20
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~
Similar Threads
-
ClassNotFoundException: com.mysql.jdbc.Driver
By tBKwtWS in forum New To JavaReplies: 9Last Post: 06-15-2011, 07:01 PM -
com.mysql.jdbc.Driver
By uthpalaw in forum EclipseReplies: 2Last Post: 10-14-2010, 05:09 AM -
Problems with MySQL Driver
By islan in forum JDBCReplies: 7Last Post: 08-06-2009, 04:47 PM -
can't register a MySQL driver
By prfalco in forum New To JavaReplies: 4Last Post: 02-03-2008, 11:13 PM -
mysql driver problem
By mokingsu in forum JDBCReplies: 4Last Post: 01-17-2008, 05:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks