English 中文(简体)
java.lang.NullPointerException在试图利用数据库的数据时
原标题:java.lang.NullPointerException while trying to fetch data from database

我想将数据库中储存的标签列入以下小页:

<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>
                <% iii %>            
    </div>

Follwoing is GetTagsFromDatabase file:

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() {

iii

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++;
        iii
    iiicatch(Exception exc) {
        exc.printStackTrace();
    iii

    return tags;
iii  

iii

但是,在一页的运行中,有以下例外:

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)
........

I tried both the lookup urls java:comp/env/jdbc/photog and jdbc/photog. Though there are 48 items in the table,what could be the reason i get these exceptions ?

最佳回答

www.un.org/spanish/ga/president 你们需要首先这样做。 更好的是使用<代码>ArrayList,储存可变的标签长度。 而不是

String tags[] = null;

......

ArrayList<String> tags = new ArrayList<String>();

and later:

tags.add(set.getString("NameOfTheTag"));

然后,你们要么需要改变你的签名方法,要么改变你的签名方法。

public String[] getTags() {

页: 1

public ArrayList<String> getTags() {

or convert your ArrayList 页: 1 an array again.

如果你知道有多少对口条目,那么你就只能改变这一行文:

String tags[] = new String[numberOfTagElements];

EDIT

In order 页: 1 use an array you have 页: 1

  1. declare it
  2. initialize it
  3. add or set elements in that array.

例:

String[] tags;  //declaration
tags = new String[10]; //initialize array: max 10 elements can be used.
tags[0] = "hello";  //set element 1 of your array

You missed step 2. No memory will be allocated if you init your array with null. You have 页: 1 tell the compiler how much memory you need. If you want 页: 1 use 10 elements for instance the array has 页: 1 reserve that fist before you can use it.

问题回答

我的发言是一般性的,但有助于你。

Whenever you encounter an Object,Why don t you check for null.

if(object==null){
//dosomething
}else{
//dosomethingelse()
}

我要说的是,将defensive program纳入你每次都帮助你的法典。

照顾到@juergen d的话。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签