weblogic的Servlet创建之后,报空指针异常

各位前辈,帮忙一下
weblogic的Servlet创建之后,报空指针异常
但是找不出原因
所有代码如下:【只有一个servlet,且数据源配置正常,实验过了的,web.xml也配置完成】

package myservlet;
import java.awt.Container;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.spi.DirStateFactory.Result;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import com.mysql.jdbc.StatementImpl;

import javax.naming.*;
import javax.sql.*;

import java.sql.*;
import java.util.*;

public class StudentServlet extends HttpServlet {
    public StudentServlet(){
        super();
    }
    public void destroy() {
        super.destroy(); 
    }

    private static final String CONTENT_TYPE="text/html;charset=GBK";
//Do Get 方法
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
//打印网页      
        out.println("<HTML>");
        out.println("<HEAD><TITLE>StudentServlet</TITLE></HEAD>");
        out.println("<BODY bgcolor=\"#fffff\">");
        if ((request.getParameter("sno"))==null ||(request.getParameter("sno")).equals("input number")){
            out.println(
                    "<form method='post' action='/lab05-2/src/myservlet/StudentServlet'>");
        //"<form method='post' action='/lab05-2/StudentServlet'>");
            out.println("");
            out.println(
                    "学号查询:<input type='text' name='sno' vaule='input number'/>");
            out.println("<input type ='submit' vaule='查询'/><br>");
            out.println("</form>");
            }
        //另外一种情况
        else if(!(request.getParameter("sno")).equals("input number")){
            Vector vc =new Vector();
            out.println("<Form method='post' action='/lab05-2/src/myservlet/StudentServlet'>");
            out.println("");
            out.println("学生学号:<input type='text' name='sno' />");
            out.println("<input type ='submit' vaule='查询'/><br>");
            out.println("</form>");
            out.println("<hr>");
            out.println("<table border='1'><tr>");
            out.println("<th>学号</th><th>姓名</th><th>语文</th><th>数学</th></tr>");
        try{
            if((vc=accessDB(Integer.parseInt((String)request.getParameter("sno"))))!=null){
                out.println("<tr>");
                out.println("<td>"+vc.elementAt(0)+"</td>");
                out.println("<td>"+vc.elementAt(1)+"</td>");
                out.println("<td>"+vc.elementAt(2)+"</td>");
                out.println("<td>"+vc.elementAt(3)+"</td>");
                out.println("</tr>");
                }
            }catch(Exception e) {
                out.println("<tr>");
                out.println("<td>没有记录</td>");
                out.println("</tr>");
                out.println("<td>数据库里没有你要查询的记录</td>");
                e.printStackTrace();
            }
        out.println("</table>");
        }
        out.println("</BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
//Do Post方法
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
//Init方法
        public void init() throws ServletException {

        }


//通过weblogic的JNDI服务来查询数据库数据
    public Vector accessDB(int id){
    Vector vc=new Vector();
    Context ctx;

    javax.sql.DataSource ds = null;
    java.sql.Connection myConn = null ;

    final String JNDI_DATABASE_NAME="AA";
    Properties ht =new Properties();
    //Hashtable ht = new Hashtable();
    ht.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
    try{
        ctx = new InitialContext(ht);
        ds = (javax.sql.DataSource)ctx.lookup(JNDI_DATABASE_NAME);
    }catch(Exception e){
        e.printStackTrace();
    }
    if(ds == null){
        System.out.println("Eorror !!!");
    }
    else{
        System.out.println("Connection is OK !!!");
    }
    java.sql.PreparedStatement myStatement = null;
    ResultSet mySet = null;
    try{
                myConn=ds.getConnection();
                myStatement=myConn.prepareStatement("select * from stdb where sno=?");
                myStatement.setInt(1, id);
                mySet=myStatement.executeQuery();
                while (mySet.next()){
                    vc.add(Integer.toString(mySet.getInt("sno")));
                    vc.add(mySet.getString("sname"));
                    vc.add(Integer.toString(mySet.getInt("chinese")));
                    vc.add(Integer.toString(mySet.getInt("math")));
                }

                //关闭
                myStatement.close();
                mySet.close();
                myConn.close();
                return vc;//返回vc
            }catch(Exception e) {
                e.printStackTrace();
            }
            return vc;
    }
    public static void main(String[] args) {
        StudentServlet aa=new StudentServlet();
        System.out.println(aa.accessDB(100).elementAt(1));
    }
    }

图片说明

【aa和student只是测试着用,与StudentServlet无关】
报错为:
图片说明

大概就是这些,请指教,
初学,找过百度等也没有解决

3个回答

qq_44858980
qq_44858980 我的weblogic可以正常启动,我也看过这个文件了,是replica.num=0;所以还是不知道怎么解决?
8 个月之前 回复

可以debug跟下代码,aa.accessDB(100)返回的Vector类型对象应该为空导致,看看如下代码是否返回的vc为空?

 myConn=ds.getConnection();
                myStatement=myConn.prepareStatement("select * from stdb where sno=?");
                myStatement.setInt(1, id);
                mySet=myStatement.executeQuery();
                while (mySet.next()){
                    vc.add(Integer.toString(mySet.getInt("sno")));
                    vc.add(mySet.getString("sname"));
                    vc.add(Integer.toString(mySet.getInt("chinese")));
                    vc.add(Integer.toString(mySet.getInt("math")));
                }

已解决,谢谢大家

1.具体是跳转的页面,应当是项目名+web.xml里的URL
我写错为src的了
这个是出现数据越界错误,我也不清楚为什么>o<

2.没有导入weblogic.jar和wlfullclient.jar的包,所以出现了空指针错误

谢谢(*°∀°)=3大家了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐