-1

I am having an issue that I can't work out. Another set of eyes on it would be handy. I have already looked at this question Null reference object and Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference, I have checked to make sure I am doing as they say and I believe I am as the text input exists in the xml with the correct id, and I can see the declaration of the object, and there is no spelling mistake. I even get the smart popups giving me options when I place the .

I have checked over my code and it seems right to me. I am creating the variable and passing it an element that exists on the view it is referencing. Below is my code. As you can see I am creating an edit text object that has the value of the text input element -- that line seems to run, then it errors on the next line saying that getText() cannot be called on a null object reference. I am entering text before pressing the click so it should not be null. The click is happening in a fragment; I don't see that being the issue but I could be wrong.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_ping, container, false);
    Button pingButton = (Button) view.findViewById(R.id.pingButton);
    pingButton.setOnClickListener(this);

    return view;
}

public void onClick(View v) {
    switch(v.getId()) {
        case R.id.pingButton:
            EditText text = (EditText) v.findViewById(R.id.editText);
            String value = text.getText().toString();
            String s = ping(value);
            TextView result = (TextView) v.findViewById(R.id.textView);
            result.setText(s);
            break;
    }
}

I am at a bit of a loss with this now. I can't see why it is happening. What can I do to resolve this?

Traceback:

01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime: FATAL EXCEPTION: main
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime: Process: uk.aperture.pinger, PID: 10966
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at uk.aperture.pinger.Ping.onClick(Ping.java:112)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at android.view.View.performClick(View.java:5198)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-05 12:35:49.323 10966-10966/uk.aperture.pinger E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

EDIT:

For some reason this question has been marked as duplicate -- which it is not -- and then IMHO erroneously closed. This question is perfectly valid; you can see that I have both an assignment and accessing of the assignment. I believe I am passing the whole view through as shown in the extended code I posted.

Community
  • 1
  • 1
bobthemac
  • 1,172
  • 6
  • 26
  • 59
  • 1
    its a function that pings an address it is not relevant to the question really it doesn't get that far. Its the text assignment above that that errors, just wanted to give some more context than the two lines that are causing the issue. – bobthemac Jan 05 '16 at 12:54
  • That line seems to work its the line under that dosen't – bobthemac Jan 05 '16 at 12:57
  • The error is `on a null object reference` – bobthemac Jan 05 '16 at 12:59
  • Can you please clarify what you mean by all relevant code. – bobthemac Jan 05 '16 at 13:01
  • 2
    As Igw150 said, you are searching the `EditText` inside your clicked `View` - which is not a `ViewGroup` so `findViewById` won't find anything. Declare your `EditText` global and call `findViewById` in the `onViewCreated` method. – yennsarah Jan 05 '16 at 13:01
  • Its not the clicked view I think its the whole view I am using this code for clicks the answer with 400+ upvotes http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments – bobthemac Jan 05 '16 at 13:05
  • I know the difference I know how to read an error I was reading the text attached to the error as I find it more useful in debugging. `java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference` – bobthemac Jan 05 '16 at 13:08
  • Thanks to @lgw150 the issue is now fixed not got back into OO fully yet so was trying to keep global vars out. – bobthemac Jan 05 '16 at 14:01
  • And also @Amy that simplified the answer for me – bobthemac Jan 05 '16 at 14:02
  • Tangential meta thread: http://meta.stackexchange.com/questions/272418/what-to-do-about-problem-users – tripleee Jan 07 '16 at 05:16

2 Answers2

3

You have used pingButton's View to find id of EditText

EditText text = (EditText) v.findViewById(R.id.editText);

means

 EditText text = (EditText) pingButton.findViewById(R.id.editText);

use layout view to find id of child view

Why NPE?
because: EditText is not inside Botton

Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
-1

The answer to this question is due to what was mentioned in the comments by @Amy. The issue was I thought that the whole view was passed to the click function; as it turns out, it isn't. So what I did to fix this was move the declarations of the TextView and EditText to the top of the class and made them accessible throughout, and then assigned the element to them in the onCreateFunction and was able to access the data from it then. Here is my amended code excluding the declarations at the top of the class.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_ping, container, false);
        Button pingButton = (Button) view.findViewById(R.id.pingButton);
        pingText = (EditText) view.findViewById(R.id.editText);
        pingResult = (TextView) view.findViewById(R.id.textView);
        pingButton.setOnClickListener(this);

        return view;
    }

    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.pingButton:
                String value = pingText.getText().toString();
                boolean s = ping(value);
                if(s == true)
                {
                    pingResult.setText("Successful ping to " + value);
                } else {
                    pingResult.setText("Unsuccessful ping to " + value);
                }
                break;
        }
    }

Thanks to those that commented. I had no idea that the full view was not passed.

tripleee
  • 175,061
  • 34
  • 275
  • 318
bobthemac
  • 1,172
  • 6
  • 26
  • 59