0

I've successfully passed a value from a static class to a non-static one, but I got an error, null value, when I put that value to an EditText.

 public class HelloBubblesActivity extends SherlockFragmentActivity { public EditText editText1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_discuss); editText1 = (EditText) findViewById(R.id.editText1); } public static class MyDialogFragment extends SherlockDialogFragment { //i fill variable emotx is "test string" public void emot(String emotx){ HelloBubblesActivity hb=new HelloBubblesActivity(); hb.smiley(emotx); //send value to smiley method.. } } public void smiley(String name){ Log.d("test", name); //result value is "test string" (success) editText1.setText(name); //here is error } } 

I'm not sure why I'm getting this issue. Can anyone see why this is not doing what is expected?

9
  • (And why are you doing toString() on a String?) Commented Jan 27, 2013 at 2:23
  • editText1 is a EditText.. Commented Jan 27, 2013 at 3:01
  • Not seeing enough code here to get what's going on. Add if(editText1 == null) Log.d("editText1", "null"); right before the editText1.setText line and post the logcat including that log if it shows up. Commented Jan 27, 2013 at 3:27
  • the value has been succes stored to "name" variable.. but when i insert that value to editText i got error..in log show "java.lang.NullPointerException"?? Commented Jan 27, 2013 at 3:43
  • You didn't answer my question -- how/where is editText1 declared, and where/how is it initialized? Commented Jan 27, 2013 at 3:51

1 Answer 1

1
HelloBubblesActivity hb=new HelloBubblesActivity(); hb.smiley(emotx); //send value to smiley method.. 

Creates a new HelloBubblesActivity object but doesn't call onCreate and hence does not assign anything to editText1. Fields that have never been assigned are null in Java.

(Of course, to say the code is not doing what is "expected" would be wrong -- it's doing exactly what one would expect from the scenario.)

(Your mistake is most likely in thinking that setting a value in ANY HelloBubblesActivity object makes the value appear in ALL HelloBubblesActivity objects. Individual objects do not share instance fields, and what you set in one object does not magically appear in another. You can't just create an object of some class and expect it to have some sort of paranormal communications with others of it's class.)

(But this is a common mistake made by those who are dumped into OOP without a good background in assembler programming, et al.)

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.