I’ve just started writing my first app.
I have EditText field in which i want to get it’s content (text)…
I’ve created a new class with several fields, one of them is the target for the EditText field content.
If i create a local String var and writes :(When name is the local var.)
name=(order_name.getText().toString());
Everything works just fine.
But when i try to use a class I’ve created which contains a public String field :
order.setName(order_name.getText().toString());
I get an FC.
I also tried direct insertion from the value to the field, and when that crashed i tried using set function as shown.
The class was firstly created using Eclipse new class wizard, after that i recreated it using AndroidManifest.xml wizard.
Log :
06-01 17:06:42.104: E/AndroidRuntime(1190): FATAL EXCEPTION: main<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): java.lang.NullPointerException<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at com.Sagi.MyOrders.new_order_activity$1.onClick(new_order_activity.java:46)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at android.view.View.performClick(View.java:2408)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at android.view.View$PerformClick.run(View.java:8816)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at android.os.Handler.handleCallback(Handler.java:587)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at android.os.Handler.dispatchMessage(Handler.java:92)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at android.os.Looper.loop(Looper.java:123)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at android.app.ActivityThread.main(ActivityThread.java:4627)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at java.lang.reflect.Method.invokeNative(Native Method)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at java.lang.reflect.Method.invoke(Method.java:521)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)<br>
06-01 17:06:42.104: E/AndroidRuntime(1190): at dalvik.system.NativeStart.main(Native Method)
Thanks guys !
Code :
public class new_order_activity extends Activity {
DatabaseHelper DBHelper;
// LAYOUT VARS
Button saveButton,clearButton;
EditText order_name,link,price;
CheckBox paid,alert;
DatePicker date;
// LOCAL VARS
Order order;
String name1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_order_layout);
date = (DatePicker)findViewById(R.id.order_date_field);
alert = (CheckBox)findViewById(R.id.alert_checkbox);
paid = (CheckBox)findViewById(R.id.paid_checkbox);
price = (EditText)findViewById(R.id.price_field);
order_name = (EditText)findViewById(R.id.order_name_field);
link = (EditText)findViewById(R.id.link_field);
saveButton = (Button) findViewById(R.id.save_button);
clearButton = (Button) findViewById(R.id.clear_button);
DBHelper=new DatabaseHelper(this);
saveButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
Line46: **order.setName(order_name.getText().toString());**
order.link=link.getText().toString();
order.price=price.getText().toString();
order.paid=paid.isChecked();
order.alert=alert.isChecked();
order.year=date.getYear();
order.month=date.getMonth();
order.day=date.getDayOfMonth();
submitOrder(order);
}
});
When you try to run
setNameon yourordervariable it isn’t instantiated yet, so it throws an exception because it doesn’t have an actual object to call the method on. You’ll need to instantiate it (call its constructor in whatever way you wish) in your onCreate method so that it is created with the Activity (unless you want to create a neworderobject every time you click – then instantiate it in your onClickListener function, but make sure it is before you try to set its name).