java - return array from method

71,286

Solution 1

One more thing everyone forgot to mention

String[] data_array = new String[999];
for (int x = 0; x <= 999; x++){}

will throw an ArrayIndexOutOfBoundsException. Possible solution

String[] data_array = new String[999];
for (int x = 0; x < 999; x++){}

Solution 2

You have defined your variable inside the while loop i.e. it is not visible to the return statement. Plus, you have defined your method as static void, meaning no return value is expected. Use static String [] instead.

Solution 3

Your declaration of the String[] is not in the same scope as the return statement.

You need to declare it in the beginning of the scope.

And you need to change the header of the function to:

public static String[] get_data()

Solution 4

Since its not accessible outside the block of while loop, compile is complaining about the same. Try this:

public static String[] get_data()
{
conn = getInstance();
String[] data_array = null;
if(conn != null)
{
    Statement query;
    try 
    {
        query = conn.createStatement();

        String sql = "SELECT data_x FROM table_x";
        ResultSet result = query.executeQuery(sql);

        while (result.next()) 
        {
            String data_x = result.getString("data_x");
            data_array = new String[999];
            for (int x = 0; x <= 999; x++)
            {
                data_array[x] = data_x;
            }
        }
    } 
    catch (SQLException e) 
    {
        e.printStackTrace();
    }
}
return data_array;
}

Solution 5

There are two things to fix here:

public static void get_data()

This method is declared to return nothing. Change it to:

public static String[] get_data()

Your variable String[] data_array is declared in the while loop, therefore it is only known there. Your return statement is outside that loop, so it doesn't have access to it.

Move the variable outside the loop:

String sql = "SELECT data_x FROM table_x";
ResultSet result = query.executeQuery(sql);
String[] data_array = new String[999];

Mind that you need to move the declaration and the initialization outside the while loop, or you will overwrite the previously stored data of that array by initializing it again. And also mind, that your for loop will overwrite the current data anyway ... you should think about storing the row data in another array so, or it will be lost.

Share:
71,286
user3683881
Author by

user3683881

Updated on February 10, 2020

Comments

  • user3683881
    user3683881 about 4 years

    I created this snippet:

    public static String[] get_data()
    {
        conn = getInstance();
        String[] data_array = null;
        if(conn != null)
        {
            Statement query;
            try 
            {
                query = conn.createStatement();
    
                String sql = "SELECT data_x FROM table_x";
                ResultSet result = query.executeQuery(sql);
    
                result.next();
                int count = result.getInt("data_x");
                result.close();
    
                data_x_array = new String[count];
    
                for (int x = 1; x <= count; x++)
                {
                    String data_x = result.getString(x);
                    data_x_array[x] = data_x;
                }
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
            }
        }
        return data_x_array;
    }
    

    I just created a class, where data from the database is collected in an array.

    Now i just want to return the array from this method.

    But what i get is:

    data_array cannot be resolved to a variable
    

    Anybody could help me ?

    Greetings!

    UPDATE:

    i changed snippet to:

    public static String[] get_data()
    {
        conn = getInstance();
        String[] data_array = null;
        if(conn != null)
        {
            Statement query;
            try 
            {
                query = conn.createStatement();
    
                String sql = "SELECT data_x FROM table_x";
                ResultSet result = query.executeQuery(sql);
    
                result.next();
                int count = result.getInt("data_x");
                result.close();
    
                data_array = new String[count];
    
                for (int x = 1; x <= count; x++)
                {
                    String data_x = result.getString(x);
                    data_x_array[x] = data_x;
                }
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
            }
        }
        return data_x_array;
    }
    

    When i compile just:

    Invalid value for getInt() - 'value_in_table'
    

    Anybody know this?

    Greetings!