老师,我这里报错帮忙看看

老师,我这里报错帮忙看看

package com.imooc.mgallery.utils;


import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.ArrayList;

import java.util.List;


import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.Node;

import org.dom4j.io.SAXReader;


import com.imooc.mgallery.entity.Painting;




/*

 * 数据源类

 */


public class XmlDatasource {

//通过static保证数据唯一

private static List<Painting> data=new ArrayList();

private static String dataFile;

static {

dataFile=XmlDatasource.class.getResource("/painting.xml").getPath();

URLDecoder decoder=new URLDecoder();

try {

dataFile=decoder.decode(dataFile,"uft-8");

//System.out.println(dataFile);

//dom4j 解析

SAXReader reader=new SAXReader();

//获取文档对象

Document document=reader.read(dataFile);

//得到xml节点

List<Node> nodes=document.selectNodes("/root/painting");

//遍历node节点

for(Node node:nodes) {

Element element=(Element)node;

String id=element.attributeValue("id");

String pan=element.elementText("pname");

Painting painting=new Painting();

painting.setId(Integer.parseInt(id));

painting.setPname(pan);

painting.setCategroy(Integer.parseInt(element.elementText("category")));

painting.setPrice(Integer.parseInt(element.elementText("price")));

painting.setPreview(element.elementText("preview"));

painting.setDecription(element.elementText("description"));

data.add(painting);

// System.out.println(id+""+pan);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static List<Painting> getRawData(){

return data;

}

public static void main(String[] args) {

// new XmlDatasource();

List <Painting> ps=XmlDatasource.getRawData();

System.out.println(ps);

}

}



package com.imooc.mgallery.utils;


import java.util.ArrayList;

import java.util.List;


public class PageModel {

private int paga;//页号

private int totalPages;//页总数

private int rows;//每页记录

private int totalRows;//总记录数

private int pageStartRow;//当前页从第N行开始

private int pageEndRow;//当前页从第N行结束

private boolean hasNextPage;//是否存在下一页

private boolean hasPreviousPage;//是否存在上一页

private List pageData;//当前页数据

public  PageModel() {

}

/**

* 初始化对象

* @param Data 原来数据集合

* @param paga 页号

* @param rows 每页计数

*/

public  PageModel(List data,int paga,int rows) {

this.paga=paga;

this.rows=rows;

totalRows=data.size();

totalPages=new Double(Math.ceil(pageEndRow/(rows*1f))).intValue();

if(pageStartRow>pageEndRow) {

pageStartRow=pageEndRow;

}

pageStartRow=(paga-1)*rows;

pageEndRow=paga*rows;

pageData=data.subList(pageStartRow, pageEndRow);

if(paga>1) {

hasPreviousPage = true;

}else {

hasPreviousPage = false;

}

if(paga<totalPages) {

hasNextPage=true;

}else {

hasNextPage=false;

}

}

public int getPaga() {

return paga;

}

public void setPaga(int paga) {

this.paga = paga;

}

public int getTotalPages() {

return totalPages;

}

public void setTotalPages(int totalPages) {

this.totalPages = totalPages;

}

public int getRows() {

return rows;

}

public void setRows(int rows) {

this.rows = rows;

}

public int getTotalRows() {

return totalRows;

}

public void setTotalRows(int totalRows) {

this.totalRows = totalRows;

}

public int getPageStartRow() {

return pageStartRow;

}

public void setPageStartRow(int pageStartRow) {

this.pageStartRow = pageStartRow;

}

public int getPageEndRow() {

return pageEndRow;

}

public void setPageEndRow(int pageEndRow) {

this.pageEndRow = pageEndRow;

}

public boolean isHasNextPage() {

return hasNextPage;

}

public void setHasNextPage(boolean hasNextPage) {

this.hasNextPage = hasNextPage;

}

public boolean isHasPreviousPage() {

return hasPreviousPage;

}

public void setHasPreviousPage(boolean hasPreviousPage) {

this.hasPreviousPage = hasPreviousPage;

}

public List getPageData() {

return pageData;

}

public void setPageData(List pageData) {

this.pageData = pageData;

}

public static void main(String[] args) {

List list=new ArrayList();

for(int i=0;i<=100;i++) {

list.add(i);

}

PageModel pa=new PageModel(list,6,8);

System.out.println(pa.getPageData());

System.out.println(pa.getTotalPages());

System.out.println(pa.getPageEndRow()+":"+pa.getPageStartRow());

}

}


package com.imooc.mgallery.dao;


import java.util.List;


import com.imooc.mgallery.entity.Painting;

import com.imooc.mgallery.utils.PageModel;

import com.imooc.mgallery.utils.XmlDatasource;


public class PainTingDao {

public PageModel pagination(int paga,int rows) {

List<Painting> list =XmlDatasource.getRawData();

PageModel pa=new PageModel(list,paga,rows);

return pa;

}

}



package com.imooc.mgallery.service;


import java.util.List;


import com.imooc.mgallery.dao.PainTingDao;

import com.imooc.mgallery.entity.Painting;

import com.imooc.mgallery.utils.PageModel;


public class PaintingService {

private PainTingDao dao=new PainTingDao();

public PageModel pagination(int paga,int rows) {

if(rows==0) {

throw new RuntimeException("无效的参数");

}

return dao.pagination(paga, rows);

}

public static void main(String[] args) {

PaintingService Pa=new PaintingService();

PageModel pp=Pa.pagination(3, 5);

List<Painting> PaintingList=pp.getPageData();

for(Painting pa :PaintingList) {

System.out.println(pa.getPname());

}

System.out.println(pp.getPageStartRow()+":"+pp.getPageEndRow());

}

}



package com.imooc.mgallery.controller;


import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.imooc.mgallery.service.PaintingService;

import com.imooc.mgallery.utils.PageModel;


/**

 * Servlet implementation class Paintingcontroller

 */

@WebServlet("/pa")

public class Paintingcontroller extends HttpServlet {

private static final long serialVersionUID = 1L;

private PaintingService pa=new PaintingService();

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public Paintingcontroller() {

        super();

        // TODO Auto-generated constructor stub

    }


/**

* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String paga=request.getParameter("p");

String rows=request.getParameter("r");

if(paga==null) {

paga="1";

}

if(rows==null) {

rows="6";

}

PageModel md=pa.pagination(Integer.parseInt(paga),Integer.parseInt(rows));

request.setAttribute("md", md);

request.getRequestDispatcher("WEB-INF/jsp/index.jsp").forward(request, response);;

}


}



<%@page contentType="text/html;charset=UTF-8"%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Document</title>

<link rel="stylesheet" type="text/css" href="css\common.css">

<script type="text/javascript" src="js\js1.js"></script>

</head>

<body>

<div class="header">

<div class="logo">

<img src="image\logo.png">

</div>

<div class="menu"   onclick="show_menu()" onmouseleave="show_menu1()">

<div class="menu_title" ><a href="###">内容分类</a></div>

<ul id="title">

<li>现实主义</li>

<li>抽象主义</li>

</ul>

</div>

<div class="auth">

<ul>

<li><a href="#">登录</a></li>

<li><a href="#">注册</a></li>

</ul>

</div>

</div>

<div class="content">

  <div class="banner">

   <img src="image/welcome.png" class="banner-img">

  </div>

  <div class="img-content">

<ul>

<c:forEach items="${md.pagaData}" var="painting">

<li>

<img src="${painting.preview}" class="img-li">

<div class="info">

<h3>${painting.pname}</h3>

<p>

${painting.description }

</p>

<div class="img-btn">

<div class="price"><fmt:formatNumber pattern="¥0.00" value="${painting.price}"></fmt:formatNumber></div>

<a href="#" class="cart">

       <div class="btn">

      <img src="image/cart.svg">

       </div>

    </a>

</div>

</div>

</li>

</c:forEach>

</ul>

  </div>

  <div class="page-nav">

<ul>

<li><a href="#">首页</a></li>

<li><a href="#">上一页</a></li>

<li><span class="first-page">1</span></li>

<li><a href="#">2</a></li>

<li><a href="#">3</a></li>

<li><a href="#">4</a></li>

<li><a href="#">5</a></li>

<li><a href="#">…</a></li>

<li><a href="#">98</a></li>

<li><a href="#">99</a></li>

<li><a href="#">下一页</a></li>

<li><a href="#">尾页</a></li>

</ul>

  </div>

</div>

<div class="footer">

<p><span>M-GALLARY</span>©2020 POWERED BY IMOOC.INC</p>

</div>

</body>

</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿园 2021-01-18 13:45:48

同学你好,是哪里报错呢,建议同学将报错信息进行反馈,便于老师定位问题;并且建议同学描述是做了什么样的操作后报错呢

祝学习愉快~

  • 提问者 热爱编程学习 #1

    http://img1.sycdn.imooc.com//climg/600561e1097baf8307680409.jpg

    2021-01-18 18:24:35
  • 提问者 热爱编程学习 #2

    老师这里是什么问题?


    2021-01-18 18:25:03
  • 同学你好,根据报错信息是在PaintingService的第18行出现了错误,拼接测试代码没有出现同学这样的错误

    http://img1.sycdn.imooc.com//climg/6005706509b1861d12960602.jpg

    错误信息表示在Integer.parseInt()转换时参数是null,所以也就无法转换为Integer类型了

    建议同学检查下报错位置处的代码,同学可以下载源码运行试试,并且进行对照

    如果还有问题建议同学指出Service18行和controller的42行代码

    祝学习愉快~

    2021-01-18 19:29:26
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师