-3

help me how to fix this code from AutocompleteTextview i cant see the error code from this part

 final String [] myData =  myDB.SelectAllData();

 final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.TVresult);
 final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice,myData);
    autoCom.setAdapter(adapter);

and This Part

public String[] SelectAllData() {


    try {
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        {
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /***
                 *  [x] = Name
                 */
                int i= 0;
                do {
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            }
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null;
    }

}

This is my code of MainActivity

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    resulttext = (TextView) findViewById(R.id.TVresult);
    rescebu = (TextView) findViewById(R.id.english);
    trans = (Button) findViewById(R.id.translate);
    respam = (TextView) findViewById(R.id.tagalog);
    resilo = (TextView) findViewById(R.id.vis);
    resbik = (TextView) findViewById(R.id.ilonngo);
    myDB = new DatabaHelper(this);
    tts = new TextToSpeech(this, this);
    final String [] myData =  myDB.SelectAllData();

    final AutoCompleteTextView autoCom = (AutoCompleteTextView)findViewById(R.id.TVresult);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice,myData);
    autoCom.setAdapter(adapter);
    autoCom.setThreshold(1);
    autoCom.setTextColor(Color.RED);

This is The Dabasehelper and SelectAllData Method

public class DatabaHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "translator4.sqlite";
private static final String DB_TABLE_NAME = "wews";
public DatabaHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

}

public Cursor getAllData(String English, String Tagalog, String Visaya, String Ilonggo) {
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from wews where English like '" + English + "' or Tagalog like '" + Tagalog + "' or Visayan like '" + Visaya + "' or Ilonggo like '" + Ilonggo + "';", null);

    return res;
}


@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}


public String[] SelectAllData() {


    try {
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        {
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /***
                 *  [x] = Name
                 */
                int i= 0;
                do {
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            }
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null;
    }

}

This is The logs LogCat

Process: application.example.com.myapplication, PID: 20663
                                                                                           Theme: themes:{default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system}
                                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{application.example.com.myapplication/application.example.com.myapplication.MainActivity}: java.lang.NullPointerException: storage == null
                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
                                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
                                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5475)
                                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                                                            Caused by: java.lang.NullPointerException: storage == null
                                                                                               at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
                                                                                               at java.util.Arrays.asList(Arrays.java:155)
                                                                                               at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
                                                                                               at application.example.com.myapplication.MainActivity.onCreate(MainActivity.java:56)
                                                                                               at android.app.Activity.performCreate(Activity.java:7125)
                                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415)
                                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
                                                                                               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5475) 
                                                                                               at java.lang.reflect.Method.invoke(Native Method) 
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                                               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
MikeT
  • 51,415
  • 16
  • 49
  • 68
  • 1
    Could you try shortening the amount of code you post down to a [mcve]? – LW001 Oct 11 '17 at 16:52
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bö macht Blau Oct 11 '17 at 16:58

2 Answers2

0

Possible Modification in this code block as you missed to created string object ArrayData[i] = new String() in do...while loop.

public String[] SelectAllData() {


    try { 
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        { 
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /*** 
                 *  [x] = Name 
                 */ 
                int i= 0;
                do { 
                    ArrayData[i] = new String();
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            } 
        } 
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null; 
    } 

}
Pritesh Patel
  • 678
  • 17
  • 35
  • Caused by: java.lang.NullPointerException: storage == null at java.util.Arrays$ArrayList.(Arrays.java:38) at java.util.Arrays.asList(Arrays.java:155) at android.widget.ArrayAdapter.(ArrayAdapter.java:137) – Marvenn Oct 11 '17 at 17:10
  • This line creates final String [] myData = myDB.SelectAllData(); error as it's returning null. Check you table if it has record or not. – Pritesh Patel Oct 11 '17 at 17:16
0

I'd suggest changing :-

public String[] SelectAllData() {


    try {
        String ArrayData[] = null;
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        if(cursor != null)
        {
            if (cursor.moveToFirst()) {
                ArrayData = new String[cursor.getCount()];
                /***
                 *  [x] = Name
                 */
                int i= 0;
                do {
                    ArrayData[i] = cursor.getString(0);
                    i++;
                } while (cursor.moveToNext());

            }
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return null;
    }

}

to :-

public String[] SelectAllData() {

    try {
        String ArrayData[];
        SQLiteDatabase db;
        db = this.getReadableDatabase(); // Read Data

        String strSQL = "SELECT  * FROM " + DB_TABLE_NAME;
        Cursor cursor = db.rawQuery(strSQL, null);

        ArrayData = new String[cursor.getCount()];
        int i = 0;
        while (cursor.moveToNext()) {
            ArrayData[i++] = cursor.getString(0);
            //ArrayData[cursor.getPosition()] = cursor.getString(0); //Could be an alternative
        }
        cursor.close();

        return ArrayData;

    } catch (Exception e) {
        return new String[0];
    }
}

In short, the check for a null cursor is useless it DOES NOT signify no data, rather an empty cursor with a count of 0 is returned when there is no data.

Thus when there is no data cursor.moveToFirst will be false and none of the code within the if will be executed. As such ArrayData will be null (the likely reason for the null pointer exception).

The modified code will return an empty array (0 elements) if there is no data or if there is another exception captured in the try block.

You could also do away with using a counter/index, by using cursor.getPosition(), which is effectively the same (as per commented out line).

MikeT
  • 51,415
  • 16
  • 49
  • 68