次のjspページで、データベースに保存されているタグをリストしたいと思いました:
<div id="site_content"> <%-- Put the tags under this tag --%>
<%!
GetTagsFromDatabase gtfd = new GetTagsFromDatabase();
String tags[] = gtfd.getTags();
%>
<%
for(int i = 0 ; i <= tags.length ; ) {
%>
<table>
<tr>
<td> <%= tags[i++] %> </td>
<td> <%= tags[i++] %> </td>
<td> <%= tags[i++] %> </td>
<td> <%= tags[i++] %> </td>
</tr>
</table>
<% } %>
</div>
以下はGetTagsFromDatabase
ファイルです:
package NonServletFiles;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import javax.naming.*;
public class GetTagsFromDatabase {
public GetTagsFromDatabase() {
}
public String[] getTags() {
String tags[] = null;
try {
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/photog");
Connection connection = ds.getConnection();
String sqlQuery = "select NAMEOFTHETAG from tagcollection";
PreparedStatement statement = connection.prepareStatement(sqlQuery);
ResultSet set = statement.executeQuery();
int i = 0;
while(set.next()) {
tags[i] = set.getString("NameOfTheTag"); // <<--- line 33
System.out.println(tags[i]);
i++;
}
}catch(Exception exc) {
exc.printStackTrace();
}
return tags;
}
}
しかし、jsp ページを実行すると、次の例外が発生します。
SEVERE: java.lang.NullPointerException
at NonServletFiles.GetTagsFromDatabase.getTags(GetTagsFromDatabase.java:33)
at org.apache.jsp.ListTags_jsp.<init>(ListTags_jsp.java:13)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:372)
at java.lang.Class.newInstance(Class.java:325)
at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:203)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
SEVERE: at NonServletFiles.GetTagsFromDatabase.getTags(GetTagsFromDatabase.java:33)
SEVERE: at org.apache.jsp.ListTags_jsp.<init>(ListTags_jsp.java:13)
SEVERE: at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
SEVERE: at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
SEVERE: at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
SEVERE: at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
SEVERE: at java.lang.Class.newInstance0(Class.java:372)
........
java:comp/env/jdbc/photog
ルックアップ URLとの両方を試しましたjdbc/photog
。テーブルには 48 個のアイテムがありますが、これらの例外が発生する理由は何ですか?