3 Replies - 315 Views - Last Post: 05 September 2017 - 07:14 PM

#1 pheonixfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-July 17

null pointer exception

Posted 03 September 2017 - 06:48 PM

G'Day,

I'm trying to work out why my app keeps throwing a null pointer exception

It gets thrown on "nextOnCorrectToggle.isChecked()", I have it declared like this in the
class declaration "ToggleButton nextOnCorrectToggle = null;

Below are the functions to assign buttons and use their values

void getFields()
    {
        questionTextView = (TextView)findViewById(R.id.questionTextView);
        pronunciationTextView = (TextView) findViewById(R.id.pronunciationTextView);
        answerButton1 = (Button) findViewById(R.id.answer1Button);
        answerButton2 = (Button) findViewById(R.id.answer2Button);
        answerButton3 = (Button) findViewById(R.id.answer3Button);
        answerButton4 = (Button) findViewById(R.id.answer4Button);
        getNextQuestionButton = (Button) findViewById(R.id.nextQuestionButton);
        correctSwitch = (Switch) findViewById(R.id.correctSwitch);
        correctAnswerTextView = (TextView) findViewById(R.id.trackCorrectTextView);
        trackCorrectSwitch = (Switch) findViewById(R.id.trackCorrectSwitch);
        trackCorrectToggle = (ToggleButton) findViewById(R.id.trackCorrectToggle);
        nextOnCorrectToggle = (ToggleButton) findViewById(R.id.nextCorrectToggle);
    }

private void checkAnswer (String userAnswer)
    {
        if (userAnswer.equals(questionAnswer))
        {
            Toast toast = Toast.makeText(context, "Correct", Toast.LENGTH_SHORT);
            toast.show();
            if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE)
            {
                if (trackCorrectSwitch.isChecked())
                {
                    incCorrect();
                }
                if (correctSwitch.isChecked())
                {
                    nextQuestion();
                }
            }
            else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL)
            {
                if (trackCorrectToggle.isChecked())
                {
                    incCorrect();
                }
                if (nextOnCorrectToggle.isChecked())
                {
                    nextQuestion();
                }
            }

        }
        else
        {
            Toast toast = Toast.makeText(context, "Incorrect", Toast.LENGTH_SHORT);
            toast.show();
            resetCorrect();
        }
    }



Error log
09-04 11:44:16.844 7251-7251/memory_aid.memoryaid E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: memory_aid.memoryaid, PID: 7251
                                                                    java.lang.NullPointerException
                                                                        at memory_aid.memoryaid.questionActivityA.checkAnswer(questionActivityA.java:170)
                                                                        at memory_aid.memoryaid.questionActivityA.access$000(questionActivityA.java:28)
                                                                        at memory_aid.memoryaid.questionActivityA$4.onclick(questionActivityA.java:89)
                                                                        at android.view.View.performClick(View.java:4445)
                                                                        at android.view.View$PerformClick.run(View.java:18446)
                                                                        at android.os.Handler.handleCallback(Handler.java:733)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:136)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5146)
                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:515)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
                                                                        at dalvik.system.NativeStart.main(Native Method)



If somebody could bring a fresh perspective to this to help me solve this problem
it would be appreciated

If more information is required please don't hesitate to ask

Any help is apppreciated
pheonixfire

Is This A Good Question/Topic? 0
  • +

Replies To: null pointer exception

#2 wseng92  Icon User is online

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 280
  • Joined: 23-September 15

Re: null pointer exception

Posted 04 September 2017 - 07:31 AM

Quote

I have it declared like this in the
class declaration "ToggleButton nextOnCorrectToggle = null;

Why you need to declare like this? At where you declare it ?
Was This Post Helpful? 0
  • +
  • -

#3 pheonixfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-July 17

Re: null pointer exception

Posted 05 September 2017 - 07:01 PM

I declared it like "ToggleButton nextOnCorrectToggle = null;" right under the following so
it would be available globally

public class questionActivityA extends AppCompatActivity
{



It's actual value is assigned to it in the getFields function, since it's my understanding that
you can't call functions in the public class section (I could be wrong)

any help is appreciated
pheonixfire
Was This Post Helpful? 0
  • +
  • -

#4 pheonixfire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 26-July 17

Re: null pointer exception

Posted 05 September 2017 - 07:14 PM

Thanks for taking the time to reply, turned out to be a UI object naming error

best regards
pheonixfire
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1