Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception in thread "main" javax.naming.NoInitialContextException: and a Null Pointer Exception

 
Lisa Austin
Ranch Hand
Posts: 119
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm receiving the following error in Eclipse. My instructor was supposed to help me with this but I haven't heard back from him. I'm wondering if anyone here would be familiar??

Here is the error.
Exception in thread "main" javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
java.lang.NullPointerException
at lab.ConnectionPool.getConnection(ConnectionPool.java:33)
at lab.LocalMySQLCP.pooling(LocalMySQLCP.java:97)
at lab.LocalMySQLCP.openDB(LocalMySQLCP.java:106)
at lab.LocalMySQLCP.<init>(LocalMySQLCP.java:22)
at lab.LocalMySQLCP.main(LocalMySQLCP.java:53)

I'll post all the code below but I have a question about line 33 which is specified in the error.

Line 33 in the ConnectionPool class is and the dataSource variable value is

The above code is taken from the book I'm working in It works for others in my class but my Eclipse is setup differently. It's setup in the same way I was told to set it up for work.

Is by any chance this line java:/comp/env/jdbc has something to do with my java folder location? Currently my java is in C:\Development\Java\jdk1.8.0_71_64 . I have it setup to match my work computer setup. In the Environment variables of my computer I have JAVA_HOME set to C:\Development\Java\jdk1.8.0_71_64. I can't help but think maybe that's why I'm receiving this error? Maybe I need to change the value of the dataSource variable? I know that JDBC is Java Database Connectivity (JDBC) so I'm probably wrong regarding this but I can't think of any other reason why it's not working for me and it does others.

Thank You for your help.

The ConnectionPool class which contains line 33 referenced above. Following this is the class ( with the remaining lines being referenced) . Last is the context.xml file that I have and that file is in the META-INF folder.

More details.
I'm using MySQL
My database schemas include a database name Country , sys, and testdata but my instructor says that I don't need to specify these database in the context.xml file.





My LocalMySQLCP class




Context.xml file

 
Alex Lieb
Ranch Hand
Posts: 58
3
Java Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the issue you're running into revolves around the fact that your IntialContext object doesn't actually know where your context.xml file is. You declared it as a "new InitialContext()" on line 17 of your connection pool class, then on the next line told it to retrieve a DataSource, but it can't because it doesn't know where to find any datasources.

If your project is a Java Web project, the part where you tell your project where to find the context.xml file is usually handled for you, so I'm actually not sure how to tell it where to find one of those in a standard project, but I'm pretty sure the issue lies in the fact that your InitialContext object, while not null, does not actually know where it should be trying to find your context.xml file.
 
Alex Lieb
Ranch Hand
Posts: 58
3
Java Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd take a look at this and see if any of it is relevant; particularly this part:

https://tomcat.apache.org/tomcat-7.0-doc/jndi-resources-howto.html

The InitialContext is configured as a web application is initially deployed, and is made available to web application components (for read-only access). All configured entries and resources are placed in the java:comp/env portion of the JNDI namespace, so a typical access to a resource - in this case, to a JDBC DataSource - would look something like this:

// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");

// Look up our data source
DataSource ds = (DataSource)
envCtx.lookup("jdbc/EmployeeDB");

// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
... use this connection to access the database ...
conn.close();

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic