JavaLord123
2015-11-22 13:28
采纳率: 100%
浏览 2.6k
已采纳

访问jsp页面时出现空指针异常,求大神解决

List中值是null,不知道怎么取到值。。。
JAVA CODE:
List listBrand = new ArrayList();
while(rs.next()){
BrandVO brand = new BrandVO();
brand.setId(rs.getInt("id"));
brand.setBname(rs.getString("name"));
listBrand.add(brand);
}
req.setAttribute("carList", rs.getInt("id"));
req.getRequestDispatcher("add_car.jsp").forward(req, resp)

        jsp中代码:
      <%
        List<BrandVO> listBrand = (List) request.getAttribute("carList");

                                for(BrandVO brand : listBrand){
                                %>
                                    <option><%=brand.getBname()%></option>
                                    <% }%>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • zhyu071010 2015-11-22 13:41
    已采纳

    看样子你是使用的迭代器啊,你的rs中有值吗?先打个断点看看

    已采纳该答案
    打赏 评论
  • yanglei00008 2015-11-22 13:31

    req.setAttribute("carList", rs.getInt("id"));

    改成 req.setAttribute("carList", listBrand);

    打赏 评论
  • 感恩的心_Dream 2015-11-22 14:04

    打断点,调试一下,一步一步的查一下,看看在传值的时候哪个地方错了

    打赏 评论
  • JavaLord123 2015-11-22 14:25

    <%
    List listBrand = (List) request.getAttribute("carList");
    if(listBrand!=null){
    for(BrandVO brand : listBrand){

                                    %>
                                        <option><%=brand.getBname()%></option>
                                        <% }}%>
    
    打赏 评论
  • JavaLord123 2015-11-22 14:25

    package com.rentcar.service;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;

    import org.apache.catalina.Session;

    import com.rentcar.vo.BrandVO;
    public class AddCarServer extends HttpServlet{
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    doPost(req, resp);
    }
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/rent","root","123456");
    Statement sta = ct.createStatement();
    ResultSet rs = sta.executeQuery("SELECT * FROM tab_brand");
    //PreparedStatement ps = ct.prepareStatement("SELECT * FROM tab_brand");
    //ResultSet rs = ps.executeQuery();
    List listBrand = new ArrayList();
    while(rs.next()){
    BrandVO brand = new BrandVO();
    brand.setId(rs.getInt("id"));
    brand.setBname(rs.getString("name"));
    listBrand.add(brand);
    }

            req.setAttribute("carList",listBrand);
            req.getRequestDispatcher("add_car.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    }

    打赏 评论
  • JavaLord123 2015-11-22 15:30

    <%@page import="com.rentcar.vo.BrandVO"%>
    <%@page import="java.util.List"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



    租车管理系统

















                </ul>
            </div>
            <div class="user_infor">
                <span class="uesr_name">欢迎您!</span> <span>租车管理系统</span>
            </div>
            <div class="clr"></div>
        </div>
        <div class="base_content">
            <div class="car_pay_content" style="border: none;">
                <div class="car_dynamic_option">
                    <div class="user_infor_right">
                        <div class="user_dest_top">
                            <div class="dest_top_one"></div>
                            <div class="dest_top_two">
                                <h1>添加车辆信息</h1>
                            </div>
                            <div class="dest_top_three"></div>
                            <div class="clr"></div>
                        </div>
    
                        <div class="user_dest_content">
                            <ul>
                                <li><label><abbr title="required">*</abbr> 车辆品牌:</label>
                                    <select>
                                    <%
                                    List<BrandVO> listBrand = (List) request.getAttribute("carList");
                                    if(listBrand!=null){
                                    for(BrandVO brand : listBrand){
    
                                    %>
                                        <option><%=brand.getBname()%></option>
                                        <% }}%>
    
                                </select></li>
                                <li><label><abbr title="required">*</abbr> 车辆型号:</label> <select>
                                        <option>1</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                        <option>5</option>
                                </select></li>
                                <li><label>大陆牌照:</label> <input type="text" /></li>
                                <li><label>香港牌照:</label> <input type="text" /></li>
                                <li><label>颜色:</label> <input type="text" /></li>
                                <li><label>所属门店:</label> <select>
                                        <option>1</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                        <option>5</option>
                                </select></li>
                                <li><label><abbr title="required">*</abbr> 状态:</label> <select>
                                        <option>1</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                        <option>5</option>
                                </select></li>
                                <div class="clr"></div>
                            </ul>
    
                            <div class="laye_form_botton">
                                <div class="save_button">
                                    <span class="input_left"></span> <a><input type="submit"
                                        value=" 新增车辆信息 " /></a> <span class="input_right"></span>
                                </div>
                                <div class="cancel_button">
                                    <span class="input_left"></span> <a><input type="button"
                                        value="取消"/></a> <span class="input_right"></span>
                                </div>
                            </div>
                            <!--laye_form_botton-->
                        </div>
                        <!--user_dest_content-->
    
    
                        <div class="user_dest_bottom">
                            <div class="dest_bottom_le"></div>
                            <div class="dest_bottom_ce"></div>
                            <div class="dest_bottom_ri"></div>
                            <div class="clr"></div>
                        </div>
                    </div>
                    <div class="clr"></div>
                </div>
    
            </div>
            <div class="import_information">
                <ul class="infor_one">
                    <li class="infor_one_text">重要提醒</li>
                    <li><a>今日订单数量<span class="red">( 10 )</span></a></li>
                    <li><a>今日用车数量<span class="red">( 4 )</span></a></li>
                    <li><a>今日还车数量<span class="red">( 20 )</span></a></li>
                    <li><a>本月车辆年检提醒<span class="red">( 30 )</span></a></li>
                    <li class="infor_month"><a>本月保险到期提醒<span class="red">(
                                10 )</span></a></li>
                    <li class="infor_one_text">常用工具</li>
                    <li class="infor_two_text"><a> <span class="icon_violate"></span>
                            <span class="icon_text">违章信息查询</span>
                    </a></li>
                    <li class="infor_two_text"><a> <span class="icon_plain"></span>
                            <span class="icon_text">航班信息查询</span>
                    </a></li>
    
                    <li class="infor_two_text"><a> <span class="icon_trail"></span>
                            <span class="icon_text">列车信息查询</span>
                    </a></li>
                    <li class="infor_two_text"><a> <span class="icon_user"></span>
                            <span class="icon_text">身份证查询</span>
                    </a></li>
    
                    <li class="infor_two_text"><a> <span class="icon_gps"></span>
                            <span class="icon_text">GPS导航</span>
                    </a></li>
                </ul>
            </div>
        </div>
    </div>
    <div class="foot">
        <p>版权所有(C)2009-2014 朗思科技</p>
    </div>
    


    打赏 评论
  • 紫气天堂 2015-11-23 01:39

    你最好打个断点 调试一下,看的话很难看出来

    打赏 评论

相关推荐 更多相似问题