简体   繁体   中英

First Element in TwoDimensional Array goes to null?

I am using this code to insert the details in to TwoDimensional Array. But while retrieving the data from the array the first element value changes to null.

    Cursor consultancy = db.getConsultancy(this);
        if(consultancy!=null)
        {
            consultancy.moveToFirst(); 
            consultancy.moveToNext();  
            consultancynames = new String[(int) db.getConsultancyCount()-1];
            for(int i=0;i<db.getConsultancyCount()-1;i++)
            { 
                consultancynames[i] = consultancy.getString(2);  
                int consultantid = Integer.parseInt(consultancy.getString(consultancy.getColumnIndex(TimeAndExpensesLocalDB.CT_CONSULTANCYID))); 
                Cursor project_namecur = db.getProjectCode(this, consultantid);
                if(project_namecur!=null)
                {
                    project_namecur.moveToFirst();  
                    projectname = new String[(int) db.getConsultancyCount()][project_namecur.getCount()];
                    for(int j=0;j<project_namecur.getCount();j++)
                    {  
                        projectname[i][j] = project_namecur.getString(3);    
                        project_namecur.moveToNext();  
                    } 
                }
                consultancy.moveToNext();
            }  

        }  

        //... Print array  
        for (int i =0; i < consultancynames.length; i++) {
        for (int j = 0; j < projectname.length; j++) {
        System.out.print(" " + projectname[i][j]);
        }
        System.out.println("");
        }

Output

       05-25 12:58:22.700: I/System.out(2373):  null null null
       05-25 12:58:22.700: I/System.out(2373):  Other-1 Other-2 Other-3

I am not sure what is happening.

Thanks for your help guys..

You're creating a new array on each iteration of the loop:

projectname = new String[(int) db.getConsultancyCount()][project_namecur.getCount()];

So on the first iteration you're creating an array and filling in the first "row" of the array. On the second iteration you're creating a new array (which will default to having null elements) and filling in the second row.

I suspect you need to allocate the "outer" array once before the loop, then allocate the "inner" array based on how many project names there are for that consultant:

 // Note: more idiomatic names would be consultancyNames and
 // projectNames. It's also unclear why you're subtracting one from the count...
 consultancynames = new String[(int) db.getConsultancyCount() - 1];
 projectnames = new String[consultancynames.length][];
 for (int i = 0;i< consultancenames.length; i++) {
     ...
     projectnames[i] = new String[project_namecur.getCount())];
     ...
 }

Then you'll need to change your display code too, eg to

for (int i =0; i < projectname.length; i++) {
    for (int j = 0; j < projectname[i].length; j++) {
        System.out.print(" " + projectname[i][j]);
    }
    System.out.println("");
}

Note that you can't do the following:

projectname = new String[(int) db.getConsultancyCount()][project_namecur.getCount()];
for(int j=0;j<project_namecur.getCount();j++)
{  
    projectname[i][j] = project_namecur.getString(3);    
    project_namecur.moveToNext();  
}

Here's why:

After the first line projectname will be an array of arrays.

Since the arrays are object references you have an array of object references.

Since the default value of an object reference is null , you'll have an array of null elements.

This means you can't do

projectname[i][j] = project_namecur.getString(3);  

since it corresponds to

String[] row = projectname[i];
// row == null !
row[j] = project_namecur.getString(3);

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