-4

I'm getting NullpointerException while trying to access db in Assets folder. And the db is indicated red in color on Assets folder.

Image containing Db in Assets

public void createdatabase() throws IOException
{
    System.out.println(" came into Create database ");
    //Open your local db as the input stream
    InputStream myinput = mycontext.getAssets().open(DB_NAME);
    System.out.println(" myinput = "+myinput.toString());
    System.out.println(" Got input ");

    // Path to the just created empty db
    String outfilename = DB_PATH + DB_NAME;
    System.out.println(" outfilename = "+outfilename);

    //Open the empty db as the output stream
    OutputStream myoutput = new FileOutputStream(outfilename);

    // transfer byte to inputfile to outputfile
    System.out.println(" created outputStream");
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myinput.read(buffer))>0) {
        myoutput.write(buffer,0,length);
    }

    //Close the streams
    myoutput.flush();
    myoutput.close();
    myinput.close();
}

Is there any way to check the db values in String?

Komal12
  • 3,119
  • 4
  • 13
  • 25
Khan.N
  • 29
  • 4
  • post your code for getting db from asset – Nikhil Sharma Apr 21 '17 at 05:18
  • but the code is stop executing at myinput showing nullPointerException – Khan.N Apr 21 '17 at 05:23
  • the you getting is not right way to get db – Nikhil Sharma Apr 21 '17 at 05:24
  • http://stackoverflow.com/questions/218384 – Mike M. Apr 21 '17 at 05:29
  • private static String getDatabasePath(Context _context, String _databaseName) { return "/data/data/"+_context.getPackageName()+"/databases/"+_databaseName; } – Nikhil Sharma Apr 21 '17 at 05:30
  • The thing is i need to get data from Assets --> db and should store that in /data/data/.... path – Khan.N Apr 21 '17 at 05:43
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – David Rawson Apr 21 '17 at 06:49
  • `The thing is i need to get data from Assets -` You want to copy a file from assets to internal memory. Thats what you want. You should have used that as subject too. That it is a sqlite file is irrelevant for the problem. A file is a file. – greenapps Apr 21 '17 at 07:20
  • if i follow the same process in new project it is functioning properly, but if i implement it in my existing project it is passing error in this line 'InputStream myinput = mycontext.getAssets().open(DB_NAME);' – Khan.N Apr 21 '17 at 09:09
  • Passing error? Please show the complete exception message. Copy from logcat. Then `mycontext` will be null. Or mycontext.getAssets(). – greenapps Apr 22 '17 at 06:20

1 Answers1

0

DB_NAME does not match with the name of the file in assets folder.

Any reason why you did not tell the values?

greenapps
  • 10,799
  • 2
  • 13
  • 19
  • How did you come to that conclusion? That would throw a `FileNotFoundException`, not a `NullPointerException`. I mean, I'm not saying you're wrong. They very well may have the wrong name, but the immediate issue is a NPE, apparently. Did I miss some comments? – Mike M. Apr 22 '17 at 00:58
  • Well tell the value of DB_NAME. And the file name. Compare. They are not equal. Why didnt you post both values if i suggest that they are not equal? And i even asked you to tell the values. Unbelievable. – greenapps Apr 22 '17 at 06:13
  • I'm not the OP. I was just asking if they'd mentioned the value of `DB_NAME` in some comment I'd missed that had been deleted. – Mike M. Apr 22 '17 at 06:18
  • Sorry, i oversaw. This is such a confusing post. Indeed a null pointer exception first. Sorry. A null context i think. – greenapps Apr 22 '17 at 06:26
  • Yeah, that's what I'm betting, too. – Mike M. Apr 22 '17 at 06:28
  • I got the output, there is nothing wrong with the code. Anyway thanks all – Khan.N Apr 24 '17 at 09:47