简体   繁体   中英

i am unable to store value of resultset into two dimensional String array

I am unable to get value of resultset into String[][]. it shows nullpointerexceptions at line 13.If i directly print value of rs.getString(0) and rs.getString(1), it works correctly

public void get_papers(String a_id)
{
 int aid=Integer.parseInt(a_id);
 String[][] s1=new String[2][];
   try
      {
        int i=0;
       Connection con=DriverManager.getConnection(url,"root","");
         Statement s=con.createStatement();
         ResultSet rs=s.executeQuery("select title,p_s_no from paper_record where                   a_id='"+aid+"'");
           while(rs.next())
             {
             s1[0][i]=(String)rs.getString(1);
              s1[1][i]=(String)rs.getString(2);
              i++;
             }

             }
             catch(SQLException se)
             {System.out.println(se);}
              //return(s1);
              System.out.println(s1[0][1]);
             System.out.println(s1[1][1]);
             }

You have not mentioned the number of columns when creating the array. You have written String s[][] = new String[2][] where the size of column is not mentioned. So Java cannot allocate the size of the array in memory. Hence whenever you try to add an element to array it shows NullPointer Exception
SOLUTION

  1. You can write a quer like SELECT COUNT(*) FROM ... and use it to get the size of the ResultSet and use that size to dynamically create the array.
  2. If you don't want to run any other queryas I mentioned above, your option is to use an ArrayList where you dont have to mention the size. Only thing you have to do is create a seperate class like below

    class Example{ private String data1,data2; public String getData1(){ return data1 ; } public void setData1(String data1){ this.data1 = data1 ; } //..... }

And then while getting data from ResultSet write

Example exp = new Example();
exp.setData1(rs.getString(0));
exp.setData2(rs.getString(1));
list.add(exp);

An array constructor call like new String[2][] gives you an array of length 2 where both entries are null. You should use this:

String[][] s1=new String[aid][2];

However, it would be preferable to collect results in an ArrayList

List<String[2]> results = new ArrayList<>();

and add results like this:

results.add( new String[]{ (String)rs.getString(1), (String)rs.getString(2) } );

Allocating an array based on worse case guestimates is always the second best idea.

On the declaration line of array ie instead of this line String[][] s1=new String[2][]; you can make declaration like this String[][] s1=new String[2][10];
It will work fine.
Just check s1[0][i]=(String)rs.getString(1); s1[1][i]=(String)rs.getString(2);
for your correct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM