PDA

View Full Version : java servlet form data error


opersai
07-01-2009, 07:50 AM
Hello everyone,

I'm new to java servlet programming, and I'm currently cluelessly stuck on a null exception when I'm trying to add any entries. I'm working on a downloaded template, and trying to edit it the way I want. So I have no idea at all where I should be looking. Can anyone suggest a place I might start at? Or suggest some common errors?

I'm not sure what other details to put, but please remind me.

servlet
07-01-2009, 01:57 PM
Start with this very simple servlet example (http://www.jsptube.com/servlet-tutorials/simple-servlet-example.html)

Jimbooo
07-01-2009, 02:35 PM
If the only problem is the null pointer exception, just put your code here including the exception. Then we can have a look at it.

opersai
07-01-2009, 08:02 PM
Thank you Jimboo:

Below is the servlet file that mainly responsible for the form:

/*
package iat;

import iat.db.Metadata;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.Element;
import sfu.util.JUtil;
import sfu.util.XmlApi;
/**
* <p>Title: MetadataServlet </p>
*
* <p>Description: This servlet provides Metadata APIs.</p>
*
* @author Ty Mey Eap
* @version 1.0
*/
public class MetadataServlet extends HttpServlet {
static Metadata metadata = null;
/**
* we have to implement HttpServlet interface, but we do not have to do anything
*/
public void init(ServletConfig config) throws ServletException {

}

/**
* creates and returns the instance of Metadata
*/
public static Metadata geMetadataClass(){
if (metadata == null){
metadata = new Metadata();
}
return metadata;
}

/**
* redirects to processAction
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
processAction(request, response);
}

/**
* redirects to processAction
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
processAction(request, response);
}

/**
* processAction processes all incoming requests.
*/
public static void processAction(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException {
response.setContentType("text/xml");
Metadata md = geMetadataClass();
Element output = null;
String msg = null;
boolean done = false;
String username = (String)request.getSession(true).getAttribute("username");
try{
String action = JSPUtil.getParam(request, "action", "");
System.out.println("Action Parameter:"+action);
if (action.equals("hit")){
String midStr = request.getParameter("mid");
String url = request.getParameter("url");
int mid = JUtil.parseInt(midStr, 0);
if (mid > 0 && !JUtil.isNull(url)){
takeHit(midStr, request);
response.sendRedirect(url);
done = true;
}else{
throw new Exception("Bad url!");
}
}else if (action.equals("add")){
output = md.getEmptyElement();
output.setAttribute("msgType", "saveNew");
}else if (action.equals("edit")){
output = md.getMetadata(JSPUtil.getInt(request, "mid", -1));
output.setAttribute("msgType", "saveUpdate");
}else if (action.equals("saveUpdate")){
int mid = JSPUtil.getInt(request, "mid", -1);
try{
output = md.update(mid, JSPUtil.getParam(request,"lang", null),
JSPUtil.getParam(request,"media_type", null),
JSPUtil.getParam(request,"url",null),JSPUtil.getParam(request,"authors",""),
JSPUtil.getParam(request,"publisher",""), JSPUtil.getParam(request,"format", null),
JSPUtil.getParam(request,"title", null),JSPUtil.getParam(request,"description",""));
output.setAttribute("msgType", "view");
}catch(Exception ex){
output = createMd(md, request, mid);
output.setAttribute("msgType", "saveUpdate");
XmlApi.createChildElement(output, ConfigServlet.NS, "error", "U R WRONG:"+ex.getMessage());
}
}else if (action.equals("saveNew")){
try{
output = md.add(JSPUtil.getParam(request,"lang", null),
JSPUtil.getParam(request,"media_type", null),
JSPUtil.getParam(request,"url",null),JSPUtil.getParam(request,"authors",""),
JSPUtil.getParam(request,"publisher",""), JSPUtil.getParam(request,"format", null),
JSPUtil.getParam(request,"title", null),JSPUtil.getParam(request,"description",""));
output.setAttribute("msgType", "view");
}catch(Exception ex){
output = createMd(md, request, -1);
XmlApi.createChildElement(output, ConfigServlet.NS, "error", "ADD WRONG:"+ex.getMessage());
output.setAttribute("msgType", "saveNew");
}
}else if (action.equals("search")){
output = md.search(JSPUtil.getParam(request,"keywords", null));
output.setAttribute("msgType", "search");
}else if (action.equals("delete")){
output = md.getMdElement();
output.setAttribute("msgType", "delete");
}else{
output = md.getMdElement();
output.setAttribute("msgType", "home");
}
}catch(Exception e){
e.printStackTrace();
msg = "Exception message:"+e.getMessage();
}
try{
if (!done){
if (output == null){
output = metadata.getMdElement();
}
if (msg != null){
XmlApi.createChildElement(output, ConfigServlet.NS, "error",msg);
}
response.getWriter().println(outputResult(output, username));
}
}catch(Exception ex){
throw new ServletException(ex.getMessage());
}
}
private static Element createMd(Metadata md, HttpServletRequest request,
int mid)throws Exception{
Element output = md.getMdElement();
String id = (mid > 0)?String.valueOf(mid):"";
md.addMetadata(output, id, JSPUtil.getParam(request,"lang", ""),
JSPUtil.getParam(request,"media_type", ""),
JSPUtil.getParam(request,"url",""),JSPUtil.getParam(request,"authors",""),
JSPUtil.getParam(request,"publisher",""), JSPUtil.getParam(request,"format", ""),
JSPUtil.getParam(request,"title", ""),JSPUtil.getParam(request,"description",""));
return output;
}
private static void takeHit(String mid, HttpServletRequest request)throws Exception{
HttpSession session = request.getSession(true);
ArrayList<String> accessedMedia = (ArrayList<String>)session.getAttribute("accessedMedia");
if (accessedMedia == null){
accessedMedia = new ArrayList<String>();
session.setAttribute("accessedMedia", accessedMedia);
}else{
if (accessedMedia.size() > 4){
accessedMedia.remove(0);
}
}
accessedMedia.add(mid);
metadata.updateHit(mid);
}

private static String outputResult(Element root, String userId)throws Exception{
return JSPUtil.outputWithXsltHeader(userId, "metadata.xsl", root);
}

/**
* we have to implement HttpServlet interface, but we do not have to do anything
*/
public void destroy() {

}
}

opersai
07-01-2009, 08:06 PM
This is the xsl file, responsible for the html end:


<form action="metadata.jsp">
<input type="hidden" name="action" value="{../@msgType}"/>
<input type="hidden" name="mid" value="{x:mid}" />
<tr><td>Title: </td>
<td><input type="text" name="title" size="50" value="{x:title}" />&nbsp;
</td>
</tr>
<tr><td>Description: </td>
<td><textarea rows="2" cols="50" name="description">
<xsl:value-of select="x:description" />
</textarea>&nbsp;
</td>
</tr>
<tr><td>Authors: </td>
<td><textarea rows="2" cols="50" name="authors">
<xsl:value-of select="x:authors" />
</textarea>&nbsp;
</td>
</tr>
<tr><td>Publisher: </td>
<td><input type="text" name="publisher" value="{x:publisher}" />&nbsp;
</td>
</tr>
<tr><td>URL: </td>
<td><input type="text" name="url" value="{x:url}" />&nbsp;
</td>
</tr>
<tr><td>Media Type:</td>
<td><input type="text" name="media_type" value="{x:media_type}" />&nbsp;
</td>
</tr>
<tr><td>Format: </td>
<td><input type="text" name="format" value="{x:format}" />&nbsp;
</td>
</tr>
<tr>
<td>Language </td>
<td>
<input type="text" name="lang" value="{x:lang}" />&nbsp;
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="button" value="Save Metadata" />&nbsp;
</td>
</tr>
</form>

servlet
07-02-2009, 03:47 PM
Any normal human can not find the line which is causing NPE in above code :)
Please post exception stack trace.

opersai
07-03-2009, 02:54 AM
Any normal human can not find the line which is causing NPE in above code :)
Please post exception stack trace.

I'm sorry, I'm new to programming. What is the "exception stack trace"? Do you mean the error message? I tested on my local server, and and page returned "ADD WRONG: null" message, and I found that message coming from this section of code below:

else if (action.equals("saveNew")){
try{
output = md.add(JSPUtil.getParam(request,"lang", null),
JSPUtil.getParam(request,"media_type", null),
JSPUtil.getParam(request,"url",null),JSPUtil.getParam(request,"authors",""),
JSPUtil.getParam(request,"publisher",""), JSPUtil.getParam(request,"format", null),
JSPUtil.getParam(request,"title", null),JSPUtil.getParam(request,"description",""));
output.setAttribute("msgType", "view");
}catch(Exception ex){
output = createMd(md, request, -1);
XmlApi.createChildElement(output, ConfigServlet.NS, "error", "ADD WRONG:"+ex.getMessage());
output.setAttribute("msgType", "saveNew");
}

Will this help? Thank you very much. =D

servlet
07-03-2009, 03:50 PM
Stack trace, that you can print with ex.printStackTrace(). it will print the trace on console.
post it here.

opersai
07-11-2009, 07:03 PM
Thanks Servlet,

Sorry I didn't respond. I was trying to find how to get the stack trace error message.

Load LOCAL DB infomation:jdbc:mysql://localhost:3306/ssheidae
java.sql.SQLException: Invalid authorization specification, message from server: "Access denied for user 'teap'@'localhost' (using password: YES)"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1977)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1900)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:2471)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:813)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1771)
at com.mysql.jdbc.Connection.<init>(Connection.java:440)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:400)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at sfu.db.DBConnector.createConnection(DBConnector.java:228)
at sfu.db.DBConnector.getConnection(DBConnector.java:184)
at sfu.db.DBConnector.getTables(DBConnector.java:291)
at sfu.db.DBConnector.createTablesIfNotExist(DBConnector.java:119)
at iat.ConfigServlet.checkTables(ConfigServlet.java:124)
at iat.ConfigServlet.init(ConfigServlet.java:74)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4149)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4458)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:987)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:909)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:495)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1206)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:314)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
java.lang.NullPointerException
at sfu.db.DBConnector.getTables(DBConnector.java:302)
at sfu.db.DBConnector.createTablesIfNotExist(DBConnector.java:119)
at iat.ConfigServlet.checkTables(ConfigServlet.java:124)
at iat.ConfigServlet.init(ConfigServlet.java:74)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4149)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4458)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:526)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:987)
at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:909)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:495)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1206)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:314)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:722)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:583)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)