lilgodwin
lilgodwin

Reputation: 1108

Android App Crash NullPointerException

I've done the searching and it seems to be a case by case basis as to what's causing this. Though, someone who truly understands it will quite possibly disagree.

My buttons going from my main screen into other activities work just fine, the only difference with the one that doesn't work is that it's actually pulling a class/variable from another .java file and so I'm sure I haven't implemented it correctly.

It's a Singleton class:

GLOBAL.JAVA

//Constructor 
//
protected Global() {
    getInstance();
}


//Singleton Method
//
public static Global getInstance() {
    if(instance == null) {
        initialize();
        instance = new Global();
    }
    return instance;
}

I have a few (static)class structures(I use the term structures because I program primarily in C++, and to me, that's what they are) that are placed inside the Global file/class, along with this array that holds all the data for the program static questionStruct[] questions = new questionStruct[TOTAL_QUESTIONS];

The initialize(); function called from the getInstance()(Singleton Method) simply fills in the data into the questionStruct[] array.

I have a method to return a string

public static String getQ(int q){

    return questions[q].q;
}

In the .java file that gets called from the button pressed, I have placeData(); just to test things out, and in that function:

public void placeData() {

    String testString = ("Testing Text: " + Global.getQ(3));

    TextView Q = (TextView) findViewById(R.id.testingText);
    Q.setText(testString);


}

and lastly... hopefully... the TextView where it's supposed to show up in the XML file:

    <TextView
        android:id="@+id/testingText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

And here's my official error I believe is the indicator to the crash, that only occurs when I press the button to initialize this activity.

java.lang.RuntimeException: Unable to start activity ComponentInfo{godwin.com.study/godwin.com.study.practice}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String godwin.com.study.Global$questionStruct.q' on a null object reference

Thoughts???

EDIT:

Classes (like a structure in C++) inside of Global.Java

class answerStruct
{
    String a;
    boolean status;
}


//Structure for Questions
//
class questionStruct
{
    String q;

    answerStruct[] answer = new answerStruct[TOTAL_ANSWERS];

}

The "initialize()" function:

public static void initialize() {

    questions[0].q = "Question # 1";
    questions[0].answer[0].a = "Q1A1";
    questions[0].answer[0].status = true;
    questions[0].answer[1].a = "Q1A2";
    questions[0].answer[1].status = false;

    ...And so on...Forever
}

Upvotes: 1

Views: 391

Answers (1)

samgak
samgak

Reputation: 24417

When you allocate an array of objects in Java like this:

static questionStruct[] questions = new questionStruct[TOTAL_QUESTIONS];

You are only allocating an array of object references, you aren't actually allocating the objects themselves. It's kind of like you allocated an array of pointers in C++, but they don't point to anything yet.

At the top of your initialize() function, you can allocate all the questionStruct objects, and also the answers for each in a nested for loop, like this:

for(int i = 0; i < TOTAL_QUESTIONS; i++)
{
    questions[i] = new questionStruct();
    for(int j = 0; j < TOTAL_QUESTIONS; j++)
        questions[i].answer[j] = new answerStruct();
}

Then you can go through and initialize all the fields in the structs as you are doing.

Upvotes: 2

Related Questions