Reputation: 194
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 ChipCount
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.
My ChipCount
class currently looks as follows:
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);
}
}
}
I also have the MainActivity
that tries to make use of ChipCount
but it crashes.
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");
}
}
By commenting out different lines, I have determined the problem to be at the line int chip = chips.getChips();
.
Why does this make the program crash, and how could I possibly fix it.
Here is the logcat:
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.<init>(ChipCount.java:15)
10-01 22:51:26.669: E/AndroidRuntime(28106): at com.example.parceltest.MainActivity.<init>(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
Upvotes: 1
Views: 3154
Reputation: 67502
You never initialize chips
. Therefore it's null
, so you can't access its methods.
Instead of ChipCount chips;
, try: ChipCount chips = new ChipCount();
.
NB: Also, in the future, when you get a crash, post the LogCat error. In this case, it was a relatively simple fix, but those logs might be necessary in the future.
EDIT:
Additionally, you never pass your ChipCount
object a file_context
parameter. That makes file_context
null
when you call it in loadChips();
.
Try this:
public class MainActivity extends Activity {
ChipCount chips; // Don't initialize here, on second thought
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chips = new ChipCount(this); // Initialize here; and pass it `this`, which is an Activity, and also a Context
TextView number_of_chips = (TextView) findViewById(R.id.number_of_chips);
int chip = chips.getChips();
//number_of_chips.setText("500");
}
}
And then, in your constructor for ChipCount
, accept a Context
parameter, then do file_context = newContext;
.
Upvotes: 2
Reputation: 137322
You have never initialized ChipCount chips;
, so it is null when called, and raises a NullPointerException
.
you need to have something like ChipCount chips = new ChipCount();
in your code.
Upvotes: 1