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.