title: java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference
body: <p>I am making an android app (Android Studio) where the main activity is an activity with a ViewPager.
  Each page shows some information on one of the 118 elements, except for the first one, which shows a list of all elements.</p>
  
  <p>I've made an <code>Element</code> enumeration where all the elements are stored. Also their names are stored there, which I want to get from the string resources (<code>res/values/strings.xml</code>). Since <code>Element</code> is a stand-alone <code>enum</code>, it has no context to get the strings from. I fixed this by making a <code>public static Context context</code> in the main activity (<code>ElementPagerActivity.java</code>). As you can see in the code below, I then call that <code>context</code> for each <code>Element</code> in their constructor.</p>
  
  <p>However, when I execute (nexus 5), Logcat will show the following error:</p>
  
  <pre><code>java.lang.ExceptionInInitializerError
  ...
  Caused by: java.lang.NullPointerException: Attempt to invoke a virtual method on a null object reference
      at com.teinproductions.tein.molu.Element.&lt;clinit&gt;(Element.java:8)
      at com.teinproductions.tein.molu.ElementPagerActivity$1.getCount(ElementPagerActivity.java:40)
      at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:439)
      at com.teinproductions.tein.molu.ElementPagerActivity.onCreate(ElementPagerActivity.java:28)
  ...
  </code></pre>
  
  <p><code>Element.java:8</code> refers to the following line:</p>
  
  <pre><code>HYDROGEN(ElementPagerActivity.context.getResources().getString(R.string.hydrogen),"H",1.0079,1),
  </code></pre>
  
  <p>which is the first declaration of an element in <code>Element.java</code> enum.</p>
  
  <p>Does this error suggest that <code>ElementPagerActivity</code> refers to null? How can I solve this error?</p>
  
  <p>Here is my code (all the code of the project is on github: <a href="https://github.com/teinvdlugt/molu" rel="nofollow">https://github.com/teinvdlugt/molu</a>)</p>
  
  <p><code>Element.java</code> (Yes there are many elements):</p>
  
  <pre><code>package com.teinproductions.tein.molu;
  
  
  import android.os.Bundle;
  
  public enum Element {
  
  HYDROGEN(ElementPagerActivity.context.getResources().getString(R.string.hydrogen),"H",1.0079,1),
  HELIUM(ElementPagerActivity.context.getResources().getString(R.string.helium),"He",4.0026,2),
  LITHIUM(ElementPagerActivity.context.getResources().getString(R.string.lithium),"Li",6.941,3),
  BERYLLIUM(ElementPagerActivity.context.getResources().getString(R.string.beryllium),"Be",9.0122,4),
  BORON(ElementPagerActivity.context.getResources().getString(R.string.boron),"B",10.811,5),
  CARBON(ElementPagerActivity.context.getResources().getString(R.string.carbon),"C",12.0107,6),
  NITROGEN(ElementPagerActivity.context.getResources().getString(R.string.nitrogen),"N",14.0067,7),
  OXYGEN(ElementPagerActivity.context.getResources().getString(R.string.oxygen),"O",15.9994,8),
  FLUORINE(ElementPagerActivity.context.getResources().getString(R.string.fluorine),"F",18.9984,9),
  NEON(ElementPagerActivity.context.getResources().getString(R.string.neon),"Ne",20.1797,10),
  SODIUM(ElementPagerActivity.context.getResources().getString(R.string.sodium),"Na",22.9897,11),
  MAGNESIUM(ElementPagerActivity.context.getResources().getString(R.string.magnesium),"Mg",24.305,12),
  ALUMINUM(ElementPagerActivity.context.getResources().getString(R.string.aluminum),"Al",26.9815,13),
  SILICON(ElementPagerActivity.context.getResources().getString(R.string.silicon),"Si",28.0855,14),
  PHOSPHORUS(ElementPagerActivity.context.getResources().getString(R.string.phosphorus),"P",30.9738,15),
  SULFUR(ElementPagerActivity.context.getResources().getString(R.string.sulfur),"S",32.065,16),
  CHLORINE(ElementPagerActivity.context.getResources().getString(R.string.chlorine),"Cl",35.453,17),
  ARGON(ElementPagerActivity.context.getResources().getString(R.string.argon),"Ar",39.948,18),
  POTASSIUM(ElementPagerActivity.context.getResources().getString(R.string.potassium),"K",39.0983,19),
  CALCIUM(ElementPagerActivity.context.getResources().getString(R.string.calcium),"Ca",40.078,20),
  SCANDIUM(ElementPagerActivity.context.getResources().getString(R.string.scandium),"Sc",44.9559,21),
  TITANIUM(ElementPagerActivity.context.getResources().getString(R.string.titanium),"Ti",47.867,22),
  VANADIUM(ElementPagerActivity.context.getResources().getString(R.string.vanadium),"V",50.9415,23),
  CHROMIUM(ElementPagerActivity.context.getResources().getString(R.string.chromium),"Cr",51.9961,24),
  MANGANESE(ElementPagerActivity.context.getResources().getString(R.string.manganese),"Mn",54.938,24),
  IRON(ElementPagerActivity.context.getResources().getString(R.string.iron),"Fe",55.845,26),
  COBALT(ElementPagerActivity.context.getResources().getString(R.string.cobalt),"Co",58.6934,27),
  NICKEL(ElementPagerActivity.context.getResources().getString(R.string.nickel),"Ni",58.6934,28),
  COPPER(ElementPagerActivity.context.getResources().getString(R.string.copper),"Cu",63.546,29),
  ZINC(ElementPagerActivity.context.getResources().getString(R.string.zinc),"Zn",65.39,30),
  GALLIUM(ElementPagerActivity.context.getResources().getString(R.string.gallium),"Ga",69.723,31),
  GERMANIUM(ElementPagerActivity.context.getResources().getString(R.string.germanium),"Ge",72.64,32),
  ARSENIC(ElementPagerActivity.context.getResources().getString(R.string.arsenic),"As",74.9216,33),
  SELENIUM(ElementPagerActivity.context.getResources().getString(R.string.selenium),"Se",78.96,34),
  BROMINE(ElementPagerActivity.context.getResources().getString(R.string.bromine),"Br",79.904,35),
  KRYPTON(ElementPagerActivity.context.getResources().getString(R.string.krypton),"Kr",83.8,36),
  RUBIDIUM(ElementPagerActivity.context.getResources().getString(R.string.rubidium),"Rb",85.4678,37),
  STRONTIUM(ElementPagerActivity.context.getResources().getString(R.string.strontium),"Sr",87.62,38),
  YTTRIUM(ElementPagerActivity.context.getResources().getString(R.string.yttrium),"Y",88.9095,39),
  ZIRCONIUM(ElementPagerActivity.context.getResources().getString(R.string.zirconium),"Zr",91.224,40),
  NIOBIUM(ElementPagerActivity.context.getResources().getString(R.string.niobium),"Nb",92.9064,41),
  MOLYBDENUM(ElementPagerActivity.context.getResources().getString(R.string.molybdenum),"Mo",95.94,42),
  TECHNETIUM(ElementPagerActivity.context.getResources().getString(R.string.technetium),"Tc",98.0,43),
  RUTHENIUM(ElementPagerActivity.context.getResources().getString(R.string.ruthenium),"Ru",101.07,44),
  RHODIUM(ElementPagerActivity.context.getResources().getString(R.string.rhodium),"Rh",102.9055,45),
  PALLADIUM(ElementPagerActivity.context.getResources().getString(R.string.palladium),"Pd",106.42,46),
  SILVER(ElementPagerActivity.context.getResources().getString(R.string.silver),"Ag",107.8682,47),
  CADMIUM(ElementPagerActivity.context.getResources().getString(R.string.cadmium),"Cd",112.411,48),
  INDIUM(ElementPagerActivity.context.getResources().getString(R.string.indium),"In",114.818,49),
  TIN(ElementPagerActivity.context.getResources().getString(R.string.tin),"Sn",118.71,50),
  ANTIMONY(ElementPagerActivity.context.getResources().getString(R.string.antimony),"Sb",121.76,51),
  TELLURIUM(ElementPagerActivity.context.getResources().getString(R.string.tellurium),"Te",127.6,52),
  IODINE(ElementPagerActivity.context.getResources().getString(R.string.iodine),"I",126.9045,53),
  XENON(ElementPagerActivity.context.getResources().getString(R.string.xenon),"Xe",131.293,54),
  CESIUM(ElementPagerActivity.context.getResources().getString(R.string.cesium),"Cs",132.9055,55),
  BARIUM(ElementPagerActivity.context.getResources().getString(R.string.barium),"Ba",137.327,56),
  LANTHANUM(ElementPagerActivity.context.getResources().getString(R.string.lanthanum),"La",138.9055,57),
  CERIUM(ElementPagerActivity.context.getResources().getString(R.string.cerium),"Ce",140.116,58),
  PRASEODYMIUM(ElementPagerActivity.context.getResources().getString(R.string.praseodymium),"Pr",140.9077,59),
  NEODYMIUM(ElementPagerActivity.context.getResources().getString(R.string.neodymium),"Nd",144.24,60),
  PROMETHIUM(ElementPagerActivity.context.getResources().getString(R.string.promethium),"Pm",(double)145,61),
  SAMARIUM(ElementPagerActivity.context.getResources().getString(R.string.samarium),"Sm",150.36,62),
  EUROPIUM(ElementPagerActivity.context.getResources().getString(R.string.europium),"Eu",151.964,63),
  GADOLINIUM(ElementPagerActivity.context.getResources().getString(R.string.gadolinium),"Gd",157.25,64),
  TERBIUM(ElementPagerActivity.context.getResources().getString(R.string.terbium),"Tb",158.9253,65),
  DYSPROSIUM(ElementPagerActivity.context.getResources().getString(R.string.dysprosium),"Dy",162.5,66),
  HOLMIUM(ElementPagerActivity.context.getResources().getString(R.string.holmium),"Ho",164.9303,67),
  ERBIUM(ElementPagerActivity.context.getResources().getString(R.string.erbium),"Er",167.259,68),
  THULIUM(ElementPagerActivity.context.getResources().getString(R.string.thulium),"Tm",168.9342,69),
  YTTERBIUM(ElementPagerActivity.context.getResources().getString(R.string.ytterbium),"Yb",173.04,70),
  LUTETIUM(ElementPagerActivity.context.getResources().getString(R.string.lutetium),"Lu",174.967,71),
  HAFNIUM(ElementPagerActivity.context.getResources().getString(R.string.hafnium),"Hf",178.49,72),
  TANTALUM(ElementPagerActivity.context.getResources().getString(R.string.tantalum),"Ta",180.9479,73),
  TUNGSTEN(ElementPagerActivity.context.getResources().getString(R.string.tungsten),"W",183.84,74),
  RHENIUM(ElementPagerActivity.context.getResources().getString(R.string.rhenium),"Re",186.207,75),
  OSMIUM(ElementPagerActivity.context.getResources().getString(R.string.osmium),"Os",190.23,76),
  IRIDIUM(ElementPagerActivity.context.getResources().getString(R.string.iridium),"Ir",192.217,77),
  PLATINUM(ElementPagerActivity.context.getResources().getString(R.string.platinum),"Pt",195.078,78),
  GOLD(ElementPagerActivity.context.getResources().getString(R.string.gold),"Au",196.9665,79),
  MERCURY(ElementPagerActivity.context.getResources().getString(R.string.mercury),"Hg",200.59,80),
  THALLIUM(ElementPagerActivity.context.getResources().getString(R.string.thallium),"Tl",204.3833,81),
  LEAD(ElementPagerActivity.context.getResources().getString(R.string.lead),"Pb",207.2,82),
  BISMUTH(ElementPagerActivity.context.getResources().getString(R.string.bismuth),"Bi",208.9804,83),
  POLONIUM(ElementPagerActivity.context.getResources().getString(R.string.polonium), "Po", 209.0, 84),
  ASTATINE(ElementPagerActivity.context.getResources().getString(R.string.astatine), "At", 210.0, 85),
  RADON(ElementPagerActivity.context.getResources().getString(R.string.radon),"Rn",222.0,86),
  FRANCIUM(ElementPagerActivity.context.getResources().getString(R.string.francium),"Fr",223.0,87),
  RADIUM(ElementPagerActivity.context.getResources().getString(R.string.radium),"Ra",226.0,88),
  ACTINIUM(ElementPagerActivity.context.getResources().getString(R.string.actinium),"Ac",227.0,89),
  THORIUM(ElementPagerActivity.context.getResources().getString(R.string.thorium),"Th",232.0381,90),
  PROTACTINIUM(ElementPagerActivity.context.getResources().getString(R.string.protactinium),"Pa",231.0359,91),
  URANIUM(ElementPagerActivity.context.getResources().getString(R.string.uranium),"U",238.0289,92),
  NEPTUNIUM(ElementPagerActivity.context.getResources().getString(R.string.neptunium),"Np",237.0,93),
  PLUTONIUM(ElementPagerActivity.context.getResources().getString(R.string.plutonium),"Pu",244.0,94),
  AMERICIUM(ElementPagerActivity.context.getResources().getString(R.string.americium),"Am",243.0,95),
  CURIUM(ElementPagerActivity.context.getResources().getString(R.string.curium),"Cm",247.0,96),
  BERKELIUM(ElementPagerActivity.context.getResources().getString(R.string.berkelium),"Bk",247.0,97),
  CALIFORNIUM(ElementPagerActivity.context.getResources().getString(R.string.californium),"Cf",251.0,98),
  EINSTEINIUM(ElementPagerActivity.context.getResources().getString(R.string.einsteinium),"Es",252.0,99),
  FERMIUM(ElementPagerActivity.context.getResources().getString(R.string.fermium),"Fm",257.0,100),
  MENDELEVIUM(ElementPagerActivity.context.getResources().getString(R.string.mendelevium),"Md",258.0,101),
  NOBELIUM(ElementPagerActivity.context.getResources().getString(R.string.nobelium),"No",259.0,102),
  LAWRENCIUM(ElementPagerActivity.context.getResources().getString(R.string.lawrencium),"Lr",262.0,103),
  RUTHERFORDIUM(ElementPagerActivity.context.getResources().getString(R.string.rutherfordium),"Rf",261.0,104),
  DUBNIUM(ElementPagerActivity.context.getResources().getString(R.string.dubnium),"Db",262.0,105),
  SEABORGIUM(ElementPagerActivity.context.getResources().getString(R.string.seaborgium),"Sg",266.0,106),
  BOHRIUM(ElementPagerActivity.context.getResources().getString(R.string.bohrium),"Bh",264.0,107),
  HASSIUM(ElementPagerActivity.context.getResources().getString(R.string.hassium),"Hs",277.0,108),
  MEITNERIUM(ElementPagerActivity.context.getResources().getString(R.string.meitnerium),"Mt",269.0,109),
  DARMSTADTIUM(ElementPagerActivity.context.getResources().getString(R.string.darmstadtium),"Ds",281.0,110),
  ROENTGENIUM(ElementPagerActivity.context.getResources().getString(R.string.roentgenium),"Rg",281.0,111),
  COPERNICIUM(ElementPagerActivity.context.getResources().getString(R.string.copernicium),"Cn",285.0,112),
  UNUNTRIUM(ElementPagerActivity.context.getResources().getString(R.string.ununtrium),"Uut",286.0,113),
  FLEROVIUM(ElementPagerActivity.context.getResources().getString(R.string.flerovium),"Fl",289.0,114),
  UNUNPENTIUM(ElementPagerActivity.context.getResources().getString(R.string.ununpentium),"Uup",289.0,115),
  LIVERMORIUM(ElementPagerActivity.context.getResources().getString(R.string.livermorium),"Lv",293.0,116),
  UNUNSEPTIUM(ElementPagerActivity.context.getResources().getString(R.string.ununseptium),"Uus",294.0,117),
  UNUNOCTIUM(ElementPagerActivity.context.getResources().getString(R.string.ununoctium),"Uuo",294.0,118);
  
  private String name,abbreviation;
  private Double mass;
  private int atomicNumber;
  
  public static final Double nA = 602214000000000000000000.0;
  
  public static final String ELEMENT = "ELEMENT_TO_PASS_TO_FRAGMENT_ARGUMENTS";
  
  Element(String name, String abbreviation, Double mass, Integer atomicNumber) {
      this.name = name;
      this.abbreviation = abbreviation;
      this.mass = mass;
      this.atomicNumber = atomicNumber;
  }
  
  
  public Double calculateGramWhenMolGiven(Double givenMol){
      return givenMol * this.mass;
  }
  
  public Double calculateParticlesWhenMolGiven(Double givenMol){
      return givenMol * nA;
  }
  
  public Double calculateMolWhenGramGiven(Double givenGram){
      return givenGram / this.mass;
  }
  
  public Double calculateMolWhenParticlesGiven(Integer givenParticles){
      return givenParticles / nA;
  }
  
  
  public static Element findElementByAbbreviation(String abbreviation){
      for(Element currentElement : Element.values()){
          if(abbreviation.equalsIgnoreCase(currentElement.getAbbreviation())){
              return currentElement;
          }
      }
  
      return null;
  }
  
  public static Element findElementByName(String name){
      for(Element currentElement : Element.values()){
          if(currentElement.getName().equalsIgnoreCase(name)) {
              return currentElement;
          }
      }
  
      return null;
  }
  
  public static Element findElementByAbbreviationOrName(String nameOrAbbreviation){
      Element foundElement;
      foundElement = Element.findElementByAbbreviation(nameOrAbbreviation);
      if(foundElement == null){
          foundElement = Element.findElementByName(nameOrAbbreviation);
      }
  
      return foundElement;
  }
  
  
  public ElementFragment toFragment(){
  
      ElementFragment elementFragment = new ElementFragment();
  
      Bundle args = new Bundle();
      args.putSerializable(ELEMENT, this);
      elementFragment.setArguments(args);
  
      return elementFragment;
  
  }
  
  
  public String getName() {
      return name;
  }
  
  public String getAbbreviation() {
      return abbreviation;
  }
  
  public Double getMass() {
      return mass;
  }
  
  public Integer getAtomicNumber() {
      return atomicNumber;
  }
  }
  </code></pre>
  
  <p><code>ElementPagerActivity.java</code>:</p>
  
  <pre><code>package com.teinproductions.tein.molu;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentStatePagerAdapter;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.ActionBarActivity;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
  public class ElementPagerActivity extends ActionBarActivity {
  
  ViewPager viewPager;
  
  // To allow the Element enumeration to get strings from the resources:
  public static Context context;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      context = getApplicationContext();
      // This class doesn't use a layout resources file.
  
      viewPager = new ViewPager(this);
  
      setContentView(viewPager);
  
      viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
          @Override
          public Fragment getItem(int position) {
              if(position == 0){
                  return new ElementListFragment();
              }
  
              return Element.values()[position + 1].toFragment();
          }
  
          @Override
          public int getCount() {
              return Element.values().length + 1;
          }
      });
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  
      getMenuInflater().inflate(R.menu.menu_element_pager_activity, menu);
  
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
      if(id == R.id.action_bar_element_pager_activity_list){
          // scrolls the viewpager back to the list of elements,
          // which is at index number 0
          viewPager.setCurrentItem(0);
      }
  
      return super.onOptionsItemSelected(item);
  }
  }
  </code></pre>
  
  <p>I've already looked for more questions on this error, but I couldn't find one that solved my problem.</p>
  
  <p>I appreciate all your help!
  If you need more code from the project, say it.</p>
  
  <p>Thanks, </p>
  
  <p>Tein from the Netherlands</p>