【发布时间】:2016-01-28 08:45:38
【问题描述】:
这是我的第一个问题,但我一直在寻找解决方案 2 天,但没有成功。
在我的项目中,我有一个具有瞬态属性的 User 实体:
@Transient
@JsonProperty
private List<String> files;
我没有setter,getter是:
public List<String> getFiles() {
/* Call one static method */
}
在调试中使用 NetBeans 执行应用程序,工作正常,并且通过 javascript,我可以使用 user.fotos 获取 getFiles 结果。但是,当我生成 .jar 文件并使用命令 java -jar app.jar 执行应用程序时,通过调用一个必须返回一个 User 对象的 Rest 函数,我得到了这个异常:
2015-10-28 14:39:35.963 WARN 27836 --- [nio-7777-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"])
2015-10-28 14:39:35.963 WARN 27836 --- [nio-7777-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Handler execution resulted in exception: Could not write content: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.pfc.soriano.wsdbmodel.entity.User["files"])
我的问题是:Netbeans 与命令行 java -jar 有什么不同,这使它可以正常工作?
【问题讨论】:
-
所以你得到了一个 NPE,并且?只有你知道那是什么代码的哪一行...
-
你需要知道什么,尼尔?从方法 getFiles(),我调用了一个静态方法:
public static Collection<String> getImages(String prefix, String path) { List<String> result = new ArrayList<>(); File directorio = new File(path); TruequeFiltro filtro = new TruequeFiltro(prefix); for (File file : directorio.listFiles(filtro)) { result.add(file.getName()); } return result; } -
你有一个 NPE,所以找出哪一行,然后找出 WHAT 为空,然后修复它。
-
我不需要做任何事情来获得 NPE。我有一个用户控制器,带有
@Autowired UserDao userDao;和这个方法:` @RequestMapping(value = "findById", method = RequestMethod.POST) @ResponseBody public User findById(@Param("id") Long id) { return userDao.findOne (ID); }. UserDao is defined as:@RepositoryRestResource(collectionResourceRel = "users", itemResourceRel = "users") public interface UserDao extends JpaRepository{ `当我使用 ajax 从 javascript 调用 findById 方法时,我得到出现在我的第一条评论。 -
如果我使用 netbeans 运行应用程序,一切正常。如果我使用
java -jar app.jar命令运行应用程序,则没有任何效果。如果我删除瞬态字段files和getter 和setter,使用netbeans 或命令行一切正常。
标签: java spring hibernate jpa netbeans