尝试使用jdbc时java.lang.NullPointerException

时间:2014-11-08 15:41:34

标签: java jdbc nullpointerexception

我正在研究的功能似乎存在问题。我使用JDBC,代码如下:

public String[] getAllFoodOrders() throws Exception{
            String[] foodordersstringarray = null;
            byte i = 0;

            Statement myStmt = null;
            ResultSet myRs = null;

            try {
                myStmt = myConn.createStatement();
                myRs = myStmt.executeQuery("SELECT description FROM orders,menu WHERE code = menu_CODE");

                while (myRs.next()) {
                    foodordersstringarray[i++] = myRs.getString("description");
                }
            }
                finally{
                        close(myStmt, myRs);
                        }

            return foodordersstringarray;
        }

在行的while语句中抛出异常:

“foodordersstringarray [i ++] = myRs.getString(”description“);”

包含上述方法的整个类的代码如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MySQLDatabaseManagement;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author localhost
 */
public class AdminExportToPDFOrXMLSQLManagement {
        private final Connection myConn;

    public AdminExportToPDFOrXMLSQLManagement() throws SQLException,FileNotFoundException,IOException{

            //partea cu conexiunea tabelului
            myConn = ConnectToDatabase.getConnection();
            //partea cu conexiunea la tabel
    }
        public String[] getAllFoodOrders() throws Exception{
            String[] foodordersstringarray = null;
            byte i = 0;

            Statement myStmt = null;
            ResultSet myRs = null;

            try {
                myStmt = myConn.createStatement();
                myRs = myStmt.executeQuery("SELECT description FROM orders,menu WHERE code = menu_CODE");

                while (myRs.next()) {
                    foodordersstringarray[i++] = myRs.getString("description");
                }
            }
                finally{
                        close(myStmt, myRs);
                        }

            return foodordersstringarray;
        }

    private static void close(Connection myConn, Statement myStmt, ResultSet myRs) throws SQLException {

        if (myRs != null) {
            myRs.close();
        }
        if (myConn != null) {
            myConn.close();
        }
    }

    private void close(Statement myStmt, ResultSet myRs) throws SQLException {
        close(null, myStmt, myRs);      
    }
}

堆栈跟踪如下:

java.lang.NullPointerException
    at MySQLDatabaseManagement.AdminExportToPDFOrXMLSQLManagement.getAllFoodOrders(AdminExportToPDFOrXMLSQLManagement.java:40)
    at xml.MySAXWriter.<init>(MySAXWriter.java:26)
    at Listeners.AdminExportToPDFOrXMListener.ExportToXML(AdminExportToPDFOrXMListener.java:35)
    at Listeners.AdminExportToPDFOrXMListener.actionPerformed(AdminExportToPDFOrXMListener.java:54)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:702)
    at java.awt.EventQueue$3.run(EventQueue.java:696)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:724)
    at java.awt.EventQueue$4.run(EventQueue.java:722)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

你能说出我做错了什么吗? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

数组foodordersstringarray未初始化。它应该以一定的大小创建。如果您不知道要指定的大小,请将结果填入List,然后将List转换为数组:

List<String> foodorderList = new ArrayList<String>();
...

try {
     myStmt = myConn.createStatement();
     myRs = myStmt.executeQuery("SELECT description FROM orders,menu WHERE code = menu_CODE");

     while (myRs.next()) {
         foodorderList.add(myRs.getString("description"));
     }
}
finally{
     close(myStmt, myRs);
}
return foodorderList.toArray(new String[1]);

答案 1 :(得分:0)

You String数组为null,如下所示,因此空指针异常。

String[] foodordersstringarray = null;

如果您确定我要说1000或更少的记录,请使用它:

String[] foodordersstringarray = new String[1000];

这里有记忆浪费的可能性。我建议你在这里使用List代替下面的数组:

List<String> foodOrderList = new LinkedList<String>();

而不是:

foodordersstringarray[i++] = myRs.getString("description");

待办事项

foodOrderList.add(myRs.getString("description"));

在方法结束时你可以做到

return (String[]) foodOrderList.toArray();//will return Object[]