在运行代码时我遇到了这个错误:
Exception in thread "Thread-5" java.lang.NullPointerException at
com.example.example1.service.RecordData.run(RecordData.java:87)
这是我的计划:
public class SearchParser {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/";
String dbName = "share";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "";
try {
List<RecordData> list = new ArrayList<RecordData>();
Connection conn = DriverManager.getConnection(url + dbName,
userName, password);
Statement stmt = conn.createStatement();
ResultSet mysqldata = stmt
.executeQuery("select * from product_links");
while (mysqldata.next()) {
RecordData data = new RecordData();
data.setId(mysqldata.getInt("id"));
data.setDomain_name(mysqldata.getString("domain_name"));
data.setCommon_link_name(mysqldata
.getString("common_link_name"));
data.setHead_element_pattern(mysqldata
.getString("head_element_pattern"));
data.setProduct_title_pattern(mysqldata
.getString("product_title_pattern"));
data.setProduct_link_pattern(mysqldata
.getString("product_link_pattern"));
data.setCurrent_price_pattern(mysqldata
.getString("current_price_pattern"));
data.setImage_src_pattern(mysqldata
.getString("image_src_pattern"));
data.setDecscription_pattern(mysqldata
.getString("decscription_pattern"));
list.add(data);
}
for (RecordData data : list) {
RecordData p = new RecordData();
p.start();
}
} catch (Exception e) {
System.out.print(e);
}
}
}
它基本上是一个从数据库中获取数据的程序。
public class RecordData extends Thread {
int id;
String domain_name, common_link_name, head_element_pattern, product_title_pattern, product_link_pattern, current_price_pattern, image_src_pattern,
decscription_pattern;
public RecordData data;
public RecordData() {
this.data = data;
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDomain_name() {
return domain_name;
}
public void setDomain_name(String domain_name) {
this.domain_name = domain_name;
}
public String getCommon_link_name() {
return common_link_name;
}
public void setCommon_link_name(String common_link_name) {
this.common_link_name = common_link_name;
}
public String getHead_element_pattern() {
return head_element_pattern;
}
public void setHead_element_pattern(String head_element_pattern) {
this.head_element_pattern = head_element_pattern;
}
public String getProduct_title_pattern() {
return product_title_pattern;
}
public void setProduct_title_pattern(String product_title_pattern) {
this.product_title_pattern = product_title_pattern;
}
public String getProduct_link_pattern() {
return product_link_pattern;
}
public void setProduct_link_pattern(String product_link_pattern) {
this.product_link_pattern = product_link_pattern;
}
public String getCurrent_price_pattern() {
return current_price_pattern;
}
public void setCurrent_price_pattern(String current_price_pattern) {
this.current_price_pattern = current_price_pattern;
}
public String getImage_src_pattern() {
return image_src_pattern;
}
public void setImage_src_pattern(String image_src_pattern) {
this.image_src_pattern = image_src_pattern;
}
public String getDecscription_pattern() {
return decscription_pattern;
}
public void setDecscription_pattern(String decscription_pattern) {
this.decscription_pattern = decscription_pattern;
}
public void run() {
System.out.println(data.domain_name);
}
}
答案 0 :(得分:5)
因为这个
public RecordData() {
this.data = data;
}
绝对没有。您正在为自己分配一个字段。由于它从null
开始,它也会以null
结束。
我不知道你想要实现什么,但你可能想为data
分配一些其他值。
public RecordData(RecordData data) {
this.data = data;
}