As Faber Fedor already said in his post, it's because you set the view before using setContentView.You can't find any Views before you have loaded the layout/view from xml.
Whoever, the code example also indicates that there is much more wrong than the setContentView placed after .findViewById. First off, why are you instantiating a EditView called billAmt, when you also search it via findViewById? Basicly these lines EditText billAmt = new EditText(this); EditText precentage = new EditText(this); TextView Ans = new TextView(this); are never used anyways. You have two options here. Either, create your Views inside the res \layout\main.xml, i.e. <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:id="@+id/billAmt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="44sp" /> <EditText android:id="@+id/percentage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="44sp" /> <TextView android:id="@+id/Ans" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="44sp" /> </LinearLayout> and then use the findViewById to assign it, i.e. EditText billAmt = (EditText)findViewById(R.id.billAmt); EditText percentage = (EditText)findViewById(R.id.percentage); TextView Ans = (TextView)findViewById(R.id.Ans); and then do the calculation billNum = Double.parseDouble(billAmt.getText()); preNum = Double.parseDouble(percentage.getText()); Ans.SetText(""+billNum * (preNum/100)); However, the next problem is, you're placing all your code inside the "onCreate" method. This one is executed as soon as the Activity is started, this also means that the your application try to do a caclulation, before the user even had a chance to insert any data. In this case the billAmt and percentage EditText's are most probably empty. And you can't convert an empty string into a double neither you can convert a string (if the user enters a string there). So you'd also need to catch the critical parts in a try/catch block and catch a possible exception (if a empty string or non-nuberical string was attempted to be converted to Double). Also it's a good idea to declare the TextViews outside the onCreate function, so once you assigned it you can access them from anywhere inside the activity and w/o having to call findViewById everytime (it's a quite costly operation and using it too much would make the Android Phones battery running low much faster, cause more CPU time is needed, especially if there are many Views in the Layout file). public class ItemEdit extends Activity { EditText billAmt; EditText percentage; TextView Ans; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); billAmt = (EditText)findViewById(R.id.billAmt); ... } } Also try avoid things like this: billNum = Double.parseDouble(((EditText)findViewById (R.id.BillAmt)).getText().toString()); It's quite for read for anyone who is trying to help you or later maintain your code. For me it seems you haven't ever worked with Java before, have you? Maybe start with some Java basics before attempting to write an Android application. On Jan 14, 1:28 am, "Eddie O'Hagan" <eddieoha...@optonline.net> wrote: > When I try to run the following code My program eather crashes or > throws a null pointer exception in the LogCat. > > public class The_Perfect_Tip extends Activity { > /** Called when the activity is first created. */ > @Override > public void onCreate(Bundle savedInstanceState) > { > super.onCreate(savedInstanceState); > > EditText billAmt = new EditText(this); > EditText precentage = new EditText(this); > TextView Ans = new TextView(this); > double billNum = 0; > double preNum = 0; > > billNum = Double.parseDouble(((EditText)findViewById > (R.id.BillAmt)).getText().toString()); > > preNum = Double.parseDouble(((EditText)findViewById > (R.id.Precentage)).getText().toString()); > > ((TextView)findViewById(R.id.Ans)).setText(""+billNum * > (preNum / > 100)); > > setContentView(R.layout.main); > } > > } > > Why is there a null pointer excetpion being thrown? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Beginners" group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~----------~----~----~----~------~----~------~--~---