Skip to content

Commit 3b6de2b

Browse files
committed
Fix nullpointer exception when there is no database.
Closing some cursor leakage, and removing some unnecessary boxing given the Android compiles against JDK 6 and up.
1 parent e276f16 commit 3b6de2b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

debug-db/src/main/java/com/amitshekhar/model/Response.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
public class Response {
3030

31-
public List rows = new ArrayList();
31+
public List<Object> rows = new ArrayList<>();
3232
public List<String> columns = new ArrayList<>();
3333
public boolean isSuccessful;
3434
public String error;

debug-db/src/main/java/com/amitshekhar/server/ClientServer.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import android.content.Context;
2828
import android.content.SharedPreferences;
29+
import android.content.pm.LabeledIntent;
2930
import android.content.res.AssetManager;
3031
import android.database.Cursor;
3132
import android.database.sqlite.SQLiteDatabase;
@@ -49,6 +50,7 @@
4950
import java.net.Socket;
5051
import java.net.SocketException;
5152
import java.util.ArrayList;
53+
import java.util.Collections;
5254
import java.util.List;
5355
import java.util.Map;
5456

@@ -372,17 +374,17 @@ private Response query(String sql) {
372374

373375
if (cursor.getCount() > 0) {
374376
do {
375-
List row = new ArrayList();
377+
List<Object> row = new ArrayList<>();
376378
for (int i = 0; i < cursor.getColumnCount(); i++) {
377379
switch (cursor.getType(i)) {
378380
case Cursor.FIELD_TYPE_BLOB:
379381
row.add(cursor.getBlob(i));
380382
break;
381383
case Cursor.FIELD_TYPE_FLOAT:
382-
row.add(Float.valueOf(cursor.getFloat(i)));
384+
row.add(cursor.getFloat(i));
383385
break;
384386
case Cursor.FIELD_TYPE_INTEGER:
385-
row.add(Integer.valueOf(cursor.getInt(i)));
387+
row.add(cursor.getInt(i));
386388
break;
387389
case Cursor.FIELD_TYPE_STRING:
388390
row.add(cursor.getString(i));
@@ -395,7 +397,7 @@ private Response query(String sql) {
395397

396398
} while (cursor.moveToNext());
397399
}
398-
400+
cursor.close();
399401
return response;
400402
} else {
401403
Response response = new Response();
@@ -408,8 +410,9 @@ private Response query(String sql) {
408410
public Response getDBList() {
409411
Response response = new Response();
410412
if (mDatabaseDir != null) {
411-
for (String name : mDatabaseDir.list()) {
412-
response.rows.add(name);
413+
String[] list = mDatabaseDir.list();
414+
if(list != null) {
415+
Collections.addAll(response.rows, list);
413416
}
414417
}
415418
response.rows.add(Constants.APP_SHARED_PREFERENCES);
@@ -427,6 +430,7 @@ public Response getAllTableName() {
427430
c.moveToNext();
428431
}
429432
}
433+
c.close();
430434
response.isSuccessful = true;
431435
return response;
432436
}
@@ -450,7 +454,7 @@ public Response getAllPrefData(String tag) {
450454
SharedPreferences preferences = mContext.getSharedPreferences(tag, Context.MODE_PRIVATE);
451455
Map<String, ?> allEntries = preferences.getAll();
452456
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
453-
List row = new ArrayList();
457+
List<String> row = new ArrayList<>();
454458
row.add(entry.getKey());
455459
row.add(entry.getValue().toString());
456460
response.rows.add(row);

0 commit comments

Comments
 (0)