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???


No comments:

Post a Comment