NullPointerException on Android

0

I have a problem listing the photos in the DCIM folder on Android. Does it give the loop error for , casting java.lang.NullPointerException ? What can it be?

See the code that takes the information from the folder:

public class MainActivity extends Activity  {

    private ListView listView;
    private List<String> minhaLista; 
    private File file;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //carrega o layout onde contem o ListView
        setContentView(R.layout.layout_arquivos);

        minhaLista = new ArrayList<String>();   

        String root_sd = Environment.getExternalStorageDirectory().toString();
        file = new File(root_sd + "/DCIM");       
        File list[] = file.listFiles();

        System.out.println(file);

        for( int i=0; i< list.length; i++)
        {
                minhaLista.add( list[i].getName() );
        }

        AdapterListView adapter = new AdapterListView(this, minhaLista);  
        listView = (ListView)findViewById(R.id.list);  
        listView.setAdapter(adapter);
    }
}

The adapter class:

public class AdapterListView extends BaseAdapter {

    private Context mContext;  
    private List<String> mArquivos; 

    public AdapterListView(Context context, List<String> arquivos) {
        // TODO Auto-generated constructor stub
        mContext = context;
        mArquivos = arquivos;
    }

    public int getCount() {  
      return mArquivos.size(); // Retorna o número de linhas  
    }  

    public String getItem(int position) {  
      return mArquivos.get(position); // Retorna o item daquela posição  
    }  

    public long getItemId(int position) {  
      return position;  
   }  

    // Classe responsável por guardar a referência da View  
    static class ViewHolder {  
        TextView hTextView;  
    } 

   public View getView(int position, View convertView, ViewGroup p) {  
      ViewHolder holder = null;  
      String item = this.getItem(position);  
      if (convertView == null) {  
         convertView = LayoutInflater.from(mContext).inflate(R.layout.item_escolher_pasta, p, false);  
         holder = new ViewHolder();  
         holder.hTextView = (TextView) convertView.findViewById(R.id.txtNomePastas);  
         convertView.setTag(holder);  
      } else {  
         holder = (ViewHolder)convertView.getTag();  
      }  

      holder.hTextView.setText(item);  

      return convertView;  
   }  

}
    
asked by anonymous 02.06.2015 / 16:25

2 answers

1

Felipe, see if it helps you

    //verifica se o SDCARD está no aparelho
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
    {
        Toast.makeText(this, "Erro, SDCARD Não Encontrado!", Toast.LENGTH_LONG).show();
    } else
    {
         //cria   
        file = new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM");
        file.mkdirs();
    }
    //verifica se existe o diretorio 
    if (file.isDirectory())
    {
         File list[] = file.listFiles();

        for (int i = 0; i < list.length; i++)
        {
             minhaLista.add( list[i].getName() );

        }

        }
    }
    
02.06.2015 / 16:50
0

Here is your loop for :

    File list[] = file.listFiles();

    System.out.println(file);

    for( int i=0; i< list.length; i++)
    {
            minhaLista.add( list[i].getName() );
    }

The NullPointerException could be cast to for only if list is null or if any of the list[i] elements is null .

Let's see a short snippet of javadoc from listFiles() :

  

If this abstract pathname does not denote the directory, then this method returns null .

Translating to Portuguese:

  

If this abstract pathname does not denote a directory, this method returns null .

In addition, the array returned by listFiles() , when the path exists, never contains null , so the cause of your problem is that your file reference does not match a existing directory. So Environment.getExternalStorageDirectory().toString() + "/DCIM" does not match an existing directory in your case.

    
02.06.2015 / 16:52