There is a class that connects to the database. It has a read method that immediately transfers data to the collection:

public class Basa_Dannyh { public static ResultSet resSet; public static void ReadDB() throws ClassNotFoundException, SQLException { resSet = statmt.executeQuery("SELECT * FROM nachaltest"); while(resSet.next()) { int id = resSet.getInt("id"); String quest = resSet.getString("quest"); String ansver1 = resSet.getString("ansver1"); String ansver2 = resSet.getString("ansver2"); String ansver3 = resSet.getString("ansver3"); String true_ansver = resSet.getString("true_ansver"); ArrayList<String> test_collection = new ArrayList<String>(); test_collection.add(quest); test_collection.add(ansver1); test_collection.add(ansver2); test_collection.add(ansver3); test_collection.add(true_ansver); for (int i = 0; i<test_collection.size(); i++) { System.out.println(test_collection.get(i)); } } } } 

Connecting to this method in another class happens like this:

 Basa_Dannyh.ReadDB(); 

I need to transfer the collection to another class. How to do it? Through the creation of a class instance:

 Basa_Dannyh bd = new Basa_Dannyh(); 

Does not work. I try to tighten up in the standard way:

 Basa_Dannyh bd = new Basa_Dannyh(); System.out.println(bd.test_collection.get(1)); //Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ 

Mistake. Does not see test_collection .

    2 answers 2

    You have a Basa_Dannyh.ReadDB() static method that returns nothing.

     Basa_Dannyh bd = new Basa_Dannyh(); System.out.println(bd.test_collection.get(1)); 

    And here you create a new class object, and try to access the test_collection collection (to the field), which you do not exist. Hence the error. Accordingly, you need to change the return value of the Basa_Dannyh.ReadDB() method:

     public static ArrayList<String> ReadDB() throws ClassNotFoundException, SQLException { ArrayList<String> test_collection = new ArrayList<String>(); resSet = statmt.executeQuery("SELECT * FROM nachaltest"); while(resSet.next()) { int id = resSet.getInt("id"); String quest = resSet.getString("quest"); String ansver1 = resSet.getString("ansver1"); String ansver2 = resSet.getString("ansver2"); String ansver3 = resSet.getString("ansver3"); String true_ansver = resSet.getString("true_ansver"); test_collection.add(quest); test_collection.add(ansver1); test_collection.add(ansver2); test_collection.add(ansver3); test_collection.add(true_ansver); for (int i = 0; i<test_collection.size(); i++) { System.out.println(test_collection.get(i)); } } return test_collection; } 

    And use this static method:

     ArrayList<String> collection = Basa_Dannyh.ReadDB(); 

      Here try to return instead of void - ArrayList<String>

       public class Basa_Dannyh { public static ResultSet resSet; //readDB исправил с малСнькой Π±ΡƒΠΊΠ²Ρ‹ public static ArrayList<String> readDB() throws ClassNotFoundException, SQLException { resSet = statmt.executeQuery("SELECT * FROM nachaltest"); ArrayList<String> test_collection = new ArrayList<String>();//здСсь ΠΌΡ‹ Π²Ρ‹Π²Π΅Π»ΠΈ объявлСниС массива ΠΈΠ· Ρ†ΠΈΠΊΠ»Π° while(resSet.next()) { int id = resSet.getInt("id"); String quest = resSet.getString("quest"); String ansver1 = resSet.getString("ansver1"); String ansver2 = resSet.getString("ansver2"); String ansver3 = resSet.getString("ansver3"); String true_ansver = resSet.getString("true_ansver"); test_collection.add(quest); test_collection.add(ansver1); test_collection.add(ansver2); test_collection.add(ansver3); test_collection.add(true_ansver); for (int i = 0; i<test_collection.size(); i++) { System.out.println(test_collection.get(i)); } } return test_collection;//здСсь Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ } } 

      You can get it

       <!!-Basa_Dannyh bd = new Basa_Dannyh();-!!>//Π²ΠΈΠ½ΠΎΠ²Π°Ρ‚, Π΄Π΅ΠΉΡΡ‚Π²ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ Ρƒ вас ΠΌΠ΅Ρ‚ΠΎΠ΄ static, ΠΌΠΎΠΆΠ½ΠΎ Π½Π΅ ΡΠΎΠ·Π΄Π°Π²Π°Ρ‚ΡŒ Π½ΠΎΠ²Ρ‹ΠΉ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚. ArrayList<String> newArrayList = Basa_Dannyh.readDB();//Ρ‚ΠΎΠ³Π΄Π° Ρ‚Π°ΠΊ