facing android run time error called ArrayIndexOutOfBoundException.

0 votes
public class Server extends Activity implements OnClickListener {
// EditText editText;
// TextView text1;
Button button;
Button button1;
SQLiteDatabase db_server;
private ServerSocket serverSocket;
 
Handler updateConversationHandler;
 
Thread serverThread = null;
 
private TextView text;
 
public static final int SERVERPORT = 6000;
 
@Override
public void onCreate(Bundle savedInstanceState) {
 
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
text = (TextView) findViewById(R.id.text2);
// editText = (EditText) findViewById(R.id.editText1);
// button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(this);
// button.setOnClickListener(this);
 
db_server = openOrCreateDatabase("server_DB1", Context.MODE_PRIVATE,
null);
db_server
.execSQL("CREATE TABLE IF NOT EXISTS server_table1(name VARCHAR,image BLOB);");
updateConversationHandler = new Handler();
 
this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
 
}
 
@Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
 
class ServerThread implements Runnable {
 
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
 
try {
 
socket = serverSocket.accept();
 
CommunicationThread commThread = new CommunicationThread(
socket);
new Thread(commThread).start();
 
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
 
class CommunicationThread implements Runnable {
 
private Socket clientSocket;
 
private BufferedReader input;
 
public CommunicationThread(Socket clientSocket) {
 
this.clientSocket = clientSocket;
 
try {
 
this.input = new BufferedReader(new InputStreamReader(
this.clientSocket.getInputStream()));
 
} catch (IOException e) {
e.printStackTrace();
}
}
 
public void run() {
 
while (!Thread.currentThread().isInterrupted()) {
 
try {
 
String read = input.readLine();
 
updateConversationHandler.post(new updateUIThread(read));
 
} catch (IOException e) {
e.printStackTrace();
}
}
}
 
}
 
class updateUIThread implements Runnable {
private String msg;
 
public updateUIThread(String str) {
this.msg = str;
}
 
@Override
public void run() {//showing error in this method
 
String data[] = msg.split(",");
String name = data[0];
String image = data[1];
for (int i = 0; i < data.length; i++) {
if (i == 0)
name = data[i];
else if (i == 1)
image = data[i];
}
Log.e("immmmmmmmaaaaaaaaaaaaggggee", name + "," + image);
byte[] decodedImage = null;
try {
decodedImage = Base64.decode(image);
} catch (IOException e) {
e.printStackTrace();
}
db_server.execSQL("insert into server_table1 values('" + name
+ "'," + decodedImage + ")");
 
text.setText(text.getText().toString() + "Client Name: " + msg
+ "\n");
}
 
}
 
@Override
public void onClick(View v) {
/*
* if (v == button) {
* db_server.execSQL("INSERT INTO server_table VALUES('" +
* editText.getText() + "');"); showMessage("Success", "Record added");
* clearText(); }
*/
if (v == button1) {
Cursor c = db_server.rawQuery("SELECT * FROM server_table1", null);
if (c.getCount() == 0) {
showMessage("Error", "No records found");
return;
}
if (c.moveToFirst()) {
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
buffer.append("name: " + c.getString(0) + "\n");
buffer.append("image: " + c.getBlob(1) + "\n");
 
}
showMessage(" Details", buffer.toString());
 
}
 
}
 
}
 
public void showMessage(String title, String message) {
Builder builder = new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
 
}
asked Oct 16, 2014 in App42 Cloud API-BaaS by Sirisha Siri (10 points)
i found the solution............
in the place of String image = data[1]; i changed to String image = data[0];.
now no such error but in the place of image it is taking null value
i mean in log cat it showing null in the place of image type
Whenever you perform operation on Array, please make a prior check on null or its length after that you can retrieve data accordingly.

1 Answer

0 votes

Hey Srisha , Here we are not support the general queries,here we are support queries related to our products.

In your case while parsing message code you are using :

String data[] = msg.split(",");
String name = data[0];
String image = data[1];
 
Please check the message that you are splitting. and also check the length of data array.
  1. Either message is empty.
  2. Or your data array length is less than 2.

So that is the resaon for the exception in your case, please debug your code by own and you can easily solve it out.

 
 
answered Oct 16, 2014 by Vishnu Garg (674 points)
edited Oct 16, 2014 by Vishnu Garg
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...