gpt4 book ai didi

java - 尝试连接到MySQL时出现NullPointerException

转载 作者:行者123 更新时间:2023-12-01 17:30:38 25 4
gpt4 key购买 nike

请帮助我确定为什么总是出现此错误:

 java.lang.NullPointerException
at TaskDAO.TaskDAO.main(TaskDAO.java:34)


注意: DatebaseHelper是使用JDBC连接到MySQL的类。

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import TaskManagement.*;

public class TaskDAO {

DatabaseHelper help;

public static void main(String[] args) throws InstantiationException,
IllegalAccessException, ClassNotFoundException, SQLException {

TaskDAO taskDAO = new TaskDAO();

int taskID = 6;
String subject = "lau cua";
Status status = Status.Completeted;
Priority priority = Priority.High;
Float percentage = 4f;
String tableName = "tasks";
String startDate = "2012/11/11";
String dueDate = "2012/11/12";

String query = "INSERT INTO "
+ tableName
+ "(taskID,subject,status,priority,percentage,startDate,dueDate)"
+ " VALUES ('" + taskID + "'," + "'" + subject + "'," + "'"
+ status + "'," + "'" + priority + "'," + "'" + percentage
+ "'," + "'" + startDate + "'," + "'" + dueDate + "')";

taskDAO.help.executeQuery(query);
}

}

最佳答案

DatabaseHelper help是成员变量,因此被初始化为null。在main中,创建一个TaskDAO,并立即使用其help成员-null。您必须使用一些有效的help初始化DatabaseHelper

关于java - 尝试连接到MySQL时出现NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11288875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com