java.lang.NullPointerException编译jdbc时出错

时间:2013-10-08 10:33:49

标签: java jdbc

我正在尝试为我的gui编译我的jdbc,它的目标是从用户获取用户输入然后选择具有相应表的表(尚未实现)然后在该行imagepath列下获取string是该行的图像路径,并使用此字符串显示图像。到目前为止,当我运行我的代码时,我收到以下错误。

java.lang.NullPointerException
    at Assignment.DisplaySearch.Search(Search.java:276)
    at Assignment.Search$4.actionPerformed(Search.java:123)

按下以下按钮时会发生此错误。

ViewerButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    DisplaySearch.Search(jtfImagename1.getText(), jtfLocation1.getText(), jtfTimestamp1.getText());
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                return;   
            }
        });

JDBC -

 class DisplaySearch {
        // database URL                              

        static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
        static final String USERNAME = "root";
        static final String PASSWORD = "root";

        // launch the application
        public static void Search(String imageName, String DateStamp, String imageTag) throws SQLException {
            Connection connection = null; // manages connection
            Statement statement = null; // query statement
            ResultSet resultSet = null; // manages results

            // connect to database books and query database
            try {
                // establish connection to database
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

                // query database 
                PreparedStatement pt = connection.prepareStatement(
                        "SELECT imageName, DateStamp, imageTag FROM images WHERE imageName=? AND DateStamp=? AND imageTag=?");
                pt.setString(1, "image-name");
                pt.setString(2, "date-stamp"); // maybe .setDate(...)
                pt.setString(3, "image-tag");
                resultSet = pt.executeQuery();

                while (resultSet.next()) {
                    // Position the cursor 
                    imageName = resultSet.getString(0);
                    DateStamp = resultSet.getString(1);
                    imageTag = resultSet.getString(2);
                    System.out.println("datas : " + imageName + " " + DateStamp + " " + imageTag);
                    if (imageName != "" || imageName != null) {
                        String Name = resultSet.getString(imageName);
                        System.out.print("Image Name: " + Name);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageName='"
                                + imageName + "'");
                    }

                    if (DateStamp != null) {
                        String Date = resultSet.getString(DateStamp);
                        System.out.print(", Date: " + Date);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where DateStamp='"
                                + DateStamp + "'");
                    }

                    if (imageTag != "" || imageTag != null) {
                        String Tag = resultSet.getString(imageTag);
                        System.out.print(", Tag: " + Tag);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageTag='"
                                + imageTag + "'");
                    }
                }// end while
            } // end try
            catch (Exception e) {
                e.printStackTrace();
            } // end catch                                                     
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } // end try                                               
            catch (Exception exception) {
                exception.printStackTrace();
            } // end catch 
        } // end main

2 个答案:

答案 0 :(得分:3)

您正在使用PreparedStatement pt但关闭statement,这是空的。

答案 1 :(得分:0)

您正在关闭Statement对象,该对象指向null,其中inturn给出NullPointerException。

更改您的代码如下...

class DisplaySearch {
        // database URL                              

        static final String DATABASE_URL = "jdbc:mysql://localhost:3306/mysql";
        static final String USERNAME = "root";
        static final String PASSWORD = "root";

        // launch the application
        public static void Search(String imageName, String DateStamp, String imageTag) throws SQLException {
            Connection connection = null; // manages connection
            PreparedStatement  pt = null; // query statement
            ResultSet resultSet = null; // manages results

            // connect to database books and query database
            try {
                // establish connection to database
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);

    //            resultSet = statement.executeQuery(
    //                    "SELECT imageName, DateStamp, imageTag FROM images WHERE imageName='*' AND DateStamp='*' AND imageTag='*'");
    //

                // query database 
                pt = connection.prepareStatement(
                        "SELECT imageName, DateStamp, imageTag FROM images WHERE imageName=? AND DateStamp=? AND imageTag=?");
                pt.setString(1, "image-name");
                pt.setString(2, "date-stamp"); // maybe .setDate(...)
                pt.setString(3, "image-tag");
                resultSet = pt.executeQuery();

                while (resultSet.next()) {
                    // Position the cursor 
                    imageName = resultSet.getString(0);
                    DateStamp = resultSet.getString(1);
                    imageTag = resultSet.getString(2);
                    System.out.println("datas : " + imageName + " " + DateStamp + " " + imageTag);
                    if (imageName != "" || imageName != null) {
                        String Name = resultSet.getString(imageName);
                        System.out.print("Image Name: " + Name);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageName='"
                                + imageName + "'");
                    }

                    if (DateStamp != null) {
                        String Date = resultSet.getString(DateStamp);
                        System.out.print(", Date: " + Date);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where DateStamp='"
                                + DateStamp + "'");
                    }

                    if (imageTag != "" || imageTag != null) {
                        String Tag = resultSet.getString(imageTag);
                        System.out.print(", Tag: " + Tag);
                        resultSet = statement.executeQuery("SELECT imageName, DateStamp, imageTag FROM images where imageTag='"
                                + imageTag + "'");
                    }
                }// end while
            } // end try
            catch (Exception e) {
                e.printStackTrace();
            } // end catch                                                     
            try {
                resultSet.close();
                pt.close();
                connection.close();
            } // end try                                               
            catch (Exception exception) {
                exception.printStackTrace();
            } // end catch 
        } // end main`enter code here`