Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: file.getName() error
    primarykey
    data
    text
    <p>I'm developing an activity in order to explore the files included in the Tablet, where the user also can select one of them and open it. However when i retrieve the file name it says that there is a NPE. How could I retrieve the file name in order to open it later? Here is my code: File Explorer: </p> <pre><code>public class MainActivity extends ListActivity { private List&lt;String&gt; item = null; private List&lt;String&gt; path = null; private String root; private TextView myPath; static String A; static File file; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myPath = (TextView)findViewById(R.id.path); root = Environment.getExternalStorageDirectory().getPath(); getDir(root); } private void getDir(String dirPath) { myPath.setText("Location: " + dirPath); item = new ArrayList&lt;String&gt;(); path = new ArrayList&lt;String&gt;(); File f = new File(dirPath); File[] files = f.listFiles(); if(!dirPath.equals(root)) { item.add(root); path.add(root); item.add("../"); path.add(f.getParent()); } for(int i=0; i &lt; files.length; i++) { File file = files[i]; if(!file.isHidden() &amp;&amp; file.canRead()){ path.add(file.getPath()); if(file.isDirectory()){ item.add(file.getName() + "/"); }else{ item.add(file.getName()); } } } ArrayAdapter&lt;String&gt; fileList = new ArrayAdapter&lt;String&gt;(this, R.layout.row, item); setListAdapter(fileList); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub File file = new File(path.get(position)); if (file.isDirectory()) { if(file.canRead()){ getDir(path.get(position)); }else{ new AlertDialog.Builder(this) .setIcon(R.drawable.ic_launcher) .setTitle("[" + file.getName() + "] folder can't be read!") .setPositiveButton("OK", null).show(); } }else { file.getName(); XMLPullParserActivity.Parsear(this); Intent i = new Intent(MainActivity.this, Correccion.class); startActivity(i); } } public static String getA(){ A = XMLPullParserHandler.getA(); return A; } public static String fileName(){ return file.getName(); } } </code></pre> <p>Open the Selected File in the Activity:</p> <pre><code>public class XMLPullParserActivity { ListView listView; public static void Parsear(Context context){ List&lt;Puntuacion&gt; puntuacion = null; try { XMLPullParserHandler parser = new XMLPullParserHandler(); puntuacion = parser.parse(context.getAssets().open(MainActivity.fileName())); } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p>LogCat:</p> <pre><code>03-22 09:57:44.296: W/ActivityThread(4943): Application com.example.androidexplorer can be debugged on port 8100... 03-22 09:57:44.398: D/AbsListView(4943): Get MotionRecognitionManager 03-22 09:57:44.507: D/libEGL(4943): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so 03-22 09:57:44.523: D/libEGL(4943): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so 03-22 09:57:44.523: D/libEGL(4943): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so 03-22 09:57:44.640: D/OpenGLRenderer(4943): Enabling debug mode 0 03-22 09:58:02.656: D/AndroidRuntime(4943): Shutting down VM 03-22 09:58:02.656: W/dalvikvm(4943): threadid=1: thread exiting with uncaught exception (group=0x410a42a0) 03-22 09:58:02.679: E/AndroidRuntime(4943): FATAL EXCEPTION: main 03-22 09:58:02.679: E/AndroidRuntime(4943): java.lang.NullPointerException 03-22 09:58:02.679: E/AndroidRuntime(4943): at com.example.androidexplorer.MainActivity.fileName(MainActivity.java:104) 03-22 09:58:02.679: E/AndroidRuntime(4943): at com.example.androidexplorer.XMLPullParserActivity.Parsear(XMLPullParserActivity.java:25) 03-22 09:58:02.679: E/AndroidRuntime(4943): at com.example.androidexplorer.MainActivity.onListItemClick(MainActivity.java:90) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.app.ListActivity$2.onItemClick(ListActivity.java:319) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.widget.AdapterView.performItemClick(AdapterView.java:298) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.widget.AbsListView.performItemClick(AbsListView.java:1280) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3067) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.widget.AbsListView$1.run(AbsListView.java:3968) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.os.Handler.handleCallback(Handler.java:615) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.os.Handler.dispatchMessage(Handler.java:92) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.os.Looper.loop(Looper.java:137) 03-22 09:58:02.679: E/AndroidRuntime(4943): at android.app.ActivityThread.main(ActivityThread.java:4895) 03-22 09:58:02.679: E/AndroidRuntime(4943): at java.lang.reflect.Method.invokeNative(Native Method) 03-22 09:58:02.679: E/AndroidRuntime(4943): at java.lang.reflect.Method.invoke(Method.java:511) 03-22 09:58:02.679: E/AndroidRuntime(4943): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 03-22 09:58:02.679: E/AndroidRuntime(4943): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 03-22 09:58:02.679: E/AndroidRuntime(4943): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload