java-ee - JavaWeb页面空指针异常
阿神
阿神 2017-04-17 17:40:13
0
5
614

一个javaweb项目,项目结构如下:

数据库操作类BaseDao:


    public class BaseDao {
        private Connection conn=null;
        private ResultSet rs=null;
        private PreparedStatement presta=null;
        
        
        private  Connection getConnection(){
            //String className=ConfigManager.getInstance().getString("jdbc.driver_class");
            String url=ConfigManager.getInstance().getString("jdbc.connection.url");
            String username=ConfigManager.getInstance().getString("jdbc.connection.username");
            String pwd=ConfigManager.getInstance().getString("jdbc.connection.password");
            
            try {
                conn=DriverManager.getConnection(url,username,pwd);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return conn;
        }
        
        /**
         * @Description: 判断连接是否打开
         * @param @return   
         * @return boolean  
         * @throws
         * @author ReticentZ
         * @date 2016年4月6日
         */
        
        
        public int executeUpdate(String sql,Object []params){
            int updateCount=0;
            
            if(conn==null){
                conn=getConnection();
            }
            
            try {
                presta=conn.prepareStatement(sql);
                if(params!=null && params.length>0){
                    for(int i=0;i<params.length;i++){
                        presta.setObject(i+1, params[i]);
                    }
                }
                updateCount=presta.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }            
            
            return updateCount;
        }
        
        public ResultSet executeQuery(String sql,Object []params){
            rs=null;
            
            if(conn==null){
                conn=getConnection();
            }
            System.out.println(conn);
            try {
                presta=conn.prepareStatement(sql);
                if(params!=null && params.length>0){
                    for(int i=0;i<params.length;i++){
                        presta.setObject(i+1, params[i]);
                    }
                }
                rs=presta.executeQuery();
            
            } catch (SQLException e) {
                e.printStackTrace();
            }
            
            return rs;
        }
        public boolean closeResource(){
            //按照先创建后释放的顺序释放
            if(rs!=null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(presta!=null){
                try {
                    presta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                conn=null;
            }
            
            return true;
        }
    }

AlbumDao类:

AlbumDaoImpl继承了BaseDao数据库操作类和AlumDao接口,其中有一个函数是返回部分查询值(getHostAlbumList(),就是在前端页面调用这个函数时出了空指针!):

 

    public class AlbumDaoImpl extends BaseDao implements AlbumDao {
        private ArrayList<Album> relist=new ArrayList<Album>();
        
        public static void main(String[] args) {
            AlbumDaoImpl test=new AlbumDaoImpl();
            ArrayList<Album> list=new ArrayList<Album>();
            list=test.getHostAlbumList();
            for(Album e:list){
                System.out.println(e.getGenreid()+"\t"+e.getArtist()+"\t"+e.getTitle());
            }
            
        }
        @Override
        public ArrayList<Album> getAlbumList() {
            String sql="select * from album";
            Object []params={};
            ResultSet res=null;
            res=this.executeQuery(sql,params);
            //System.out.println(res);
            try {
                while(res.next()){
                    Album reAlbum=new Album();
                    
                    //System.out.println(id+"\t"+artist+"\t"+title+"\t"+price+"\t"+dateReleased+"\t"+description);
                    reAlbum.setId(res.getInt("id"));
                    reAlbum.setArtist(res.getString("artist"));
                    reAlbum.setDateReleased(res.getDate("DateReleased"));
                    reAlbum.setDescription(res.getString("Description"));
                    reAlbum.setPrice(res.getDouble("Price"));
                    reAlbum.setGenreid(res.getInt("Genreid"));
                    reAlbum.setTitle(res.getString("Title"));
                    relist.add(reAlbum);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                this.closeResource();
            }
            return relist;
        }
    
        @Override
        public ArrayList<Album> getAlbumsByGenreId(int genreid) {
            String sql="select * from album where Genreid=?";
            Object []params={genreid};
            
            ResultSet res=null;
            res=this.executeQuery(sql,params);
            //System.out.println(res);
            try {
                while(res.next()){
                    Album reAlbum=new Album();
                    
                    //System.out.println(ids+"\t"+artist+"\t"+title+"\t"+price+"\t"+dateReleased+"\t"+description);
                    reAlbum.setId(res.getInt("id"));
                    reAlbum.setArtist(res.getString("artist"));
                    reAlbum.setDateReleased(res.getDate("DateReleased"));
                    reAlbum.setDescription(res.getString("Description"));
                    reAlbum.setPrice(res.getDouble("Price"));
                    reAlbum.setGenreid(res.getInt("Genreid"));
                    reAlbum.setTitle(res.getString("Title"));
                    relist.add(reAlbum);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                this.closeResource();
            }
            return relist;
        }
    
        @Override
        public ArrayList<Album> getAlbumsByArtist(String artist) {
            String sql="select * from album where Artist=?";
            Object []params={artist};
            
            ResultSet res=null;
            res=this.executeQuery(sql,params);
            //System.out.println(res);
            try {
                while(res.next()){
                    Album reAlbum=new Album();
                    //System.out.println(ids+"\t"+artist+"\t"+title+"\t"+price+"\t"+dateReleased+"\t"+description);
                    reAlbum.setId(res.getInt("id"));
                    reAlbum.setArtist(res.getString("artist"));
                    reAlbum.setDateReleased(res.getDate("DateReleased"));
                    reAlbum.setDescription(res.getString("Description"));
                    reAlbum.setPrice(res.getDouble("Price"));
                    reAlbum.setGenreid(res.getInt("Genreid"));
                    reAlbum.setTitle(res.getString("Title"));
                    relist.add(reAlbum);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                this.closeResource();
            }
            return relist;
        }
    
        @Override
        public boolean addAlbums(Album newalbum) {
            boolean reBool=false;
            int updateCount=-1;
            String sql="insert into album(id,genreid,artist,title,price,dateReleased,description) values(?,?,?,?,?,?,?)";
            Object[] params={
                    newalbum.getId(),
                    newalbum.getGenreid(),
                    newalbum.getArtist(),
                    newalbum.getTitle(),
                    newalbum.getPrice(),
                    newalbum.getDateReleased(),
                    newalbum.getDescription()};
            updateCount=this.executeUpdate(sql, params);
            if(updateCount>0)
                reBool=true;
            return reBool;
        }
    
        @Override
        public boolean deleteAlbums(int id) {
            boolean reBool=false;
            int updateCount=-1;
            String sql="delete from album where id=?";
            Object[] params={id};
            updateCount=this.executeUpdate(sql, params);
            if(updateCount>0)
                reBool=true;
            return reBool;
        }
    
        @Override
        public boolean deleteAlbums(String artist) {
            boolean reBool=false;
            int updateCount=-1;
            String sql="delete from album where Artist=?";
            Object[] params={artist};
            updateCount=this.executeUpdate(sql, params);
            if(updateCount>0)
                reBool=true;
            return reBool;
        }
    
        @Override
        public void updateAlbums(int id, Album album) {
            String sql="update Album set genreid=?,artist=?,title=?,price=?,dateReleased=?,description=? where id=?";
            Object []params={
                album.getGenreid(),    
                album.getArtist(),
                album.getTitle(),
                album.getPrice(),
                album.getDateReleased(),
                album.getDescription(),
                album.getId()
            };
            this.executeUpdate(sql, params);
        }
        @Override
        public Album getAlbumsById(int id) {
            String sql="select * from album where id=?";
            Album reAlbum=new Album();
            Object []params={id};
            ResultSet res=null;
            res=this.executeQuery(sql,params);
            //System.out.println(res);
            try {
                while(res.next()){    
                    //System.out.println(ids+"\t"+artist+"\t"+title+"\t"+price+"\t"+dateReleased+"\t"+description);
                    reAlbum.setId(res.getInt("id"));
                    reAlbum.setArtist(res.getString("artist"));
                    reAlbum.setDateReleased(res.getDate("DateReleased"));
                    reAlbum.setDescription(res.getString("Description"));
                    reAlbum.setPrice(res.getDouble("Price"));
                    reAlbum.setGenreid(res.getInt("Genreid"));
                    reAlbum.setTitle(res.getString("Title"));
                                    
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                this.closeResource();
            }
            return reAlbum;
        }
        @Override
        public ArrayList<Album> getHostAlbumList() {
            Album temp=new Album();
            int id=0;
            int []albumnum =new int[25];
            for(int i=0;i<25;i++){
                albumnum[i]=0;
            }
            int count=0;
            while(count<9){
                id=(int) (Math.random()*25);
                if(id==0)continue;
                if(albumnum[id]==0){
                    count++;
                    temp=this.getAlbumsById(id);
                    relist.add(temp);
                    System.out.println(temp.getId()+"\t"+temp.getTitle());
                    albumnum[id]=1;
                }
            }        
            
            return relist;
        }
    }

   

AlbumService类:(就是AlbumDaoImpl的服务层)

public class AlbumServiceImpl implements AlbumService {
    private ArrayList<Album> reList=new ArrayList<Album>();
    private AlbumDao albumdao=new AlbumDaoImpl();
    private Album al;
//    public static void main(String[] args) {
//        AlbumService albumService=new AlbumServiceImpl();
//        ArrayList<Album> list=new ArrayList<Album>();
//        list=albumService.getHostAlbumList();
//        
//        for(Album e:list){
//            System.out.println(e.getTitle());
//        }
//    }
    @Override
    public ArrayList<Album> getAlbumList() {
        reList=albumdao.getAlbumList();
        return reList;
    }

    @Override
    public ArrayList<Album> getAlbumsByGenreId(int genreid) {
        reList=albumdao.getAlbumsByGenreId(genreid);
        return reList;
    }

    @Override
    public ArrayList<Album> getAlbumsByArtist(String artist) {
        reList=albumdao.getAlbumsByArtist(artist);
        return reList;
    }

    @Override
    public boolean addAlbums(Album newalbum) {
        boolean flag=false;
        flag=albumdao.addAlbums(newalbum);
        
        return flag;
    }

    @Override
    public boolean deleteAlbums(int id) {
        boolean flag=false;
        flag=albumdao.deleteAlbums(id);
        return flag;
    }

    @Override
    public boolean deleteAlbums(String artist) {
        boolean flag=false;
        flag=albumdao.deleteAlbums(artist);
        return flag;
    }

    @Override
    public void updateAlbums(int id, Album album) {
        albumdao.updateAlbums(id, album);
    }

    @Override
    public Album getAlbumsById(int id) {
        al=albumdao.getAlbumsById(id);
        return al;
    }
    

    @Override
    public ArrayList<Album> getHostAlbumList() {
        reList=albumdao.getHostAlbumList();
        return reList;
    }
    

}

无论在AlbumDaoImpl和AlbumServiceImpl里这个getHostAlbumList()函数都是可以实现的,并且拿到数据的(后端测试图如下):


但是在前端的jsp页面中:

<p id="main">
                <h3 id="main-title">最新热销唱片</h3>
                <p id="hotAlbums">
                <%
                    AlbumService albumService=new AlbumServiceImpl();
                       Album temp=new Album();
                     ArrayList<Album> list=new ArrayList<Album>();
                     list=albumService.getHostAlbumList();
                     
                    for(Album e:list){
                        int id=e.getId();
                        String name=e.getTitle();
                        String picPath=String.valueOf(id)+".jpg";
                %>
                    <dl class="hotAlbumsItem">
                        <dt>
                            <a href="AlbumDetail.jsp?albumid=2"><img src="CoverImages/10.jpg" alt=""/></a>
                        </dt>
                        <dd><%= name %></dd>
                    </dl>
                <%
                }
                %>    
                </p>
            </p>

执行的时候是空指针异常!
控制台信息:

网页信息:

不明白为什么在前端会有空指针异常,但是后端数据又可以取出来!!!而且引起空指针异常的路径问题,既然在后端都没事,为什么在前端就无法识别。难不成在前端还要加载mysql驱动= =||。
最近百度找到一个javaweb同为空指针问题的解决方案,贴出来大家参考下附上链接。但是我所使用的lib中并没有这两个jar包...

还望各路大神指点!!!

阿神
阿神

闭关修行中......

reply all(5)
Ty80

In the BaseDao class: Class.forName() is not added;
After adding it, the null pointer problem is solved............................. ........................
But I still wonder why the data can be read by running the java file directly even without adding this sentence. Just because I saw that java files can be run and data can be read, I always thought that the database class was correct, and then ignored it. .......

Peter_Zhu

The code is posted directly in full

迷茫

Aren’t these layers injected through Spring? Then you have to manually create a new instance, check it

巴扎黑

The exception information is very clear, the null pointer in line 86 of BaseDao.

刘奇

Agree with the answer on the second floor.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template