0
 void fReadFileWriteDB()
{
    String name = "";
    String color = "";
    String food = "";
    String food2 = "";
    String diet = "";
    String gender = "";
    String age = "";
    String salary = "";

    //Read From text
    StringBuffer bufName = new StringBuffer();
    InputStream issName = null;
    StringBuffer bufDiet = new StringBuffer();
    InputStream issDiet = null;
    StringBuffer buffood = new StringBuffer();
    InputStream issfood = null;
    StringBuffer buffood2 = new StringBuffer();
    InputStream issfood2 = null;
    StringBuffer bufAge = new StringBuffer();
    InputStream issAge = null;
    StringBuffer bufGender = new StringBuffer();
    InputStream issGender = null;
    StringBuffer bufSalary = new StringBuffer();
    InputStream issSalary = null;
    StringBuffer bufColor = new StringBuffer();
    InputStream issColor = null;

    //get Shared prefs

    ContentValues values = new ContentValues();

    try{
        issName = MainActivity.this.getResources().getAssets().open("names.txt");
        issColor = MainActivity.this.getResources().getAssets().open("colors.txt");
        issDiet = MainActivity.this.getResources().getAssets().open("diet.txt");
        issfood =  MainActivity.this.getResources().getAssets().open("food.txt");
        issfood2 = MainActivity.this.getResources().getAssets().open("food2.txt");
        issGender = MainActivity.this.getResources().getAssets().open("gender.txt");
        issAge = MainActivity.this.getResources().getAssets().open("age.txt");
        issSalary = MainActivity.this.getResources().getAssets().open("salary.txt");

        BufferedReader Namereader = new BufferedReader((new InputStreamReader(issName)));
        BufferedReader Agereader = new BufferedReader((new InputStreamReader(issAge)));
        BufferedReader Colorreader = new BufferedReader((new InputStreamReader(issColor)));
        BufferedReader Dietreader = new BufferedReader((new InputStreamReader(issDiet)));
        BufferedReader Genderreader = new BufferedReader((new InputStreamReader(issGender)));
        BufferedReader foodreader = new BufferedReader((new InputStreamReader(issfood)));
        BufferedReader food2reader = new BufferedReader((new InputStreamReader(issfood2)));
        BufferedReader salaryreader = new BufferedReader((new InputStreamReader(issSalary)));
        if((issName!=null) && (issColor!=null) && (issDiet!=null) && (issfood!=null) && (issfood2!=null) && (issGender!=null) && (issAge!=null) && (issSalary!=null))
        {
            while (((name=Namereader.readLine())!=null) && ((color=Colorreader.readLine())!=null) && ((diet=Dietreader.readLine())!=null) && ((age=Agereader.readLine())!=null) &&
            ((food=foodreader.readLine())!=null) && ((food2=food2reader.readLine())!=null) && ((gender=Genderreader.readLine())!=null) && ((salary=salaryreader.readLine())!=null))
            {
                Log.d("Test","Read is SUCESSFULL->"+name+","+color+","+diet+","+age+","+food+","+food2+","+gender+","+salary);

                bufName.append(name+"\n");
                bufColor.append(color+"\n");
                buffood.append(food+"\n");
                buffood2.append(food2+"\n");
                bufGender.append(gender+"\n");
                bufDiet.append(diet+"\n");
                bufAge.append(age+"\n");
                bufSalary.append(salary+"\n");

                values.put("name",name);
                values.put("diet",diet);
                values.put("colors",color);
                values.put("food",food);
                values.put("food2",food2);
                values.put("gender",gender);
                values.put("age",Integer.parseInt(age));
                values.put("salary",Long.parseLong(salary));




                long ins = qdb.insert("employees",null,values);
                //Below lines for test only
                if(ins > 0)
                    Log.d("Test","Row inserted->"+name+","+color+","+diet+","+age+","+food+","+food2+","+gender+","+salary);
                else
                    Log.d("Test","Row Not inserted->");

            }
            issName.close();
            issColor.close();
            issDiet.close();
            issfood.close();
            issfood2.close();
            issGender.close();
            issAge.close();
            issSalary.close();

        }
    }
    catch (IOException e){
        e.printStackTrace();
        Log.e("Test","Error->"+e.toString());}

}

Below is STACT TRACE:

com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
                                                 at com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)
                                                 at com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)
                                                 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:14)
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)
                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                 at java.lang.Thread.run(Thread.java:764)
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.ai.run(SourceFile:6)
                                              Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
                                                 at com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)
                                                 at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
                                                 at java.io.InputStream.read(InputStream.java:101)
                                                 at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)
                                                 at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)
                                                 at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) 
                                                 at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:14) 
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4) 
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4) 
                                                 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                                                 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                                                 at java.lang.Thread.run(Thread.java:764) 
                                                 at com.google.android.apps.gsa.shared.util.concurrent.a.ai.run(SourceFile:6) 

DESCRIPTION:

Here I am trying to take values from text file and save into database. But I am getting buffer overflow exception error. When i debugged the code i found out that following line of code is causing the problem long ins = qdb.insert("employees",null,values); My conclusion is reading from the text file works fine but writing to database if causing problem, kindly help me.

if I remove below part of code I get following output(out snapshot and contents of files added below). And i wont get the buffer overflow exception

long ins = qdb.insert("employees",null,values);
                //Below lines for test only
                if(ins > 0)
                    Log.d("Test","Row inserted->"+name+","+color+","+diet+","+age+","+food+","+food2+","+gender+","+salary);
                else
                    Log.d("Test","Row Not inserted->");

OUTPUT

Text Files Content

  • Can you post the stack trace. – cincy_anddeveloper Nov 28 '17 at 03:29
  • I have added to stack trace to the question kindly check out thank you – user2988694 Nov 28 '17 at 03:47
  • The problem isn't with your Database it's with your BufferReaders, you're running out of memory while attempting to read the data from the txt files. – cincy_anddeveloper Nov 28 '17 at 03:49
  • the below code is used to print the values read from file "Log.d("Test","Read is SUCESSFULL->"+name+","+color+","+diet+","+age+","+food+","+food2+","+gender+","+salary);:. This will execute fine without inserting values into database. – user2988694 Nov 28 '17 at 03:55
  • You have that log message inside of while loop, so after a few dozen or maybe even a hundred loops you will run out of memory. Debug the code and step through it until it crashes. – cincy_anddeveloper Nov 28 '17 at 03:57
  • I have updated the question according to your request kindly check out – user2988694 Nov 28 '17 at 04:32
  • Try changing your targetAPI to 23 as per this stackoverflow answer: https://stackoverflow.com/questions/43900539/google-play-example-error-code-393238 – cincy_anddeveloper Nov 28 '17 at 04:42
  • Thank you very much for the reply. I found out the issue, this was caused because i did not get an instance of the database. I was missing the statement " qdb = db.getReadableDatabase();" – user2988694 Nov 30 '17 at 01:23

0 Answers0