我需要帮助找出为什么会出现空指针异常。我试过进入调试模式,但如果这适用于 Eclipse 和 Android,我不知道如何使用它。第一种方法只是计算文件的行数(这将随时间变化)以初始化数组。数据实际上是在方法 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());
}
}