老师,求助 添加分类提示空指针异常
package com.imooc.icake.global; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class GlobalController extends GenericServlet { public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { /* .do /login.do DefaultController login /Cake/detail.do CakeController detail /admin/Cake/add.do CakeController add */ HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; String path=request.getServletPath(); if (path.indexOf("/admin") != -1) { path=path.substring(7); } else { path=path.substring(1); } String className=null; String methodName=null; int index=path.indexOf("/"); if (index != -1) { className = "com.imooc.icake.controller." + path.substring(0, index)+"Controller"; methodName=path.substring(index+1, path.indexOf(".do")); } else { className="com.imooc.icake.controller."+"DefaultController"; methodName=path.substring(0, path.indexOf(".do")); } try { Class cla = Class.forName(className); Object object = cla.newInstance(); Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); method.invoke(object, request, response); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } package com.imooc.icake.biz; import com.imooc.icake.entity.Catalog; import java.util.List; public interface CatalogBiz { void add(List<Catalog> list); void remove(int id); Catalog getRoot(); } package com.imooc.icake.biz.impl; import com.imooc.icake.biz.CatalogBiz; import com.imooc.icake.dao.CatalogDao; import com.imooc.icake.entity.Catalog; import com.imooc.icake.global.DaoFactory; import java.util.List; public class CatalogBizImpl implements CatalogBiz { CatalogDao catalogDao = DaoFactory.getInstance().getDao(CatalogDao.class); public void add(List<Catalog> list) { catalogDao.batchInsert(list); } public void remove(int id) { catalogDao.delete(id); } public Catalog getRoot() { return catalogDao.select(10000); } } package com.imooc.icake.dao; import com.imooc.icake.entity.Catalog; import org.apache.ibatis.annotations.*; import java.util.List; public interface CatalogDao { /** * 新增 * @param list */ @Insert("<script>" + "insert into catalog(title,pid,info) values"+ "<foreach connection='list' var='catalog' separator=','>" + "(#{catalog.title},#{catalog.pid},#{catalog.info})" + "</foreach>"+ "</script>") @Options(useGeneratedKeys = true,keyProperty = "id") void batchInsert( List<Catalog> list); /** * 删除 */ @Delete("delete from catalog where id=#{id}") void delete(int id); /** * 查询所有分类 */ @Select("select * from catalog where id=#{id}") @Results(id = "all",value = { @Result(column = "id", property = "id", id = true), @Result(column = "title",property = "title"), @Result(column = "pid", property = "pid"), @Result(column = "info", property = "info"), @Result(column = "id",property = "children",many = @Many(select = "selectByPid")) }) Catalog select(int id); @Select("select * from catalog where pid=#{pid}") @ResultMap("all") List<Catalog> selectByPid(int pid); } Catalog{id=0, title='生日', pid=10000, info='好吃', children=[]} [Catalog{id=0, title='生日', pid=10000, info='好吃', children=[]}] java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.imooc.icake.global.GlobalController.service(GlobalController.java:44) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at com.imooc.icake.global.EncodingFilter.doFilter(EncodingFilter.java:21) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:748) Caused by: org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: java.lang.NullPointerException ### Cause: java.lang.NullPointerException at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200) at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:57) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) at com.sun.proxy.$Proxy14.batchInsert(Unknown Source) at com.imooc.icake.biz.impl.CatalogBizImpl.add(CatalogBizImpl.java:13) at com.imooc.icake.controller.CatalogController.add(CatalogController.java:52) ... 30 more Caused by: java.lang.NullPointerException at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) at org.apache.ibatis.scripting.xmltags.OgnlCache.parseExpression(OgnlCache.java:51) at org.apache.ibatis.scripting.xmltags.OgnlCache.getValue(OgnlCache.java:44) at org.apache.ibatis.scripting.xmltags.ExpressionEvaluator.evaluateIterable(ExpressionEvaluator.java:43) at org.apache.ibatis.scripting.xmltags.ForEachSqlNode.apply(ForEachSqlNode.java:55) at org.apache.ibatis.scripting.xmltags.MixedSqlNode.apply(MixedSqlNode.java:33) at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:41) at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:292) at org.apache.ibatis.executor.statement.BaseStatementHandler.<init>(BaseStatementHandler.java:64) at org.apache.ibatis.executor.statement.PreparedStatementHandler.<init>(PreparedStatementHandler.java:40) at org.apache.ibatis.executor.statement.RoutingStatementHandler.<init>(RoutingStatementHandler.java:46) at org.apache.ibatis.session.Configuration.newStatementHandler(Configuration.java:545) at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:48) at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117) at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:198) ... 36 more
0
收起
正在回答 回答被采纳积分+1
2回答
指尖板命狂敲
2019-03-05 19:52:48
package com.imooc.icake.controller; import com.imooc.icake.biz.CatalogBiz; import com.imooc.icake.biz.impl.CatalogBizImpl; import com.imooc.icake.entity.Catalog; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class CatalogController { private CatalogBiz catalogBiz=new CatalogBizImpl(); /** * 列表页 * @param request * @param response * @throws ServletException * @throws IOException */ // /admin/Catalog/list.do public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Catalog root=catalogBiz.getRoot(); request.setAttribute("root", root); request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_list.jsp").forward(request,response); } // /admin/Catalog/toAdd.do public void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Catalog root=catalogBiz.getRoot(); request.setAttribute("root", root); request.getRequestDispatcher("/WEB-INF/pages/admin/catalog_add.jsp").forward(request,response); } // /admin/Catalog/add.do public void add(HttpServletRequest request, HttpServletResponse response) throws IOException { String[] titles = request.getParameterValues("title"); String[] pids = request.getParameterValues("pid"); String[] infos = request.getParameterValues("info"); List<Catalog> catalogs = new ArrayList<Catalog>(); for (int i = 0; i < titles.length; i++) { Catalog catalog=new Catalog(); catalog.setTitle(titles[i]); catalog.setPid(Integer.parseInt(pids[i])); catalog.setInfo(infos[i]); System.out.println(catalog); catalogs.add(catalog); } System.out.println(catalogs); catalogBiz.add(catalogs); response.sendRedirect("list.do"); } // /admin/Catalog/remove.do public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException { int id = Integer.parseInt(request.getParameter("id")); catalogBiz.remove(id); response.sendRedirect("list.do"); } } 老师,我debug了一遍还是没找到原因
Java数据库开发与实战应用2018版
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星