title: Database Error: "NullPointerException: Attempt to invoke virtual method getApplicationInfo()... on a null object reference"
body: <p>I'm building a simple multi-activity list app to practice with SQLite. I was trying to get the strings from the database to display in the <code>listview</code> when this started. I'm getting the error</p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference</p>
  </blockquote>
  
  <p>Here the code</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      SharedPreferences sharedPreferences;
      ArrayList&lt;String&gt; listItem;
      ArrayAdapter adapter;
      ListView lv = (ListView) findViewById(R.id.list_view);
      DatabaseHelper db = new DatabaseHelper(this);
      Context context;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          listItem = new ArrayList&lt;&gt;();
          viewData();
          context = this;
  
          FloatingActionButton fab = findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  startActivity(new Intent(MainActivity.this, textInput.class));
              }
          });
      }
  
      // cast from sql database to the listview
      private void viewData() {
          Cursor res = db.viewData();
  
          if (res.getCount() == 0 ){
              Toast.makeText(this,"No Data!",Toast.LENGTH_SHORT).show();
          }
          else{
              while (res.moveToNext()){
                  listItem.add(res.getString(1));
              }
              adapter = new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1,listItem);
              lv.setAdapter(adapter);
          }
      }
  }
  </code></pre>
  
  <p>This is the Database Helper:</p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper {
  public static final String DATABASE_NAME = "item.db";
  public static final String TABLE_NAME = "item_table";
  public static final String ID = "item_id";
  public static final String NAME = "item_name";
  public static final String PRICE = "item_price";
  public static final String URL = "item_url";
  
  public DatabaseHelper(@Nullable Context context) {
      super(context, DATABASE_NAME, null, 1);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL("create table " + TABLE_NAME +" ( item_id INTEGER PRIMARY 
  KEY AUTOINCREMENT,item_name TEXT,item_price INTEGER,item_url TEXT) ");
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
      onCreate(db);
  }
  
  public boolean insertData(String name, String price, String url){
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put(NAME, name);
      contentValues.put(PRICE, price);
      contentValues.put(URL, url);
      long result = db.insert(TABLE_NAME,null,contentValues);
      if(result == -1)
          return false;
      else
          return true;
  }
  
  
  public Cursor viewData(){
      SQLiteDatabase db  = this.getReadableDatabase();
      Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
      return res;
  }
  </code></pre>
  
  <p>This is the textInput Activity</p>
  
  <pre><code>public class textInput extends AppCompatActivity {
  SharedPreferences sharedPreferences;
  //    Editor editor;
  //    Context _context;
  //    int PRIVATE_MODE = 0;
  //    private static final String PREF_NAME = "Items";
  //    public static final String NAME_LIST = "name";
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_text_input);
      //DatabaseHelper db;
  
      //return's to main activity on press
      Button addButton = (Button) findViewById(R.id.addButton);
      final EditText name = (EditText) findViewById(R.id.itemName);
      final EditText url = (EditText) findViewById(R.id.itemURL);
      final EditText price = (EditText) findViewById(R.id.itemPrice);
   //   final SharedPreferences sharedPreferences = 
  getApplicationContext().getSharedPreferences("items", 0);
      final DatabaseHelper db = new DatabaseHelper(this);
      addButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              //Checks to make sure that all fields are filled out, if yes 
  then returns to MainActivity
              if (name.length() != 0 &amp;&amp; price.length() != 0 &amp;&amp; url.length() 
   !=0){
                  boolean isInserted = 
  db.insertData(name.getText().toString(),
                          price.getText().toString(),
                          url.getText().toString());
                  if(isInserted == true)
                      Toast.makeText(textInput.this, "New Item Added", 
   Toast.LENGTH_SHORT).show();
                  else
                      Toast.makeText(textInput.this, "Failed to Add", 
  Toast.LENGTH_SHORT).show();
                  //The graphic and the return to MainActivity
                  Intent returnToMain = new Intent(getApplicationContext(), 
  MainActivity.class);
                  startActivity(returnToMain);
              }
              else{
                  Toast.makeText(textInput.this, "Please fill out all 
  fields!", Toast.LENGTH_SHORT).show();
              }
  
          }
      });
  </code></pre>
  
  <p>Debug (Error?) Log</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.xmaswishlist, PID: 25470
      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.xmaswishlist/com.example.xmaswishlist.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2841)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
          at android.app.ActivityThread.-wrap11(Unknown Source:0)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
          at android.os.Handler.dispatchMessage(Handler.java:105)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6944)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
       Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
          at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
          at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
          at android.content.Context.obtainStyledAttributes(Context.java:655)
          at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
          at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
          at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
          at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
          at com.example.xmaswishlist.MainActivity.&lt;init&gt;(MainActivity.java:36)
          at java.lang.Class.newInstance(Native Method)
          at android.app.Instrumentation.newActivity(Instrumentation.java:1180)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
          at android.app.ActivityThread.-wrap11(Unknown Source:0) 
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
          at android.os.Handler.dispatchMessage(Handler.java:105) 
          at android.os.Looper.loop(Looper.java:164) 
          at android.app.ActivityThread.main(ActivityThread.java:6944) 
          at java.lang.reflect.Method.invoke(Native Method) 
          at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
  </code></pre>
  
  <p>I have tried to fix this using the following:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/52509890/android-studio-attempt-to-invoke-virtual-method-android-database-cursor-on-a-n">Android studio “ Attempt to invoke virtual method android.database.Cursor on a null object reference” problem</a></li>
  <li><a href="https://stackoverflow.com/questions/41713823/database-error-attempt-to-invoke-virtual-method-cursor-getscene-on-a-null">Database error: Attempt to invoke virtual method &#39;Cursor .getScene()&#39; on a null object reference</a></li>
  <li><a href="https://stackoverflow.com/questions/34197026/android-content-pm-applicationinfo-android-content-context-getapplicationinfo">&#39;android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()&#39; on a null object reference when notify notification Android</a>  </li>
  </ol>