2

It seems there's a problem with a function I'm working on. I use JDBC and the code is the following:

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

The exception is thrown within the while statement at the row:

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

The code of the entire class containing the above method is the following:

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

The stack trace is the following:

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)

Could you tell what was I doing wrong? Thank you in advance!

Andrei
  • 219
  • 2
  • 10

2 Answers2

2

The array foodordersstringarray is not initialized. It should be created with a certain size. If you don't know the size to specify, fill the results in a List and then convert the List to an array:

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]);
M A
  • 65,721
  • 13
  • 123
  • 159
0

You String array is null as below hence null pointer exception.

String[] foodordersstringarray = null;

If you are sure that i am going to get say 1000 or less record use it like:

String[] foodordersstringarray = new String[1000];

There are chances of memory wastage here. I would suggest you using List here instead of array like below:

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

and instead of:

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

Do

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

And at the end of the method you could do

return (String[]) foodOrderList.toArray();//will return Object[]
SMA
  • 33,915
  • 6
  • 43
  • 65