Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:13:53+00:00 2026-06-10T03:13:53+00:00

I am trying use a from a multi-dimensional array that I create in another

  • 0

I am trying use a from a multi-dimensional array that I create in another classes method. Below is my main method:

public class main {

public static void main(String[] args) throws Exception {

    sql test = new sql();
    String[][] test2 = test.getDb();
    System.out.print(test2[0][0]);
}

Now here is the class that returns an multi-dimensional array.

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JTextField;

import com.mysql.jdbc.Statement;

public class sql {

java.sql.Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:8889/deliveryEarn";
String user = "root";
String password = "root";

ArrayList<String> sqlCol1 = new ArrayList<String>();
ArrayList<String> sqlCol2 = new ArrayList<String>();
ArrayList<String> sqlCol3 = new ArrayList<String>();
ArrayList<String> sqlCol4 = new ArrayList<String>();
ArrayList<String> sqlCol5 = new ArrayList<String>();
ArrayList<String> sqlCol6 = new ArrayList<String>();
ArrayList<String> sqlCol7 = new ArrayList<String>();
String sqlArray[][] = new String[7][7];

public sql() {
}

public String[][] getDb() {
    try {
         con = DriverManager.getConnection(url, user, password);
         pst = con.prepareStatement("select * from incomeCalc");
         rs = pst.executeQuery();

    while (rs.next()) {

        sqlCol1.add(rs.getString(1));
        int i1=0;
        for(String s: sqlCol1){
            sqlArray[i1++][0] = s;
        }

        sqlCol2.add(rs.getString(2));
        int i2=0;
        for(String s: sqlCol2){
            sqlArray[i2++][1] = s;
        }

        sqlCol3.add(rs.getString(3));
        int i3=0;
        for(String s: sqlCol3){
            sqlArray[i3++][2] = s;
        }

        sqlCol4.add(rs.getString(4));
        int i4=0;
        for(String s: sqlCol4){
            sqlArray[i4++][3] = s;
        }

        sqlCol5.add(rs.getString(5));
        int i5=0;
        for(String s: sqlCol5){
            sqlArray[i5++][4] = s;
        }

        sqlCol6.add(rs.getString(6));
        int i6=0;
        for(String s: sqlCol6){
            sqlArray[i6++][5] = s;
        }
        sqlCol7.add(rs.getString(7));
        int i7=0;
        for(String s: sqlCol7){
            sqlArray[i7++][6] = s;
        }
    }
    }

catch( Exception E ) { 
    System.out.println( E.getMessage() );   
}
    return sqlArray;
}
}

Here is the screenshot of the MySQL database.
enter image description here

Edit: It appears I wasn’t clear with my question. I apologize. I am getting a runtime error at this line:

System.out.print(test2[0][0]);

What am I doing wrong? Also, for correct OOP, is it better to use a constructor or a method to pull from or input to a database? THis is my first program so sorry if it seems trivial.

Edit2: Here is the error:

Exception in thread “main” java.lang.NullPointerException
at main.main(main.java:17)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:13:55+00:00Added an answer on June 10, 2026 at 3:13 am

    As to why you’ve got an error, it would be nicer to know the error, however…

    Personally, I’d drop the contents of the result set into a “Data Object”…

    public class Income {
        // Column decelerations...
    
        private long id;
        private int tips;
        private int hours;
        private int gas;
        private double hourly;
        private double other;
        private double other2;
    
        public int getGas() {
            return gas;
        }
    
        public double getHourly() {
            return hourly;
        }
    
        public int getHours() {
            return hours;
        }
    
        public long getId() {
            return id;
        }
    
        public double getOther() {
            return other;
        }
    
        public double getOther2() {
            return other2;
        }
    
        public int getTips() {
            return tips;
        }
    
        public void setGas(int gas) {
            this.gas = gas;
        }
    
        public void setHourly(double hourly) {
            this.hourly = hourly;
        }
    
        public void setHours(int hours) {
            this.hours = hours;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        public void setOther(double other) {
            this.other = other;
        }
    
        public void setOther2(double other2) {
            this.other2 = other2;
        }
    
        public void setTips(int tips) {
            this.tips = tips;
        }
    }
    

    Then when you load it you could do something like…

    public Income[] getIncome() {
    
        // Call database...
    
        List<Income> data = new ArrayList<Income>(25);
    
        while (rs.next()) {
    
            Income income = new Income();
    
            income.setID(rs.getInt(1)));
            income.setTips(rs.getInt(2)));
            income.setHours(rs.getInt(3)));
            income.setGas(rs.getInt(4)));
            income.setHourly(rs.getDouble(5)));
            income.setOther(rs.getDouble(6)));
            income.setOther2(rs.getDouble(7)));
    
            data.add(income);
    
        }
    
        return data.toArray(new Income[data.size()]);
    
    }
    

    The you could do things like this…

    sql test = new sql();
    Income[] incomes = test.getIncome();
    
    System.out.println(incomes[0].getID());
    

    Isn’t that easier to read 😛

    Your attempt to use a Factory is probably the best idea. It comes down to a matter of management as to weather you maintain a single instance (Singlton) or allow multiple instances of this Factory to be created. Personally, I prefer to use Singltons in this case where I can, it allows a centralised place to perform operations (saving changes, creating new objects, listing, deleting) and helps manage the resources involved. IMHO

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a COM object that I'm trying to use from C++ (not .NET),
I am trying to use a result from one query as input for another
I'm trying to create an array to use for a curl_multi_exec, but I can't
I am having issues assinging values to a multi-dimensional array. I am trying to
I am trying to create a multi-dimensional NetCDF file using the R package ncdf
I currently construct a multi-dim array from my input. like so: (example) <form method=post
When I use Python to model a 3-Dimensional matrix, I first make a multi-array
I am trying to use datetimepicker from: http://trentrichardson.com/ functionality is working it seems, but
I am trying to use GLSMultipleLinearRegression (from apache commons-math package) for multiple linear regression.
I've been trying to use Robot from awt, to input some text on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.