获取文件路径报空指针
2016-01-06 10:43
375 查看
String filePath =request.getSession().getServletContext().getRealPath("/");
提示这行代码报空指针。
总代码是这样的:
//得到文件夹路径
String filePath1 = request.getSession().getServletContext().getRealPath("/");
String filePath = filePath1+"prCodeImages";
System.out.println(filePath);
File prCodeFile = new File(filePath);
if(!prCodeFile .exists() && !prCodeFile .isDirectory()){
prCodeFile .mkdir();
}按道理说当文件不存在,我判断了并新建,所以应该不可能出错。
这里报空指针的原因其实和获取文件没什么关系了。
是getsession时报错,当没有session对象时getsession是会报错的。
String filePath1 = request.getSession(true).getServletContext().getRealPath("/");这样加个true就没事了。
提示这行代码报空指针。
总代码是这样的:
//得到文件夹路径
String filePath1 = request.getSession().getServletContext().getRealPath("/");
String filePath = filePath1+"prCodeImages";
System.out.println(filePath);
File prCodeFile = new File(filePath);
if(!prCodeFile .exists() && !prCodeFile .isDirectory()){
prCodeFile .mkdir();
}按道理说当文件不存在,我判断了并新建,所以应该不可能出错。
这里报空指针的原因其实和获取文件没什么关系了。
是getsession时报错,当没有session对象时getsession是会报错的。
String filePath1 = request.getSession(true).getServletContext().getRealPath("/");这样加个true就没事了。
相关文章推荐
- C#获取路径的几种方式实例分析
- 解析C语言中空指针、空指针常量、NULL & 0的详解
- JavaScript获取路径设计源码
- java 代码中预防空指针异常的处理办法
- [学习笔记]C++空指针异常捕获
- 【java基础】引用类型数组初始化中,setter和getter函数空指针问题
- AMapNavi.getInstance(this) 返回值为null 的解决方法
- 段错误(segmentation fault)
- 0x00881789 处的第一机会异常(在 receive_filter.exe 中): 0xC0000005: 读取位置 0x00000000 时发生访问冲突。
- layoutinflater 空指针错误
- spinner notnullpoint 的出现
- Storm OutputCollector并发问题导致NullPointerException的解决
- 空指针和野指针的区别
- 关于线程调用Spring注解对象报空指针异常
- 解决4.0以下继承PopupWindow报空指针的bug
- 关于C程序中空指针(null指针)的设计哲学
- 【android】null pointer exception
- 浏览根目录下index.jsp 报 空指针
- 关于equals的一点小窍门
- Android学习笔记:继承popupwindow 报空指针问题