0

I am trying to make an android app that when a button is clicked goes into the phones' directory and can open a file. Here is the code I have so far:

package com.open1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.view.View.OnClickListener;



public class MainActivity extends Activity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        Button button = (Button) findViewById(R.id.button1);



        button.setOnClickListener(new OnClickListener() {



            @Override

            public void onClick(View view) {

                Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();

             // get the files directory
                File lister = this.getFilesDir();

                FileInputStream inputStream = null;

                byte[] bytes = new byte[500];

                int fileIdx = -1;

                for (String list : lister.list()){
                    fileIdx++;
                    if(list.endsWith("ddd")){
                        File file = lister.listFiles()[fileIdx];
                        try {
                           inputStream = new FileInputStream(file);
                           bytes = new byte[inputStream.available()];
                           inputStream.read(bytes);
                        } catch (FileNotFoundException e) {
                           e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();}
                        finally {
                            if (inputStream != null) {
                               try {
                                   inputStream.close();
                               } catch (IOException e) {
                                   e.printStackTrace();
                               }
                            }
                        }
                    }
                }

            }

            private File getFilesDir() {
                // TODO Auto-generated method stub
                return null;
            }



        });

        return true;

    }



}

I can see the errors but can't make sense of how to fix them. Here is the logcat:

08-22 12:54:34.447: E/Trace(22072): error opening trace file: No such file or directory (2)
08-22 12:54:34.778: D/libEGL(22072): loaded /system/lib/egl/libEGL_mali.so
08-22 12:54:34.778: D/libEGL(22072): loaded /system/lib/egl/libGLESv1_CM_mali.so
08-22 12:54:34.788: D/libEGL(22072): loaded /system/lib/egl/libGLESv2_mali.so
08-22 12:54:34.828: D/OpenGLRenderer(22072): Enabling debug mode 0
08-22 13:01:03.667: E/Trace(22765): error opening trace file: No such file or directory (2)
08-22 13:01:03.817: D/libEGL(22765): loaded /system/lib/egl/libEGL_mali.so
08-22 13:01:03.837: D/libEGL(22765): loaded /system/lib/egl/libGLESv1_CM_mali.so
08-22 13:01:03.837: D/libEGL(22765): loaded /system/lib/egl/libGLESv2_mali.so
08-22 13:01:03.887: D/OpenGLRenderer(22765): Enabling debug mode 0
08-22 13:01:05.699: D/AndroidRuntime(22765): Shutting down VM
08-22 13:01:05.699: W/dalvikvm(22765): threadid=1: thread exiting with uncaught exception (group=0x417de318)
08-22 13:01:05.709: E/AndroidRuntime(22765): FATAL EXCEPTION: main
08-22 13:01:05.709: E/AndroidRuntime(22765): java.lang.NullPointerException
08-22 13:01:05.709: E/AndroidRuntime(22765):    at com.open1.MainActivity$1.onClick(MainActivity.java:65)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at android.view.View.performClick(View.java:4103)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at android.view.View$PerformClick.run(View.java:17117)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at android.os.Handler.handleCallback(Handler.java:615)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at android.os.Looper.loop(Looper.java:137)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at android.app.ActivityThread.main(ActivityThread.java:4744)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-22 13:01:05.709: E/AndroidRuntime(22765):    at dalvik.system.NativeStart.main(Native Method)
08-22 13:01:08.722: I/Process(22765): Sending signal. PID: 22765 SIG: 9
mark16
  • 1
  • 1

1 Answers1

2

Your method

        private File getFilesDir() {
            // TODO Auto-generated method stub
            return null;
        }

returns null whose return value is then used here:

            File lister = this.getFilesDir();
            ...
>>>>        for (String list : lister.list()){

You are obviously referencing an object (here: lister) that has been initialized with null.

Care to use a debugger and/or working on your TODO comments?

Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127