In my Main Activity, I have this part which contains the filepath and the filename of the choosen file.
public class MainActivity extends Activity implements OnClickListener{
private static File selectedFile;
public static File Filename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case REQUEST_PICK_FILE:
if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {
selectedFile = new File
(data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
filePath.setText(selectedFile.getPath());
}
break;
}
}
} //onActivityResult
//and i added this so that I will be able to save the filepath and access it from my CoordinatesXmlParser.java (a non-activity class)
public void Filename () {
String filename = selectedFile.toString();
//return selectedFile;
}
}
And then on my CoordinatesXmlParser.java, I have this part:
File file = MainActivity.Filename;
file.toString();
//String file = Environment.getExternalStorageDirectory() + "/XML/coordinates.xml";
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
FileInputStream fis = new FileInputStream(file);
parser.setInput(new InputStreamReader(fis));
Problem: It is trowing a NullPointException. How would I be able to pass the filepath from the MainActivity.java to the CoordinatesXmlParser.java? TIA
It returns Null because in any moment you have assigned any value to the variable Filename (which by the way, goes against the patterns: variables should not begin with upper case letters).
public class MainActivity extends Activity implements OnClickListener{
private static File selectedFile;
public static File Filename;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case REQUEST_PICK_FILE:
if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {
selectedFile = new File
(data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
filePath.setText(selectedFile.getPath());
}
break;
}
}
} //onActivityResult
//and i added this so that I will be able to save the filepath and access it from my CoordinatesXmlParser.java (a non-activity class)
public static String getFilename () {
return selectedFile.toString();
}
}
And then
File file = MainActivity.getFilename();
file.toString();
Also, it is important to notice that this is not a good idea at all. You should only call getFilename() after you are sure that "onActivityResult" has already been called, otherwise it will always raise a NullPointerException.