Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:26:21+00:00 2026-06-12T04:26:21+00:00

I wanted to practice what I would consider basic principles of android development before

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:26:23+00:00Added an answer on June 12, 2026 at 4:26 am

    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;.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm taking over a project and wanted to understand if this is common practice
I have been working on an app for my school recently and wanted to
I'm trying to practice writing better code, so I wanted to validate my input
If I wanted to have my data in SQL Server, but wanted to use
Hey, I was just writing another basic script for my JS & jQuery practice,
I wanted to do this because i wanted to have a master page bound
I've been reviewing algorithms for practice, and I'm currently looking at a permutation algorithm
I'm learning HTML and I wanted to practice by recreating a invoice sent to
Just to be educated I wanted to know if it is a good practice
I wanted to get an idea of the best practice for how much information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.