简体   繁体   中英

NullPointerException when trying to connect to MySQL

Please help me to identify why I always got this error :

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

Note: DatebaseHelper is a class which is using JDBC to connect to 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 is a member variable, hence initialized to null . In main , you create a TaskDAO , and immediately use its help member - which is null . You must initialize help with some valid DatabaseHelper .

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

DatabaseHelper help = new DatabaseHelper();

You are calling taskDAO.help.executeQuery(query);
But till there DatabaseHelper help; is null because the reference variable is by default is null in java. So fist define DatabaseHelper help

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

and then call

taskDAO.help.executeQuery(query);

A simple code to read from MySQL database is given below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM