title: Null pointer exception: Attempt to invoke virtual method on a null object reference Android
body: <p>I am trying to create a database in android and am calling the create function defined in DBManager from the onCreateInputView() in another file.</p>
  
  <p>Below are the files that are needed. New to android and not too sure what is happening. What must I do? And what does this error mean?</p>
  
  <p>DBManager.java</p>
  
  <pre><code>package com.project.pesit.finaltelim;
  
  
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  import java.util.List;
  
  import android.app.LauncherActivity;
  import android.content.Context;
  import android.content.res.Resources;
  import android.database.Cursor;
  import android.database.SQLException;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.util.Log;
  
  public class DBManager extends SQLiteOpenHelper {
  
      private SQLiteDatabase db;
  
      private final Context context;
  
      private static final String DATABASE_NAME = "gredict";
      private static final int DATABASE_VERSION = 1;
  
      private static final String DATABASE_CREATE1 ="CREATE TABLE IF NOT EXISTS wordindex (wid INTEGER(4), word VARCHAR(256), gremeaning VARCHAR(1024), isFav TINYINT(1));";
      private static final String DATABASE_CREATE2 ="CREATE TABLE IF NOT EXISTS lists (wid INTEGER(4), sid INTEGER(8), isDefault TINYINT(1));";
      private static final String DATABASE_CREATE3 ="CREATE TABLE IF NOT EXISTS gloss (sid INTEGER(8), meaning VARCHAR(1024), pos INTEGER(1));";
  
      public DBManager(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          this.context=context;
      }
  
  
      @Override
      public void onCreate(SQLiteDatabase db)
      {
  
          db.execSQL(DATABASE_CREATE1);
          db.execSQL(DATABASE_CREATE2);
          db.execSQL(DATABASE_CREATE3);
  
          BufferedReader br = null;
          String sCurrentLine;
  
          //Populate word index table
          try
          {
              Resources res=context.getResources();
              InputStream is = res.openRawResource(R.raw.wordindex);
              br = new BufferedReader(new InputStreamReader(is));
  
              while ((sCurrentLine = br.readLine()) != null) {
                  db.execSQL(sCurrentLine);
              }
  
          }
  
          catch
                  (Exception e)
          {
              e.printStackTrace();
          }
  
          finally
          {
              try
              {
                  if (br != null)br.close();
              }
              catch (IOException ex)
              {
                  ex.printStackTrace();
              }
          }
  
          //Populate lists table
          try
          {
  
              Resources res=context.getResources();
              InputStream is = res.openRawResource(R.raw.list);
              br = new BufferedReader(new InputStreamReader(is));
  
              while ((sCurrentLine = br.readLine()) != null) {
                  db.execSQL(sCurrentLine);
              }
          }
  
          catch
                  (Exception e)
          {
              e.printStackTrace();
          }
  
          finally
          {
              try
              {
                  if (br != null)br.close();
              }
              catch (IOException ex)
              {
                  ex.printStackTrace();
              }
          }
  
          //Populate gloss table
          try
          {
              Resources res=context.getResources();
              InputStream is = res.openRawResource(R.raw.gloss);
              br = new BufferedReader(new InputStreamReader(is));
  
              while ((sCurrentLine = br.readLine()) != null) {
                  db.execSQL(sCurrentLine);
              }
          }
  
          catch
                  (Exception e)
          {
              e.printStackTrace();
          }
  
          finally
          {
              try
              {
                  if (br != null)br.close();
              }
              catch (IOException ex)
              {
                  ex.printStackTrace();
              }
          }
  
  
  
  
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion,
                            int newVersion)
      {
  
          onCreate(db);
      }
  
  
      public Boolean open() throws SQLException
      {
          db = this.getWritableDatabase();
          return true;
      }
  
      public void close()
      {
          db.close();
      }
  
  
      public List&lt;String&gt; read(String box)
      {
  
          List&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
          String query;
          Cursor cursor=null;
          if(box.equals("HF"))
          {
              query = "SELECT distinct word FROM wordindex WHERE wid&lt;335";
          }
          else
          {
              query = "SELECT distinct word FROM wordindex WHERE word LIKE '"+box+"%' and wid&gt;334";
          }
          cursor = db.rawQuery(query, null);
  
          if (cursor.moveToFirst()) {
              do {
                  String word;
                  word=cursor.getString(cursor.getColumnIndexOrThrow("word"));
                  wordList.add(word);
              } while (cursor.moveToNext());
          }
  
          cursor.close();
          return wordList;
      }
  
      public List&lt;String[]&gt; getWord(String name){
          // TODO Auto-generated method stub
          //String[] cols=new String []{WORD,MEAN};
          //String sid=String.valueOf(id);
          List&lt;String[]&gt; meanings = new ArrayList&lt;String[]&gt;();
  
          Cursor c1=db.rawQuery("SELECT DISTINCT (sid) FROM lists WHERE wid = (SELECT wid FROM wordindex where word='"+name+"')",null);
          if (c1.moveToFirst()) {
              do {
  
                  int sid=c1.getColumnIndex("sid");
                  Cursor c2=db.rawQuery("SELECT pos, meaning FROM gloss WHERE sid = "+c1.getString(sid),null);
  
                  if (c2.moveToFirst()) {
                      do {
  
                          int ipos=c2.getColumnIndex("pos");
                          int imean=c2.getColumnIndex("meaning");
                          String pos=null;
                          if(c2.getInt(ipos)==0)
                              pos="(n)";
                          else if(c2.getInt(ipos)==1)
                              pos="(v)";
                          else if(c2.getInt(ipos)==2)
                              pos="(adj)";
                          String mean[] = {pos,c2.getString(imean)};
  
                          meanings.add(mean);
                      } while (c2.moveToNext());
                  }
  
              } while (c1.moveToNext());
          }
  
  
          return meanings;
      }
  
      public String getGREMeaning(String name){
  
          String meaning=null;
          Cursor c=db.rawQuery("SELECT gremeaning FROM wordindex where word='"+name+"'",null);
          c.moveToFirst();
  
          int imean=c.getColumnIndex("gremeaning");
          meaning=c.getString(imean);
  
          return meaning;
      }
  
      public String getGREMeaningWID(int wid){
  
          String meaning=null;
          Cursor c=db.rawQuery("SELECT gremeaning FROM wordindex where wid='"+wid+"'",null);
          c.moveToFirst();
  
          int imean=c.getColumnIndex("gremeaning");
          meaning=c.getString(imean);
  
          return meaning;
      }
  
      public List&lt;ListItem&gt; getList(String word) {
          List&lt;ListItem&gt; l=new ArrayList&lt;ListItem&gt;();
          int groupcount;
          Cursor c=db.rawQuery("SELECT * FROM lists where wid in(SELECT wid from wordindex where word='"+word+"')",null);
          int imean=c.getColumnIndex("sid");
          for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
          {   Cursor s=db.rawQuery("SELECT distinct wid FROM lists where sid="+c.getString(imean),null);
              int index=s.getColumnIndexOrThrow("wid");
              groupcount=s.getCount();
              if(groupcount&gt;1){
                  ListItem li=new ListItem();
                  li.setSID(c.getString(imean));
                  for(s.moveToFirst();!s.isAfterLast();s.moveToNext())
                  {
                      li.addWID(s.getString(index));
  
                  }
                  l.add(li);
              }
          }
          return l;
      }
  
      public List&lt;ListItem&gt; getGroupList() {
          List&lt;ListItem&gt; l=new ArrayList&lt;ListItem&gt;();
  
          Cursor c=db.rawQuery("SELECT sid,meaning FROM v2",null);
          for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
          {
              ListItem li=new ListItem();
              li.setSID(c.getString(c.getColumnIndex("sid")));
              li.setGloss(c.getString(c.getColumnIndex("meaning")));
              l.add(li);
          }
  
          /*
          Cursor c=db.rawQuery("SELECT sid FROM  `v` WHERE  `count` &gt;1",null);
          int imean=c.getColumnIndex("sid");
          for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
          {
              Cursor s=db.rawQuery("SELECT sid,meaning FROM gloss where sid="+c.getString(imean),null);
              s.moveToFirst();
              ListItem li=new ListItem();
              li.setSID(s.getString(s.getColumnIndex("sid")));
              li.setGloss(s.getString(s.getColumnIndex("meaning")));
          l.add(li);
          }
          */
          return l;
      }
  
      public String getGloss(String sid)
      {
          Cursor c=db.rawQuery("select meaning from gloss where sid="+sid,null);
          int i=c.getColumnIndex("meaning");
          if(c.moveToFirst())
          {
              return c.getString(i);
          }
          return "null";
      }
      public String getwd(String wid)
      {
          Cursor c=db.rawQuery("select word from wordindex where wid="+wid,null);
          int i=c.getColumnIndex("word");
          if(c.moveToFirst())
          {
              return c.getString(i);
          }
          return "null";
      }
  
      public List&lt;String&gt; getGroupWords(String sid)
      {
          List&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
          String query;
          Cursor cursor=null;
          query = "SELECT wid,isDefault FROM lists WHERE sid="+sid;
          cursor = db.rawQuery(query, null);
          int iwid=cursor.getColumnIndexOrThrow("wid");
          int defid=cursor.getColumnIndexOrThrow("isDefault");
          if (cursor.moveToFirst()) {
              do {
                  Cursor c=db.rawQuery("SELECT word FROM wordindex WHERE wid="+cursor.getString(iwid),null);
                  c.moveToFirst();
                  String word;
                  word=c.getString(c.getColumnIndexOrThrow("word"));
  
                  wordList.add(word+":"+cursor.getString(defid));
              } while (cursor.moveToNext());
          }
  
          cursor.close();
          return wordList;
      }
  
      public String addToGroup(String sid, String word)
      {
          String wid=null;
          Cursor cursor=null;
          String query = "SELECT wid FROM wordindex WHERE word='"+word+"'";
          cursor = db.rawQuery(query, null);
          String result=null;
          if (cursor.moveToFirst()) {
              wid=cursor.getString(cursor.getColumnIndexOrThrow("wid"));
  
              cursor.close();
  
              query = "SELECT * FROM lists WHERE wid="+wid+" AND sid="+sid;
              cursor = db.rawQuery(query, null);
              if(cursor.moveToFirst())
              {
                  result="Word already in the group.";
              }
              else
              {
                  query = "INSERT INTO lists VALUES("+wid+", "+sid+",0);";
                  try{
                      db.execSQL(query);
                      result="Added successfully";
                  }
                  catch(Exception e)
                  {
                      result="Could not add the word.";
                  }
              }
          }
  
          return result;
      }
  
      public String newGroup(String meaning, String pos, String word)
      {
          if(pos.equals("Noun"))
              pos="0";
          else if(pos.equals("Verb"))
              pos="1";
          else if(pos.equals("Adjective"))
              pos="2";
  
          String wid;
          int max;
          String query = "SELECT max(sid) FROM lists;";
          Cursor cursor = db.rawQuery(query, null);
          String result=null;
          cursor.moveToFirst();
          max=Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow("max(sid)")));
          cursor.close();
  
          query = "SELECT wid FROM wordindex WHERE word='"+word+"'";
          cursor = db.rawQuery(query, null);
          cursor.moveToFirst();
          wid=cursor.getString(cursor.getColumnIndexOrThrow("wid"));
          cursor.close();
  
          try{
              query = "INSERT INTO lists VALUES ("+wid+", "+(max+1)+",0);";
              db.execSQL(query);
              query = "INSERT INTO gloss VALUES ("+(max+1)+", '"+meaning+"',"+pos+");";
              db.execSQL(query);
  
              db.execSQL("DROP VIEW v1;");
              db.execSQL("CREATE  VIEW v1 AS SELECT sid, isDefault, COUNT(sid) as count FROM lists GROUP BY sid;");
              db.execSQL("DROP VIEW v2;");
              db.execSQL("CREATE  VIEW v2 AS SELECT sid,meaning FROM gloss where sid IN (SELECT sid FROM v1 WHERE count&gt;1);");
  
              result="Created successfully";
          }
          catch(Exception e)
          {
              result="Could not create group.";
          }
  
          return result;
      }
  
      public List&lt;String&gt; search(String text,String box)
      {
  
          List&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
          String query;
          Cursor cursor=null;
          if(box.equals("HF"))
          {
              query = "SELECT distinct word FROM wordindex WHERE wid&lt;335 and word LIKE '"+text+"%'";
          }
          else
          {
              query = "SELECT distinct word FROM wordindex WHERE word LIKE '"+box+"%' and wid&gt;334 and word LIKE'"+text+"%'";
          }
          cursor = db.rawQuery(query, null);
  
          if (cursor.moveToFirst()) {
              do {
                  String word;
                  word=cursor.getString(cursor.getColumnIndexOrThrow("word"));
                  // Adding word to list
                  //  Toast.makeText(getApplicationContext(), word, Toast.LENGTH_LONG).show();
                  wordList.add(word);
              } while (cursor.moveToNext());
          }
  
          cursor.close();
          return wordList;
      }
      public List&lt;ListItem&gt; searchGroup(String text)
      {
          List&lt;ListItem&gt; l=new ArrayList&lt;ListItem&gt;();
          //List&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
          String query;
          Cursor c=null;
          query = "SELECT * FROM wordindex WHERE word LIKE'"+text+"%'";
  
          c = db.rawQuery(query, null);
  
          for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
          {
              Cursor s=db.rawQuery("SELECT sid,meaning FROM gloss where sid in(SELECT sid from lists where wid="+c.getString(c.getColumnIndexOrThrow("wid"))+")",null);
              for(s.moveToFirst();!s.isAfterLast();s.moveToNext())
              {
                  //s.moveToFirst();
                  ListItem li=new ListItem();
                  li.setSID(s.getString(s.getColumnIndex("sid")));
                  li.setGloss(s.getString(s.getColumnIndex("meaning")));
                  l.add(li);
              }
  
          }
          c.close();
          return l;
      }
      public String getID(String word1)
      {
          Cursor c=db.rawQuery("select wid from wordindex where word='"+word1+"'",null);
          int i=c.getColumnIndex("wid");
          if(c.moveToFirst())
          {
              return c.getString(i);
          }
          return "null";
      }
  
      public List&lt;String&gt; getFavList()
      {
  
          List&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
          String query;
          Cursor cursor=null;
          query = "SELECT distinct word FROM wordindex WHERE isFav=1";
          cursor = db.rawQuery(query, null);
  
          if (cursor.moveToFirst()) {
              do {
                  String word;
                  word=cursor.getString(cursor.getColumnIndexOrThrow("word"));
                  wordList.add(word);
              } while (cursor.moveToNext());
          }
  
          cursor.close();
          return wordList;
      }
  
      public Boolean isFav(String word)
      {
          Cursor c=db.rawQuery("SELECT isFav FROM wordindex WHERe word='"+word+"'",null);
          int i=c.getColumnIndex("isFav");
          if(c.moveToFirst())
          {
              if(c.getString(i).equals("1"))
              {
                  return true;
              }
  
              else
              {
                  return false;
              }
          }
          return false;
      }
  
      public void toggleFav(String word, Boolean isFav)
      {
          String fav;
          if(isFav)
          {
              fav="1";
          }
  
          else
          {
              fav="0";
          }
          db.execSQL("UPDATE wordindex SET isFav="+fav+" WHERE word='"+word+"'");
  
  
      }
  
      public int getWordCount()
      {
          Cursor c=db.rawQuery("SELECT MAX(wid) FROM wordindex",null);
          int i=c.getColumnIndex("MAX(wid)");
          if(c.moveToFirst())
          {
              return c.getInt(i);
          }
  
          else
          {
              return 0;
          }
      }
      public String getBoxCount(String box)
      {
  
          //List&lt;String&gt; wordList = new ArrayList&lt;String&gt;();
          String query;
          String query1;
          String result="";
          Cursor cursor=null;
          Cursor cursor1=null;
  
          if(box.equals("HF"))
          {
              return "1@334";
          }
          else
          {   query1 = "SELECT MIN(wid) AS count FROM wordindex WHERE word LIKE '"+box+"%' and wid&gt;334";
              query = "SELECT MAX(wid) AS count FROM wordindex WHERE word LIKE '"+box+"%' and wid&gt;334";
          }
          cursor = db.rawQuery(query, null);
          cursor1 = db.rawQuery(query1, null);
  
          if (cursor1.moveToFirst()) {
              result+=cursor1.getString(cursor1.getColumnIndexOrThrow("count"));
          }
          if (cursor.moveToFirst()) {
              result+="@"+cursor.getString(cursor.getColumnIndexOrThrow("count"));
          }
  
          cursor.close();
          return result;
      }
      public String removefromgroup(String item,String sid)
      {
          String result;
          String query;
          Log.d("ITEM",item);
          Log.d("MEANING",getGloss(sid));
          String wid=getID(item);
          query="DELETE FROM lists WHERE wid="+wid+" AND sid="+sid;
          try{
              db.execSQL(query);
              result=item+" removed successfully";
          }
          catch(Exception e)
          {
              e.printStackTrace();
              result="Could not delete the word.";
          }
          return result;
      }}
  </code></pre>
  
  <p>onCreateInputView.java</p>
  
  <pre><code>@Override public View onCreateInputView() {
          mInputView = (KeyboardView) getLayoutInflater().inflate(
                  R.layout.input, null);
          mInputView.setOnKeyboardActionListener(this);
          mInputView.setKeyboard(mQwertyKeyboard);
          DBManager mDBAdapter = new DBManager(getApplicationContext());
          //  boolean a=mDBAdapter.checkDatabase(getApplicationContext());
          //if (!mDBAdapter.checkDatabase(getApplicationContext())) {
  //              try{
  //                  sampleDB=getApplicationContext().openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
  //
  //              }catch(SQLiteException e){
  //                  Toast.makeText(getApplicationContext(), "Could not create database", Toast.LENGTH_SHORT).show();
  //              }sampleDB.close();
  
  
              mDBAdapter.onCreate(sampleDB);
              Toast.makeText(getApplicationContext(), "Database Created", Toast.LENGTH_SHORT).show();
         // }else{
  
             //Toast.makeText(getApplicationContext(), "Available", Toast.LENGTH_SHORT).show();
          //}
          return mInputView;
  
      }
  </code></pre>
  
  <p>The error log is as follows: </p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference
              at com.project.pesit.finaltelim.DBManager.onCreate(DBManager.java:44)
              at com.project.pesit.finaltelim.SoftKeyboard.onCreateInputView(SoftKeyboard.java:118)
              at android.inputmethodservice.InputMethodService.updateInputViewShown(InputMethodService.java:1108)
              at android.inputmethodservice.InputMethodService.showWindowInner(InputMethodService.java:1493)
              at android.inputmethodservice.InputMethodService.showWindow(InputMethodService.java:1468)
              at android.inputmethodservice.InputMethodService$InputMethodImpl.showSoftInput(InputMethodService.java:433)
              at android.inputmethodservice.IInputMethodWrapper.executeMessage(IInputMethodWrapper.java:203)
              at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:37)
              at android.os.Handler.dispatchMessage(Handler.java:102)
              at android.os.Looper.loop(Looper.java:135)
              at android.app.ActivityThread.main(ActivityThread.java:5312)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:372)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
  </code></pre>