I need help finding why I’m getting a null pointer exception. I’ve tried going into debug mode, but if this works with Eclipse and Android, I can’t figure out how to use it.
The first method is just to count the number of lines that the file has (this will change over time) in order to initialize the array. The data is actually set in the method setData().
public class ATS_BoxesActivity extends Activity {
private String iButtonID, iButtonName;
private String routeID, stopNum;
private String lat, lon;
private String[] dropBoxes;
public ATS_BoxesActivity() throws IOException{
dropBoxes=new String[countLines()];
}
private int countLines() throws IOException{
BufferedReader(getResources().openRawResource(R.raw.spinner_data));
int count=0;
Scanner s = new Scanner(getAssets().open("spinner_data.csv"));
while (s.hasNextLine()) { //this is where the error is pointing
count++;
s.nextLine();
}
}
public String[] setData() throws IOException {
Scanner s = new Scanner(getAssets().open("spinner_data.csv"));
String line = "";
int pointer = 0;
while (s.hasNextLine()) {
line=s.nextLine();
StringTokenizer st = new StringTokenizer(line);
iButtonID = (st.nextToken(","));
iButtonName = (st.nextToken(","));
routeID = (st.nextToken(","));
stopNum = (st.nextToken());
lat = (st.nextToken(","));
// setLongitude(st.nextToken(","));
System.out.println("DropBox: " + iButtonName);
dropBoxes[pointer] = iButtonName;
pointer += 1;
}
return dropBoxes;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner dropSpinner = (Spinner) findViewById(R.id.dbSpinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
this, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
try {
String[] db = setData();
for (int i = 0; i < db.length; i++) {
adapter.add(db[i]);
}
dropSpinner.setAdapter(adapter);
} catch (IOException e) {
e.printStackTrace();
}
Spinner fillSpinner = (Spinner) findViewById(R.id.fillSpinner);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.fill_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fillSpinner.setAdapter(adapter1);
fillSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
}
Your constructor call is throwing a NullPointerException. If the object being passed to the scanner constructor doesn’t implement the Readable interface, a null pointer exception will be thrown. So the open function call or getAssets must be returning null, causing null to be passed to the function.
I read the following, here
And the following code produces a NullPointerException
Save the spinner_data.csv input stream in an InputStream object and then check to see if it’s null. If that’s the case, then the issue may be with the filename, or the files properties. Is it readable? Does it have any permissions?