【发布时间】:2021-08-24 10:27:01
【问题描述】:
这是我的 Spring Boot 应用程序。 当我运行 main 方法时,总是抛出一个空指针异常。 我不知道为什么 @Autowired JsonReaderService 为空。正如我将其定义为组件。
它是项目 src 文件夹中的子文件夹,因此 Main Method 位于源文件夹之上。那么spring应该正确扫描吧??
我还有一个测试方法,效果很好。
@SpringBootApplication
public class DemoApplication {
@Autowired
private JsonReaderService jsonReaderService;
private static JsonReaderService stat_jsonReaderService;
static Logger logger = LoggerFactory.getLogger(DemoApplication.class);
public static void main(String[] args) throws IOException {
String textFileName = scanFileName();
Reader reader = Files.newBufferedReader(Paths.get("src/main/resources/" + textFileName));
// This line is always null pointer exception. The @autowired Annotation don't work with my JsonReaderService????? WHY
List<EventDataJson> jsonReaderServicesList = stat_jsonReaderService.readEventDataFromJson(reader);
stat_jsonReaderService.mappingToDatabase(jsonReaderServicesList);
}
public static String scanFileName() {
logger.info("Scanning keyboard input");
System.out.println("enter a file to scan");
Scanner scanInput = new Scanner(System.in);
String text = scanInput.nextLine();
logger.info("successful keyboard input was : " + text);
return text;
}
@PostConstruct
public void init() {
logger.info("initializing Demo Application");
stat_jsonReaderService = jsonReaderService;
}
}
这里我有一个类,它使用存储库@Autowired 来保存一些实体,但我总是在 repository.save(...) 行中得到一个空指针异常
@Component
public class JsonReaderService {
static Logger logger = LoggerFactory.getLogger(DemoApplication.class);
@Autowired
EventDataRepository repository;
private Reader reader;
private List<EventDataJson> eventDataList;
@Autowired
public JsonReaderService(){}
public List<EventDataJson> readEventDataFromJson(Reader reader) throws IOException {
try {
logger.info("parsing event data from json started");
Gson gson = new Gson();
EventDataJson[] eventData = gson.fromJson(reader, EventDataJson[].class);
eventDataList = Arrays.asList(eventData);
reader.close();
} catch (IOException e) {
logger.error("Error while reading the json file");
e.printStackTrace();
}
logger.info("parsing json eventData successful finished");
return eventDataList;
}
public Boolean mappingToDatabase(List<EventDataJson> eventDataList) {
logger.info("mapping from json to database eventData started ...");
Set<String> idList = eventDataList.stream().map(EventDataJson::getId).collect(Collectors.toSet());
for (String id : idList
) {
Stream<EventDataJson> filteredEventDataList1 = eventDataList.stream().filter((item) -> item.getId().equals(id));
Stream<EventDataJson> filteredEventDataList0 = eventDataList.stream().filter((item) -> item.getId().equals(id));
EventDataJson startedEvent = filteredEventDataList1.filter((item) -> item.getState().equals("STARTED")).findAny().orElse(null);
EventDataJson finishedEvent = filteredEventDataList0.filter((item) -> item.getState().equals("FINISHED")).findAny().orElse(null);
long duration0 = finishedEvent.getTimestamp() - startedEvent.getTimestamp();
Boolean alert;
if (duration0 > 4) {
alert = true;
} else {
alert = false;
}
try {
this.repository.save(new EventDataDb(id, duration0, startedEvent.getType(), startedEvent.getHost(), alert));
logger.info("mapping to Database Repository action successful");
} catch (Exception e) {
logger.error("Exception in database mapping occurred");
e.printStackTrace();
return false;
}
}
return true;
}
}
带注释的存储库
@Repository
public interface EventDataRepository extends JpaRepository<EventDataDb, String> {
EventDataDb findAllById(String id);
}
测试用例与@autowired Annotation 一起工作得很好我不知道为什么它在 main 方法中不起作用。是因为它是静态的吗?
@Autowired
private EventDataRepository repository;
@Autowired
private JsonReaderService jReader;
@Test
public void whenParseJson_thenTransform_and_save_to_db() throws IOException {
BufferedReader reader = Files.newBufferedReader(Paths.get("src/main/resources/" + "logfile.txt"));
List<EventDataJson> eventDataList1 = jReader.readEventDataFromJson(reader);
if (jReader.mappingToDatabase(eventDataList1)) {
EventDataDb eventDataFromDb = this.repository.findAllById("scsmbstgra");
Assertions.assertTrue(eventDataFromDb.getType().equals("APPLICATION_LOG"));
Assertions.assertTrue(eventDataFromDb.getHost().equals("12345"));
Assertions.assertTrue(eventDataFromDb.getAlert().equals(true));
Assertions.assertTrue(eventDataFromDb.getDuration() == 5);
logger.info("Assert successfully accomplished");
} else
logger.error("Could not persist eventData to DB Error");
}
堆栈跟踪
`线程“restartedMain”java.lang.reflect.InvocationTargetException中的异常
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
原因:java.lang.NullPointerException 在 com.creditsuisse.demo.DemoApplication.main(DemoApplication.java:33) ... 5 更多`
【问题讨论】:
标签: java spring-boot components autowired