尝试通过Springboot保存/更新到MySQL数据库时出现Java NullPointerException



前言:试图通过springboot保存/更新到mysql数据库。尝试访问DAOImpl类时在中获取空指针异常:

这是实体类:

import javax.persistence.*;
@Entity
@Table(name="tracker")
public class Tracker {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="text")
private String text;
@Column(name="link")
private String link;
public Tracker() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
@Override
public String toString() {
return "Tracker{" +
"id=" + id +
", text='" + text + ''' +
", link='" + link + ''' +
'}';
}
}

这是DAO类:

import com.example.Tracker;
import java.util.List;
public interface DAO {
public List<Tracker> getItems();
public void saveItems(String text, String link);
public Tracker getItem(int theId);
public void deleteItem(int theId);
}

以下是DAO类的实现类:

package com.example.tracker.dao;
import com.example.tracker.entity.Tracker;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.HashMap;
import java.util.List;
@Repository
public class DOAImpl implements DAO{
@Autowired
private SessionFactory sessionFactory;
@Override
public List<Tracker> getItems() {
//To-Do
return null;
}
@Override
public void saveItems(String text, String link) {
Tracker item = new Tracker();
System.out.println(text);
System.out.println(link);
item.setLink(link);
item.setText(text);
Session currentSession = sessionFactory.getCurrentSession();
currentSession.saveOrUpdate(item);
}
@Override
public Tracker getItems(int theId) {
Session currentSession = sessionFactory.getCurrentSession();
return null;
}
@Override
public void deleteItem(int theId) {
//To-Do
}
}

这是做这项工作的类:

package com.example.WebScrapper;
import com.example.dao.DOAImpl;
import com.example.entity.Tracker;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.util.*;
public class itemScraper {
private String baseURL = "https://www.example-page.com/page/";
private int page = 1;
@Autowired
private DOAImpl itemDOA;
WebScraper itemScraper = new WebScraper();
public String getAllPages() throws IOException {
while(true){
String webpage = baseURL + page;
itemScraper.setWebURL(webpage);
itemScraper.getWebReturn();
HashMap pageItems = itemScraper.getElements();
Iterator iterator = pageItems.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry currentItem = (Map.Entry) iterator.next();
itemDOA.saveItems((String) currentItem.getKey(), (String) currentItem.getValue());
System.out.println("Item Saved");
}
}
}
}

当调用itemDAO.saveItems时,这就是抛出空指针的时候。我已经检查过了,currentItem值和键都存在。但是抛出了一个空指针。所以,不知道我为什么会遇到这个问题,任何帮助都很感激。

StackTrace上下文:

Exception in thread "restartedMain" 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 org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NullPointerException
at com.example.WebScrapper.itemScraper.getAllPages(ExampleScraper.java:37)
at com.example.Application.main(Application.java:17)
... 5 more

因此,尽管我的Hibernate Config类在设置会话工厂时没有出现任何错误,但经过一些额外的测试,发现空指针是因为数据库连接不存在。奇怪的是,我只是在尝试发送到数据库时才收到这个错误,而不是在启动时,它应该创建连接。

最新更新