• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using MySql with a Java Web App

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm currently building a database app where the web app sits on Applications Server 9.1 and after a user fills in a form it the information is sent to a MySql database.

When I press submit I get a java.lang.NullPointerException. I have placed the driver in the jre/lib/ext folder and the code for connecting to the database is as follows:

try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pd206",
"root", "password");
} catch (ClassNotFoundException e) {
System.out.println("Class still not found");
} catch (SQLException e) {
System.out.println("SQL Exception in init");
}

The system is currently working on a local machine and is not expected to go onto a full network until later in the year.

Can somebody please help?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which line does the exception occur? Looking at this code, I wouldn't think that any of it can throw a NPE.
 
dave green
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it doesnt come up with a line on the code. Its when I press the submit button on the web page of the app it goes to the error message. The first line on the stack trace is:

Servlet.service() for servlet initialEnq threw exception java.lang.NullPointerException at initalEnq.submitData(initialEnq.java:63)
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what's happening on line 63?
 
dave green
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 63 is the last line of this code section which shows everything upto that point. it is basically where it tries to create the statement. I keep thinking it is something to do with the mySql driver because I am trying to run it on the machine it will work from normally, however it works fine on my developer machince when launched from NetBeans

public class initialEnq extends HttpServlet {

Connection con;

public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pd206",
"root", "password");
} catch (ClassNotFoundException e) {
System.out.println("Class still not found");
} catch (SQLException e) {
System.out.println("SQL Exception in init");
}
}

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String a = request.getParameter("number");
String b = request.getParameter("rank");
String c = request.getParameter("surname");
String d = request.getParameter("forenames");
String e = request.getParameter("trade");
String f = request.getParameter("unit");
String g = request.getParameter("unitAddress");
String h = request.getParameter("workContactNo");
String i = request.getParameter("mobileContactNo");
String j = request.getParameter("actionTaken");
String k = request.getParameter("actionRequired");
String l = request.getParameter("submitedBy");
String m = request.getParameter("dateCompleted");

submitData(a, b, c, d, e, f, g, h, i, j, k, l, m);

RequestDispatcher view = request.getRequestDispatcher("submited.jsp");
view.forward(request, response);
}

public void submitData(String a, String b, String c, String d, String e, String f,
String g, String h, String i, String j, String k, String l, String m) {

try {
Statement stmt = con.createStatement();
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by dave green:
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/pd206",
"root", "password");
} catch (ClassNotFoundException e) {
System.out.println("Class still not found");
} catch (SQLException e) {
System.out.println("SQL Exception in init");
}
}


This method is a red flag to me. If an error occurs loading the driver or getting the connection, the method quietly logs it and then goes on. If the connection cannot be retrieved, I would throw a runtime exception so the servlet doesn't become available to users. This will tell you more specifically what the error is without failing with a null pointer.

Also, I recommend including the exception in your error message as it will give you a bigger clue as to what is going wrong. Feel free to post the error message here for more help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic