0

请帮助我确定为什么我总是收到此错误:

 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);
    }

}
4

4 回答 4

3

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

于 2012-07-02T06:49:35.883 回答
2

help is null ,so NPE,忘记初始化了。

DatabaseHelper help = new DatabaseHelper();
于 2012-07-02T06:50:51.063 回答
0

下面给出了从 MySQL 数据库读取的简单代码

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;




public class NewClass {

public static void main(String[] args) {


    Connection conn = null;

    String url = "jdbc:mysql://192.168.100.100:3306/";
    String dbName = "databaseName";
    Statement stmt = null;
    ResultSet result = null;
    String driver = "com.mysql.jdbc.Driver";
    String databaseUserName = "admin";
    String databasePassword = "root";
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url + dbName, databaseUserName, databasePassword);
        stmt = conn.createStatement();
        result = null;
        String password,username;
        result = stmt.executeQuery("select * from userTable where username ='user1' ");
        if(!result.isBeforeFirst()){
            System.out.println("resultset contin no rows");
        }
        while (result.next()) {

            username=result.getString("username");
            password = result.getString("password");
            System.out.println(username+"  "+password);

        }
        conn.close();
    } catch (Exception e) {      
        e.printStackTrace();
    }
}
}
于 2012-07-02T06:57:52.980 回答
0

您正在调用taskDAO.help.executeQuery(query);
But until there DatabaseHelper help;is null 因为引用变量在 java 中默认为 null。所以拳头定义DatabaseHelper help

DatabaseHelper help = new DatabaseHelper();//or whatever create DatabaseHelper 

然后打电话

taskDAO.help.executeQuery(query);
于 2012-07-02T06:53:00.190 回答