I have an int/string(s) that is passed from one fragment to another. For each way I code it, I get a NPE at line 67 (see where 67 is below). I dont know If I am going about it in the correct way so please look thru my methods and include some code in your answer.
Thnx.
What passes the arguments:
. . .
int rawRes = R.raw.regulatory_list;//<--THIS IS THE RES I NEED TO PASS
args.putInt("KEY_RAW_RES", rawRes);
boolean isRawRes = true;
args.putBoolean("KEY_IS_RAW_RES", isRawRes);
ListViewFragment lvf = new ListViewFragment();
lcFT.replace(R.id.discriptionListContainer, lvf).commit();
lvf.setArguments(args);
. . .
ListViewFragment’s onActivityCreated:
. . .
private static final String KEY_URL = "KEY_URL";
private static final String KEY_IS_RAW_RES = "KEY_IS_RAW_RES";
private static final String KEY_RAW_RES = "KEY_RAW_RES";
. . .
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get the string to query from last Fragment and pass it to this
// Fragment
Bundle args = this.getArguments();
String url = args.getString(KEY_URL);
boolean rawRes = args.getBoolean(KEY_IS_RAW_RES);
int fileName = (Integer) null;//<--THIS IS LINE 67!!
fileName = args.getInt(KEY_RAW_RES);
this.runJsonFile(url, jsonSrc, fileName);
}
private void runJsonFile(String url, boolean rawRes, int fileName) {
if (rawRes == true) {
getFromRawRes(fileName);
} else {
getFromURL(url);
}
}
private void getFromRawRes(int fileName) {
// InputStream file = getResources().openRawResource(R.raw.regulatory_list);
InputStream file = getResources().openRawResource(fileName);
JSONParser jParser = new JSONParser();
JSONArray json = jParser.getJSONFromFile(file);
callback(json);
}
EDIT:
I change lines 67 – 68 as per Raghav Sood answer to:
// int fileName = (Integer) null;
int fileName = args.getInt(KEY_RAW_RES);
This got the class past the NPE – Thnx!!
You’re casting a null value to an integer variable. There is no conceivable scenario in which this will not result in a NullPointerException, since you are using null in the first place. In Java, and several other languages, primitive data types like integer, double, char etc cannot be null.