# CMS mit JSP und MySQL, irgendwann ein Nullpointer



## CJb3LL (13. Jan 2007)

Hallo zusammen!

Ein Freund und ich entwerfen zur Zeit unser eigenes kleines CMS, was uns hauptsächlich als persönliche Übung dienen soll. An einer Stelle hängen wir aber fest. Wir haben eine JSP section_form.jsp, welche über ein Formular die Eingabe eines Titels, eines Contents und einer Rubrik (später wichtig zum generieren der Navigation) einliest. Hier zunächst der Code dieser JSP:


```
<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isThreadSafe="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Neue Section hinzuf&gen</title>
</head>

<jsp:useBean id="connection" class="dragicms.DBConnection" scope="session"/>

<body>

<form method="post" action="upload.jsp">


Title: <input type="text" id="title" name="title" maxlength="255" style="width:300px;" /></p>


SuperSec: 

	<select id="superSec" name="superSec">
		<option value="0">neue Section</option>
		<%
		java.sql.ResultSet result =  null;
		result = connection.getAllSections();
		
		int id;
		String title;
		
		while (result.next())
		{
			id = result.getInt("id");
			title = result.getString("title");
		%>
			<option value="<%=id%>"><%=title%></option>
		<%
		}
		%>
	</select>
</p>


Content:
<textarea id="content" name="content" style="width:300px; height:150px;"></textarea></p>


<input type="submit" id="submit" name="submit" value="submit" /> <input type="reset" id="reset" name="reset" value="reset" /></p>
</form>

</body>
</html>
```

Die Werte werden an upload.jsp gesendet, die wie folgt aussieht:


```
<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isThreadSafe="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page import="java.sql.Timestamp"%>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>

<jsp:useBean id="section" class="dragicms.Section" scope="page" />
<jsp:useBean id="connection" class="dragicms.DBConnection" scope="session" />

<jsp:setProperty name="section" property="*" />

<body>

<%

if (section.getSuperSec() != 0)
{
	java.sql.ResultSet superSec = connection.getSectionById(section.getSuperSec());
	
	if (superSec.next())
	{
		int superSecDepth = superSec.getInt("depth");
		section.setDepth(superSecDepth+1);
	}
}

	
	connection.insertInDB(section);
	
%>

<%
		java.sql.ResultSet result =  null;
		
		
		int id;
		String title;
		Timestamp time;
		String content;
		result = connection.getAllSections();
		
		result.last();
		id = result.getInt("id");
		result = connection.getSectionById(id);
		result.last();
		time = result.getTimestamp("timestamp"); 
		title = result.getString("title");
		content = result.getString("content");
		%>
		
Die Seite wurde hinzugef> am <%=time %>

Achtung! Hier muss noch abgefragt werden, ob dies wirklich erfolgreich war!

Die Seite hat die ID <%=id %> erhalten!


Folgende Angaben wurden gemacht:

Titel: <%=title %>

Content: <%=content %>

</body>
</html>
```

Und hier erstmal noch die zugehörigen Beans:

DBConnection.java:

```
package dragicms;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBConnection
{
	private static String table 				= "section";
	private static Connection theConnection 	= null;
	
	private static String dbname				= "dragicms";
	private static String user					= "zensiert";
	private static String password				= "zensiert";
	
	
	public synchronized void insertInDB (Section section)
	{
		//int id;
		String content;
		//String filepath;
		String title;
		int depth;
		int superSec;
		
		//id 			= section.getId();
		content		= section.getContent();
		//filepath	= section.getFilepath();
		title		= section.getTitle();
		depth		= section.getDepth();
		superSec	= section.getSuperSec();
		
		
		try
		{
			connect();
			
			Statement statement = theConnection.createStatement();
			
			String insertFields = "content, title, super_sec, depth";
			String insertValues = "'" + content + "', '" + title + "', '" + superSec + "', '" + depth + "'";
			
			System.out.println("INSERT INTO " +  table + " (" + insertFields + ") VALUES (" + insertValues + ")");
			
			statement.executeUpdate("INSERT INTO " +  table + " (" + insertFields + ") VALUES (" + insertValues + ")");
			
		}
		catch (SQLException e)
		{
			System.out.println("Ein Fehler ist bei DBConnection.insertInDB() aufgetreten beim speichern aufgetreten: " + e.getMessage());
		}
		
	}
	
	public synchronized ResultSet getSectionsByDepth (int depth)
	{		
		Statement statement = null;
		ResultSet result = null;
		
		try
		{
			connect();
			
			statement = theConnection.createStatement();
			
			result = statement.executeQuery("SELECT * FROM " + table + " WHERE depth=" + depth);
		
			return result;
		}
		catch (SQLException e)
		{
			System.out.println("Ein Fehler ist aufgetreten bei DBConnection.getSectionsByDepth(): " + e.getMessage());
			return result;
		}
	}
	
	public synchronized ResultSet getSectionById (int id)
	{
		Statement statement = null;
		ResultSet result	= null;
		
		try
		{
			connect();
			
			statement 	= theConnection.createStatement();
			result		= statement.executeQuery("Select * FROM " + table + " WHERE id="+id);
		
			return result;
		}
		catch (SQLException e)
		{
			System.out.println("Ein Fehler ist aufgetreten bei DBConnection.getSectionById(): " + e.getMessage());
			return result;
		}
	}
	
	public synchronized ResultSet getAllSections ()
	{
		Statement statement = null;
		ResultSet result	= null;
		
		try
		{
			connect();

			statement 	= theConnection.createStatement();
			result		= statement.executeQuery("Select * FROM " + table + " ORDER BY `id` ASC ");
		
			return result;
		}
		catch (SQLException e)
		{
			System.out.println("Ein Fehler ist aufgetreten bei DBConnection.getAllSections(): " + e.getMessage());
			return result;
		}
	}
	
	public synchronized void connect ()
	{
		if (theConnection == null)
		{
			try 
			{				
				Class.forName("org.gjt.mm.mysql.Driver").newInstance();
				theConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/"+dbname, user , password);
			}
			catch (Exception e)
			{
				System.out.println("Ein Fehler ist beim Verbinden aufgetreten DBConnection.connect(): " + e.getMessage());
			}
		}
	}
	
	public void closeConnection ()
	{
		if (theConnection != null)
		{
			try
			{
				theConnection.close();
			}
			catch (SQLException e)
			{
				System.out.println("Ein Fehler ist beim Schließen aufgetreten DBConnection.closeConnection(): " + e.getMessage());
			}
		}
	}
}
```

Section.java:

```
package dragicms;


public class Section
{
	private int 	id;
	private String content;
	private String title;
	//private String filepath;
	private int 	superSec; 
	private int 	depth;
	private int timestamp;
	
	
	public int getDepth()
	{
		return depth;
	}
	public void setDepth(int depth)
	{
		this.depth = depth;
	}
	public String getContent()
	{
		return content;
	}
	public void setContent(String content)
	{
		this.content = content;
	}
	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id = id;
	}
	public int getSuperSec()
	{
		return superSec;
	}
	public void setSuperSec(int superSec)
	{
		this.superSec = superSec;
	}
	public String getTitle()
	{
		return title;
	}
	public void setTitle(String title)
	{
		this.title = title;
	}
	public int getTimestamp() {
		return timestamp;
	}
	public void setTimestamp(int timestamp) {
		this.timestamp = timestamp;
	}
	
}
```

Das ganze ist an eine MySQL-Datenbank angebunden.
Unser Problem ist nun folgendes: Wenn wir das soweit testen, dann läuft alles soweit vernünftig, aber sobald wir das als .war online setzen und man parallel mit mehreren Benutzer darauf zugreift, dann passiert es, dass man irgendwann eine NullPointerException erhält. Wir vermuten, dass dies was mit der Nebenläufigkeit zu tun hat. Wir finden den Fehler aber einfach nicht.
Hat vielleicht jemand von Euch eine Idee, woran es liegen könnte?


----------



## MatthiasKnorr (13. Jan 2007)

Poste mal den StackTrace der Exception, denn da steht ne ganze Menge wichtiger Kram drin.


----------



## Guest (13. Jan 2007)

Die Fehlermeldung:


```
HTTP Status 500 - 

--------------------------------------------------------------------------------

type Exception report

message 

description The server encountered an internal error () that prevented it from fulfilling this request.

exception 

org.apache.jasper.JasperException
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:207)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
	at java.lang.Thread.run(Thread.java:534)


root cause 

java.lang.NullPointerException
	at org.apache.jsp.section_form_jsp._jspService(section_form_jsp.java:82)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:92)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:159)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:240)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:187)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:200)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:146)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:209)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
	at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
	at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
	at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
	at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
	at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
	at java.lang.Thread.run(Thread.java:534)



--------------------------------------------------------------------------------

Apache Tomcat/4.1
```

Hier kann man die Fehlermeldung nochmal nachlesen:
christianbell.de:9080/test/section_form.jsp


----------



## CJb3LL (14. Jan 2007)

P.S.: Der obige Eintrag stammt von mir. Ich hatte leider nicht darauf geachtet, ob ich eingeloggt bin.


----------



## SlaterB (14. Jan 2007)

tja, die Fehlermeldung sagt ja nun wenig aus 
vielleicht wird die durch die Instanzen weitergereicht und nur die Exception-Message, nicht aber der Stacktrace kopiert,

versuche es mal mit

try {


} catch (Throwable t) {
   // logging von t und StackTrace
   throw t;
}

in allen beteiligten JPSs am Anfang und Ende und/ oder gezielt um die Java-Code-Teile herum,
wenn Throwable Probleme macht, dann eben nur NullPointerException


----------

bzw. 
section_form_jsp.java:82
ist ja angegeben,

suche mal die Datei section_form_jsp.java,
im Tomcat müsste die als Quelltext vorliegen, was ist dan in Zeile 82 los?


----------



## CJb3LL (15. Jan 2007)

Vielen Dank für die Hinweise! Ich werde es alles mal durchprobieren und dann werde ich Dir/Euch Bericht erstatten.


----------

