0

I'm adding elements from database to this stdData arrayList. I checked it after adding that they are present in stdData arrayList, but when I call this stdData arrayList from another method as you see here it returns null.

package school;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ReadingStd {
    String conURL = "jdbc:sqlserver://localhost:1433;" + "databaseName=schoolDB;user=fas;password=24071982";
    List<String> stdData = new ArrayList<String>();
    String std = null;

    public void readStd(String stdId) {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(conURL);
            System.out.println("faisal:");
            String sqlQuery = "select * from schoolDB.dbo.addstudent where sid = '" + stdId + "'";
            Statement stmt = con.createStatement();
            System.out.println("faisal11:");
            ResultSet rs = stmt.executeQuery(sqlQuery);

            while (rs.next()) {
                stdData.add(0, rs.getString("sid"));
                stdData.add(1, rs.getString("name"));
                stdData.add(2, rs.getString("fname"));
                stdData.add(3, rs.getString("dob"));
                stdData.add(4, rs.getString("classs"));
                stdData.add(5, rs.getString("occupation_father"));
                stdData.add(6, rs.getString("edu_father"));
                stdData.add(7, rs.getString("email"));
                stdData.add(8, rs.getString("father_office_address"));
                stdData.add(9, rs.getString("cont1"));
                stdData.add(10, rs.getString("occupation_mother"));
                stdData.add(11, rs.getString("mother_office_address"));
                stdData.add(12, rs.getString("mname"));
                stdData.add(13, rs.getString("cont2"));
                stdData.add(14, rs.getString("caddress"));
                stdData.add(15, rs.getString("paddress"));
                stdData.add(16, rs.getString("sex"));
                stdData.add(17, rs.getString("addate"));
                stdData.add(18, rs.getString("division"));
                stdData.add(19, rs.getString("division"));
            }
            System.out.println(stdData.size());
            con.close();
        }

        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public List < String > returnStd() {
        return stdData;
    }

    public void printstd() {
        System.out.println(stdData);
        System.out.println(stdData.size());
    }

    public static void main(String[] args) {
        new ReadingStd().readStd("czc");
        new ReadingStd().printstd();
    }
}
1
  • Was your question resolved? Commented Mar 28, 2019 at 14:14

2 Answers 2

2

The problem is here:

public static void main (String[] args) {
        new ReadingStd().readStd("czc");
        new ReadingStd().printstd();
    }

The list is a field in the object which you instantiate. But you instantiate two objects which both have their own field List<String> stdData and fill that list by calling the readStd() method. You call the readStd() method of the first object and afterwards you print the list of the other object which didn't fill its own list. This will fix that issue:

 public static void main (String[] args) {
            ReadingStd reader = new ReadingStd();
            reader.readStd("czc");
            reader.printStd();
        }

Now the object reader of type ReadingStd fills its list with the values and the same object prints these values.

Sign up to request clarification or add additional context in comments.

Comments

1

Your are Actually creating 2 new objects. which cause the problem . change

 public static void main (String[] args) {
        new ReadingStd().readStd("czc");
        new ReadingStd().printstd();
    }

to

public static void main (String[] args) {
       ReadingStd r= new ReadingStd();
        r.readStd("czc");
        r.printstd();
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.