mercredi 6 avril 2016

NullPointerException - Retrieving SQLite information with ArrayList

I have a method in my DB.java however I can't figure out how to get rid of NullPointerException.

I've been told it could be a constructor issue, but even if it is I'm not sure how I'd go about doing this because modules, modules1 and modules2 are declared in the constructor.

The method in DB.java is to simply select data from a database and store it in a list however it has a problem with the line: if (!student.modules.isEmpty()), I get that this means it's empty as it's a null pointer exception I just can't figure out how to fix it:

public List<tableStudents> getStudentsModules(String username) {
        List<tableStudents> studentModules = new ArrayList<tableStudents>();

        // Select All Query depending on who is logged in.      
        String selectQuery = "SELECT " + Modules1 + ", " + Modules2 + ", " + Modules + " FROM " + Table + " WHERE " + CNumber + " = \"" + username + "\"";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                tableStudents student = new tableStudents();
                if (!student.modules.isEmpty())
                {
                student.modules = cursor.getString(1);          
                studentModules.add(student);
                }
                else if (!student.modules1.isEmpty())
                {
                student.modules1 = cursor.getString(2);                 
                studentModules.add(student);
                }
                else if (!student.modules2.isEmpty())
                {
                student.modules2 = cursor.getString(3);  
                studentModules.add(student);
                }
                else 
                {
                    Log.d("Unable", "To Find Any Modules");
                }
            } while (cursor.moveToNext());
        }
        // return contact list
        return studentModules;      
    }

This is used in my MainActivity.java in the onResume() method, like so:

protected void onResume() {     
        super.onResume();
        final Intent intent = getIntent();
        //If statement triggered when user logs in only.
        if (loginSuccess == 0)
        {
        if (intent.hasExtra("loginSuccess")) {
            loginSuccess++;
            //Store session name            
            SharedPreferences preferences = getSharedPreferences("temp", getApplicationContext().MODE_PRIVATE);
            String sessionName = preferences.getString("sessionName", "");     
            boolean loginSuccess = preferences.getBoolean("loginSuccess", true);
            Toast.makeText(MainActivity.this, "Welcome " + sessionName.toString(), Toast.LENGTH_LONG).show();
            TextView loggedinas = (TextView)findViewById(R.id.loggedinas);
            loggedinas.setText(sessionName);


            //User has logged in, changing login button to logout button.
            Button login=(Button)findViewById(R.id.log);
            Button logout=(Button)findViewById(R.id.logout);
            login.setVisibility(View.GONE);
            logout.setVisibility(View.VISIBLE);
            //PASS ADMIN SESSION NAME TO HOMEPAGE, IF STATEMENT ON HOMEPAGE TO CHECK IF SESSION NAME IS ADMIN, IF ADMIN ENABLE EDIT BUTTON.
            //Set today's events            
            //getTodaysModules(sessionName);

            //Modules          
            List<tableStudents> studentModules = db.getStudentsModules(sessionName);    
            if (!studentModules.isEmpty())
            {           
            for (tableStudents session: studentModules)
            {
                //Save modules to string
                String module1 = session.modules.toString();
                String module2 = session.modules1.toString();
                String module3 = session.modules2.toString();
                //Save to shared preferences for use in module tab              
                Editor editor = preferences.edit();
                editor.putString("mod1", module1);
                editor.putString("mod2", module2);
                editor.putString("mod3", module3);
                Log.d("Modules", "Shared");
                editor.commit();

            }

          }
        }
        }
    }

LogCat

04-06 16:04:10.399: E/AndroidRuntime(6697): FATAL EXCEPTION: main
04-06 16:04:10.399: E/AndroidRuntime(6697): Process: com.example.project, PID: 6697
04-06 16:04:10.399: E/AndroidRuntime(6697): java.lang.RuntimeException: Unable to resume activity {com.example.project/com.example.project.MainActivity}: java.lang.NullPointerException
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2774)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2803)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2238)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.os.Looper.loop(Looper.java:136)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.ActivityThread.main(ActivityThread.java:5001)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at java.lang.reflect.Method.invoke(Method.java:515)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at dalvik.system.NativeStart.main(Native Method)
04-06 16:04:10.399: E/AndroidRuntime(6697): Caused by: java.lang.NullPointerException
04-06 16:04:10.399: E/AndroidRuntime(6697):     at com.example.project.DB.getStudentsModules(DB.java:226)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at com.example.project.MainActivity.onResume(MainActivity.java:117)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.Activity.performResume(Activity.java:5310)
04-06 16:04:10.399: E/AndroidRuntime(6697):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2764)
04-06 16:04:10.399: E/AndroidRuntime(6697):     ... 12 more

Aucun commentaire:

Enregistrer un commentaire