Use external class within android activity
<p>I wanted to practice what I would consider basic principles of android development before I actually began working on a project. I currently have a <code>ChipCount</code> class that will take care of saving and loading the amount of chips a player would have. The class would also be responsible for keeping track of the amount of chips in general.  </p>

<p>My <code>ChipCount</code> class currently looks as follows:</p>

<pre><code>package com.example.parceltest;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;

public class ChipCount {

    private int number_of_chips;
    Context file_context;

    public ChipCount() {
        loadChips();
    }

    public void setChips(int chips) {
        this.number_of_chips = chips;
    }

    public int getChips() {
        return number_of_chips;
    }

    public void saveChips() {
        String filename = "chipsave";
        String save_string = String.valueOf(number_of_chips);

        try {
            FileOutputStream output = file_context.getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE);
            output.write(save_string.getBytes());
            output.close();
        } catch(IOException e) {

        }

    }

    public void loadChips() {
        String filename = "chipsave";
        String chip_count = "";

        try {
            FileInputStream input = file_context.getApplicationContext().openFileInput(filename);
            input.read(chip_count.getBytes());
        } catch(IOException e) {
            this.setChips(500);
        }
    }

}
</code></pre>

<p>I also have the <code>MainActivity</code> that tries to make use of <code>ChipCount</code> but it crashes.</p>

<pre><code>package com.example.parceltest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    ChipCount chips = new ChipCount();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView number_of_chips = (TextView) findViewById(R.id.number_of_chips);
        int chip = chips.getChips();
        //number_of_chips.setText("500");
    }

}
</code></pre>

<p>By commenting out different lines, I have determined the problem to be at the line <code>int chip = chips.getChips();</code>.</p>

<p>Why does this make the program crash, and how could I possibly fix it.</p>

<p><strong>Here is the logcat:</strong></p>

<pre><code>10-01 22:51:06.958: I/Process(28051): Sending signal. PID: 28051 SIG: 9
10-01 22:51:26.669: D/AndroidRuntime(28106): Shutting down VM
10-01 22:51:26.669: W/dalvikvm(28106): threadid=1: thread exiting with uncaught exception (group=0x40b7c300)
10-01 22:51:26.669: E/AndroidRuntime(28106): FATAL EXCEPTION: main
10-01 22:51:26.669: E/AndroidRuntime(28106): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.parceltest/com.example.parceltest.MainActivity}: java.lang.NullPointerException
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.os.Looper.loop(Looper.java:137)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.main(ActivityThread.java:4745)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.reflect.Method.invokeNative(Native Method)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.reflect.Method.invoke(Method.java:511)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at dalvik.system.NativeStart.main(Native Method)
10-01 22:51:26.669: E/AndroidRuntime(28106): Caused by: java.lang.NullPointerException
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.example.parceltest.ChipCount.loadChips(ChipCount.java:45)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.example.parceltest.ChipCount.&lt;init&gt;(ChipCount.java:15)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at com.example.parceltest.MainActivity.&lt;init&gt;(MainActivity.java:9)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.Class.newInstanceImpl(Native Method)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at java.lang.Class.newInstance(Class.java:1319)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
10-01 22:51:26.669: E/AndroidRuntime(28106):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
10-01 22:51:26.669: E/AndroidRuntime(28106):    ... 11 more
</code></pre>
