android:String index out of bound - android

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 netwrkk has the length of 4 or more. If not, the substring(0, 3) will fail.
Also make yourself familiar with the Log class in android. You shouldn't use System.out.println()

Related

Android - JSON No value for 1 - but there is using Log

I'm stuck at trying to get a simple string from a JsonObject.
Here is the basic code. result is a JsonObject returned from an Asynctask. I've done dozens of the same way, and here it doesn't work.
Response:{"status":200}
Error: An error is thrown saying "No value for 1"
When I Log the content of the JsonObject, I get this: 200, which is what I want. For which reason could this return null when there is a value?
Edit if I use the result.has("status"); true is returned. I don't understand.
Log.e("TAG", result.get("status").toString());
try{
String status = result.getString("status");
} catch (Exception e){
e.getMessage();
}
You are getting int value in the object and you are holding in string which causes the error
You are getting int value in status
{
"status":200
}
if it is like
{
"status":"200"
}
Your code works perfectly.
Try this
Log.e("TAG", result.get("status").toString());
try{
int status = result.getInt("status");
} catch (Exception e){
e.getMessage();
}

Eclipse - Functions issue [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I'm trying to get text from server and then check it a to know what actions to take with the text adopted. The problem is that when I try to check if the received text for example is "Exited" the query always return the value "false" when the received text is really "Exited".
Here is the code :
class Get_Message_From_Server implements Runnable
{
public void run()
{
InputStream iStream = null;
try
{
iStream = Duplex_Socket_Acceptor.getInputStream();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//Create byte array of size image
byte[] Reading_Buffer = null;
try
{
Reading_Buffer = new byte [Duplex_Socket_Acceptor.getReceiveBufferSize()];
//New_Buffer = new byte [100];
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
byte[] Byte_Char_1 = new byte[1];
int Byte_String_Lenght = 0;
//read size
try
{
iStream.read(Reading_Buffer);
String Reading_Buffer_Stream_Lenghtor = new String(Reading_Buffer);
//System.out.println("full : " + Reading_Buffer_Stream_Lenghtor);
Byte_String_Lenght = Reading_Buffer_Stream_Lenghtor.indexOf(new String(Byte_Char_1));
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//Convert to String
Meassage = new String(Reading_Buffer);
Meassage = Meassage.substring(0, Byte_String_Lenght);//The text that received
Message_Getted = 1;
}
}
The query :
if(Message_1 != "Exited")//the message query
{
System.out.println("Continued 253");
continue;
}
Its always return the value - false
its important to know that the message is in Utf - 8 encoding
so how i can to fix the issue ?
If you compare strings by using oparators, Java will not look at the contents of the string but at the reference in memory. To compare String content in Java, you should use the following:
String Message_1; // Hopefully has a value sent by the server
if(Message_1.equals("Exited")) {
// Do stuff when exited
} else {
// Do stuff when not exited
}
String is a variable - and variables should start with lower Case letter - Please read Java Code conventions. Also to check if your message contains string you thing it should just do System.out.println(Message_1); and if the message contains what you expect you compare string doing
if(Message_1.equals("Exited")) {
System.out.println("Yes they are equal");
} else {
System.out.println("No they are not");
}
If this will print "No they are not" that simply means that your variable Message_1 is not what you think it is.. As simple as that. There is no such a thing as .equals method does not work. Its your variable that doesn't ;)

Strange behaviour when receiving data

I'm developing an Android app that requires me to get some data from the server, this data is comes as JSON data, I have to receive like 7 JSON Objects, I'm using regular socket programming to get this data, and I get it by launching a thread that will wait for a data to come from the server.
I'm using the following method:
public String getServerRespons() throws JSONException {
String responseLine, server_response = null_string;
try {
input = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
} catch (IOException e) {
}
int count = 0;
boolean first = true;
try {
while (true)
{
if((responseLine = input.readLine()) == null){
break;
}
first = false;
server_response = server_response + responseLine;
//
// some processing to make sure it's a valid JSON
//
if(count == 0){ // related to the Processing Lines result
System.out.println(server_response);
return response; // when commenting that line everything is ok
}
}
} catch (IOException e) {
Login.errorMessage.setText(conn_err);
}
return null;
}
With that way i got like only two or three JSON Objects of the seven ones. BUT, when commenting the return Line and let it completes with the receiving process I got all the seven Objects efficiently and each Object is separated which makes me make sure that the processing i made to validate the JSON is going so well.
I think int count is always 0 in your example so the return statement is always hit.

Android regex - credita card track

I'm emplementing a card reader and I need to use regex in android. Following wikipedia the regex for track1 is:
^%([A-Z])([0-9]{1,19})\^([^\^]{2,26})\^([0-9]{4}|\^)([0-9]{3}|\^)([^\?]+)\?$
Tried here: http://www.regexplanet.com/advanced/java/index.html with the following example:
%B6011898748579348^DOE/ JOHN ^37829821000123456789?
and it worked but not in my aplication.
String re = "%([A-Z])([0-9]{1,19})\\^([^\\^]{2,26})\\^([0-9]{4}|\\^)([0-9]{3}|\\^)([^\\?]+)\\?";
Pattern p = Pattern.compile(re);
String teste = "%B6011898748579348^DOE/ JOHN ^37829821000123456789?";
Matcher m = p.matcher(teste);
Log.d(TAG,"1111: "+m.groupCount());
int i=0;
for(i=0;i<m.groupCount();i++){
try{
Log.d(TAG, "GROUP"+Integer.toString(i)+" - "+m.group(i));
}catch (IllegalStateException e){
Log.d(TAG, e.toString());
}
}
Teste with ^ and $ and multiline but none worked :s
the result is always:
1111: 6
java.lang.IllegalStateException: No successful match so far
java.lang.IllegalStateException: No suc...
...
You need to use m.find() first. Also you should iterate including last group. Try this way
...
if(m.find()){
Log.d(TAG,"1111: " + m.groupCount());
//change '<' into '<=' to include group 6
for(int i=0; i<=m.groupCount(); i++){
try{
Log.d(TAG, "GROUP"+Integer.toString(i)+" - "+m.group(i));
}catch (IllegalStateException e){
Log.d(TAG, e.toString());
}
}
}

Dynamically populating an expandableListView

So All I'm trying to do is create a dynamic expandableListView Currently It works if I just do the groupViews. The problem comes in when I have to populate the children of those groupViews.. I don't know if I'm doing something wrong, or if theres another better way to do it. If anyone knows please let me know. I'm open to anything.
Currently I'm pulling my data off a server and the error I'm getting is java null pointer exception. So I'm thinking it might have something to do with how big I specified my array sizes?
private static String[][] children = new String[7][4];
private static String[] groups = new String[7];
Here is the rest of the code when I try to populate the View.
public void getData(){
try {
int tempGroupCount = 0;
URL food_url = new URL (Constants.SERVER_DINING);
BufferedReader my_buffer = new BufferedReader(new InputStreamReader(food_url.openStream()));
temp = my_buffer.readLine();
// prime read
while (temp != null ){
childrenCount = 0;
// check to see if readline equals Location
//Log.w("HERasdfsafdsafdsafE", temp);
// start a new location
if (temp.equalsIgnoreCase("Location"))
{
temp = my_buffer.readLine();
groups[tempGroupCount] = temp;
tempGroupCount++;
Log.w("HERE IS TEMP", temp);
}
temp = my_buffer.readLine();
while (temp.equalsIgnoreCase("Location") == false){
Log.w("ONMG HEHREHRHERHER", temp);
children[groupCount][childrenCount] = "IAJHSDSAD";
childrenCount++;
temp = my_buffer.readLine();
}
groupCount++;
}
my_buffer.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("IO EXCEPTION", "Exception occured in MyExpandableListAdapter:" + e.toString());
}
}
to me it looks like an error in the loop - as you are reading another line without checking is it null
your while loop should look something like this methinks:
// prime read
while (temp != null ){
int childrenCount = 0;
// check to see if readline equals Location
// start a new location
//Log.w("HERasdfsafdsafdsafE", temp);
if (temp.equalsIgnoreCase("Location"))
{
temp = my_buffer.readLine();
groups[tempGroupCount] = temp;
tempGroupCount++;
Log.w("HERE IS TEMP", temp);
}
//>>remove following line as that one isn't checked and
//>>you are loosing on a line that is potentialy a child
//temp = my_buffer.readLine();
//>>check do you have first item to add subitems
else if (tempGroupCount>0){
while (temp.equalsIgnoreCase("Location") == false){
Log.w("ONMG HEHREHRHERHER", temp);
children[tempGroupCount-1][childrenCount] = "IAJHSDSAD";
childrenCount++;
temp = my_buffer.readLine();
}
//>>next counter is probably not need but can't see if you're using it somewhere else
//groupCount++;
}
I would first replace strings array to some 2d collection for example arraylist2d ( you can google it ) so you could easally add and remove data from list. If you created adapter that extends BaseExpandableListAdapter everything should be handled without any problems.
About NULLPointer, could you paste stacktrace or more info on which line it occurs ?

Categories

Resources