0

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.

12
  • You got the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in the manifest? Commented May 8, 2013 at 7:28
  • Please highlight the QuizAndroidGUI.java:36 line. Commented May 8, 2013 at 7:29
  • ClassLoader.getSystemResource ? What's the purpose? Commented May 8, 2013 at 7:29
  • Where is "questions.csv" stored? in Assets? If so you should be accessing it through InputStream is = getAssets().open(filename); Commented May 8, 2013 at 7:30
  • 3
    Why don't you just debug your app? Commented May 8, 2013 at 7:30

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How do I put into my class path?
You have several ways to do that. Are you using eclipse?
Then just drag and drop the file into your project. You should be able to load it correctly then. Take a look at the relative path the file is given into eclipse.
0

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.

2 Comments

Is there a way to use asset manager in a file that is not an android app? I have an application that is non-android that is written in Java that must take an argument of a file to return to me quiz questions.
Unfortunately no, but you create an interface for reading and decide which implementation to use based on which platform you're on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.