Nullpointerexception on Android

Asked

Viewed 109 times

0

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

See the code that I get 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 class Adapter:

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;  
   }  

}
  • 2

    http://answall.com/help/mcve

  • I moved the folder to SDCARD and still the problem persists in FOR.

  • Felipe when Voce does this 'String root_sd = Environment.getExternalStorageDirectory(). toString();' you get the path your sdcard has from the device if your images are inside the DCIM keep that way, but if there is no folder in your sdcard it will happen as @Victorstafusa said.

2 answers

1

Felipe, see if this 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() );

        }

        }
    }

0

Here is your bond 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 in the for only if list for null or if any of the elements list[i] for null.

Let’s see a little bit of the javadoc’s method listFiles():

If this Abstract pathname does not denote a directory, then this method Returns null.

Translating into Portuguese:

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

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

  • Victor creating the folder if it doesn’t exist would solve the @Elipe problem?

  • @Wellingtonavelino Maybe. But it may be that he is simply looking in the wrong place, and in this case he would be creating the wrong folder.

  • I get it, maybe what I posted isn’t useful to him.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.