Reputation: 5797
I'm building an Android app to quiz people on subjects, the backend is written in Java and it reads and parses CSV file (hard coded URL) , and the front end is my Android app.
Here is my error:
05-08 01:19:00.792: E/AndroidRuntime(790): java.lang.RuntimeException: Unable to start activity ComponentInfo{cs314.adamnick.p4/cs314.adamnick.p4.QuizTakerGUI}: java.lang.NullPointerException
05-08 01:19:00.792: E/AndroidRuntime(790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.os.Looper.loop(Looper.java:123)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-08 01:19:00.792: E/AndroidRuntime(790): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 01:19:00.792: E/AndroidRuntime(790): at java.lang.reflect.Method.invoke(Method.java:521)
05-08 01:19:00.792: E/AndroidRuntime(790): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-08 01:19:00.792: E/AndroidRuntime(790): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-08 01:19:00.792: E/AndroidRuntime(790): at dalvik.system.NativeStart.main(Native Method)
05-08 01:19:00.792: E/AndroidRuntime(790): Caused by: java.lang.NullPointerException
05-08 01:19:00.792: E/AndroidRuntime(790): at wharehouse.QuizAndroidGUI.<init>(QuizAndroidGUI.java:36)
05-08 01:19:00.792: E/AndroidRuntime(790): at cs314.adamnick.p4.QuizTakerGUI.onCreate(QuizTakerGUI.java:38)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 01:19:00.792: E/AndroidRuntime(790): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-08 01:19:00.792: E/AndroidRuntime(790): ... 11 more
Which causes my program to crash.
Here's my class:
public class QuizTakerGUI extends Activity {
private RadioButton answer1;
private RadioButton answer2;
private RadioButton answer3;
private RadioButton answer4;
private Button submit;
private Button previous;
private Button next;
private Button finish;
private TextView questionArea;
private QuizAndroidGUI dataWharehouse;
private QuizQuestion currentQuestion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_taker);
dataWharehouse = new QuizAndroidGUI(this);
answer1 = (RadioButton) findViewById(R.id.answer1);
answer2 = (RadioButton) findViewById(R.id.answer2);
answer3 = (RadioButton) findViewById(R.id.answer3);
answer4 = (RadioButton) findViewById(R.id.answer4);
questionArea = (TextView) findViewById(R.id.quizQuestion);
submit = (Button) findViewById(R.id.submitAnswer);
previous = (Button) findViewById(R.id.prev);
next = (Button) findViewById(R.id.next);
finish = (Button) findViewById(R.id.finish);
submit.setOnClickListener(submitHandler);
previous.setOnClickListener(previousHandler);
next.setOnClickListener(nextHandler);
finish.setOnClickListener(finishHandler);
}
Here's my datawhare house class:
public QuizAndroidGUI(QuizTakerGUI app){
URL path = ClassLoader.getSystemResource("questions.csv");
this.androidApplication = app;
NUMBERofQUIZES = 10;
// since we implement quiz ui we pass in the current program
quizTaker = new QuizTaker(this);
try {
// passing in the csv file hard coded
parser = new QuestionParser(new File(path.toURI()));
} catch (FileNotFoundException e) {
parser = null;
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
quizTaker.startAndDisplayQuiz("Android Quiz", this.NUMBERofQUIZES, this.parser);
}
I think the issue maybe caused because the parser is getting set to null.
Upvotes: 0
Views: 1110
Reputation: 2658
Not enough line info but I suspect that ClassLoader.getSystemResource("questions.csv")
isn't what you really wanted, it's highly unusual to see it in android at least in my experience.
I would put the file in assets
directory and use AssetManager to read the file.
Upvotes: 0
Reputation: 11830
Put the questions.csv
file into your classpath and do this:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
URL resource = loader.getResource("myfile");
To load the file properly.
Upvotes: 1