Reputation: 8421
Sorry i am new in android
I have 2 EditTexts and 1 button i am going to when i push the button the value of EditTexts be printed, but i give the error
The XML:
<Button
android:id="@+id/submit"
android:layout_width="212dp"
android:layout_height="wrap_content"
android:layout_x="52dp"
android:layout_y="337dp"
android:text="@string/Submit"
android:onClick="submit" />
<EditText
android:id="@+id/quantity"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:layout_x="9dp"
android:layout_y="86dp"
android:ems="10" />
<EditText
android:id="@+id/Unit"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:layout_x="11dp"
android:layout_y="15dp"
android:ems="10" >
<requestFocus />
</EditText>
The Java class is:
public class add extends Activity
{
private static String[] montharray;
Button mButton;
EditText Unit;
EditText quantity;
private int days;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
Unit =(EditText) findViewById(R.id.unit);
quantity =(EditText) findViewById(R.id.quantity);
mButton =(Button) findViewById(R.id.submit);
}
public void submit(View view)
{
System.out.println("Unit= "+Unit.getText().toString()+" quantity= "+quantity.getText().toString());
}
}
Here is thr errors:
05-16 15:37:35.857: E/AndroidRuntime(17951): FATAL EXCEPTION: main
05-16 15:37:35.857: E/AndroidRuntime(17951): java.lang.IllegalStateException: Could not execute method of the activity
...
05-16 15:37:35.857: E/AndroidRuntime(17951): Caused by: java.lang.NullPointerException
05-16 15:37:35.857: E/AndroidRuntime(17951): at net.learn2develop.UsingIntent.add.submit(add.java:56)
I initiate all the objects, what is the java.lang.NullPointerException?
Upvotes: 0
Views: 307
Reputation: 44571
Unit
is capitalized here
<EditText
android:id="@+id/Unit"
android:layout_width="182dp"
android:layout_height="wrap_content"
android:layout_x="11dp"
android:layout_y="15dp"
android:ems="10" >
but not in your code
Unit =(EditText) findViewById(R.id.unit);
You should change it in your xml to lowercase. Also, you should follow a standard for naming conventions. Class names should be camelcased (All words start with capital letter) and variable names should be mixedcase (start with lowercase and capitalize following words i.e String mixedCase;
) This isn't necessary but it will most likely cause you grief in the future. It also makes it easier for others to understand what you have.
Upvotes: 0
Reputation: 6159
the id's are case sensitive...
you should do: findViewById(R.id.Unit);
Upvotes: 1