我是一名新的Java开发人员。当运行带有两种情况的Spring项目时:
-创建另一个主体,然后调用JpaRepository方法。
-从controller.impl中,我使用JpaRepository方法调用类的方法。
我遇到错误:
java.lang.NullPointerException
at solution.Testclass.getvalue(Testclass.java:20)
at mydemo.controller.testmain.main(testmain.java:11)
我尝试添加许多符号,但是它不起作用。
这是我的代码。
主要:
package mydemo.controller;
import solution.Testclass;
public class testmain {
public static void main(String[] args) {
Testclass testclass = new Testclass();
testclass.getvalue();
}
}
Testclass.java:
package solution;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import mydemo.entity.HistoryUpload;
import mydemo.repository.HistoryUploadRepository;
public class Testclass {
@Autowired HistoryUploadRepository historyUploadRepository;
List<HistoryUpload> listHistoryUploadNullFlag;
public List<HistoryUpload> getvalue(){
try {
this.listHistoryUploadNullFlag = historyUploadRepository.findAll();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return this.listHistoryUploadNullFlag;
}
}
我的存储库:
package mydemo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import mydemo.entity.HistoryUpload;
public interface HistoryUploadRepository extends JpaRepository<HistoryUpload, Integer>{
// List<HistoryUpload> findByStatus(String p_status);
// @Query(value = "SELECT o.* FROM history_upload o where o.status = ?1 AND o.flag_synchronize is null",nativeQuery = true)
// List<HistoryUpload> findByStatusAndNullFlagSynchronize(String p_status);
}
我的实体
package mydemo.entity;
import java.sql.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Entity
@Table(name="history_upload")
@Data
public class HistoryUpload {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Integer logFileId;
private Integer flagSynchronize;
private Integer instance_id;
private String instanceName;
private Date uploadTime;
private String status;
}
请帮助我! 感谢您的阅读。
答案 0 :(得分:0)
您似乎没有将应用程序作为Spring Boot应用程序运行,这将不允许在存储库中创建Bean,从而在您尝试调用存储库方法getAll()
这可以像这样添加到您的主类中:
@SpringBootApplication(scanBasePackages = *your base package, eg com.test.application*) // this is not essential but required if you use a non standard directory structure
public class TestMain {
public static void main(String[] args) {
SpringApplication.run(TestMain.class, args);
// you will now need to access your test class through a spring bean - see below for an example using a controller endpoint:
}
}
// in TestController
@RestController
class TestController {
private final Testclass testClass;
@Autowired
public TestController(TestClass testClass) {
this.testCLass = testClass
}
// now a simple endpoint to get the data, call this in postman to see the result
@GetMapping(value = "/")
public ResponseEntity<List<HistoryUpload>> getHsitoryList() {
return new ResponseEntity.ok().body(TestClass.getvalue());
}
}
还要避免在try catch块中捕获通用异常,根据Java cert标准,这通常是一种不好的做法,尤其是当您捕获NullPointerExceptions(这些异常永远不会被捕获,请阅读更多here)
在将来进一步研究日志记录时,调用e.print堆栈跟踪可能会提高性能,而且记录器还提供了更多配置,请阅读here
我建议您研究使用专用的异常处理程序类,您可以阅读有关here的信息
关于目录结构,Spring可能很挑剔,请确保所有bean(控制器,存储库,服务等)都位于主应用程序的以下目录中:
com
your
app
Main.java
controller / your controllers
repositories / your repos
....