I am making an app in which i am stuck in this exception – String index out of bound and i get error at the following line.
String netwrkk = mTelephonyMgr.getNetworkOperator();
System.out.println("hello");
if (netwrkk != null) {
System.out.println("hello2");
// int mcc = Integer.parseInt(netwrkk.substring(0, 3)); // exception at this line
System.out.println("hello3");
int mnc = Integer.parseInt(netwrkk.substring(3));// exception at this line
System.out.println("hello4");
mccc1=(TextView)findViewById(R.id.mccc1);
System.out.println("netwrk:"+netwrkk);
mccc1.setText(Integer.toString(mcc));
mncc1=(TextView)findViewById(R.id.mncc1);
System.out.println("netwrk:"+netwrkk);
mncc1.setText(Integer.toString(mnc));
System.out.println("mccc: "+mcc);
try{
System.out.println("bearing : "+locn.getBearing());
}catch (Exception e) {
System.out.println(e);
}System.out.println("mnc"+mnc);
}
You should check if
netwrkkhas the length of 4 or more. If not, thesubstring(0, 3)will fail.Also make yourself familiar with the
Logclass in android. You shouldn’t useSystem.out.println()