Null Pointer Exception in android when switching activities?

It's line 34 in Main. The lines under "Caused by" in the logcat output are where the relevant info usually is Okay, culprit should be the initializers Intent I = getIntent(); final String serviceType = i. GetStringExtra("serviceType"); final EditText caller = (EditText) findViewById(R.id.

CallerEnter), callExt = (EditText) findViewById(R.id. CallNumberEnter), computerName = (EditText) findViewById(R.id. ComputerNameEnter), location = (EditText) findViewById(R.id.

LocationEnter), request = (EditText) findViewById(R.id. RequestEnter) occurring in the Object constructor. Since the onClickListener constructor will run before the onCreate() method getIntent() will return null, and you will not have set your contentView so all your findViewById()'s will also return null The correct way would be: private OnClickListener buttonListener = new OnClickListener() { ArrayList data = new ArrayList(5); public void onClick(View v) { Intent I = getIntent(); final String serviceType = i.

GetStringExtra("serviceType"); final EditText caller = (EditText) findViewById(R.id. CallerEnter), callExt = (EditText) findViewById(R.id. CallNumberEnter), computerName = (EditText) findViewById(R.id.

ComputerNameEnter), location = (EditText) findViewById(R.id. LocationEnter), request = (EditText) findViewById(R.id. RequestEnter); Intent intent = new Intent(Main.

This, HelpDeskEnd. Class); final String email = {"target emails"}; data. Add(new BasicNameValuePair("Caller: ", textToString(caller))); data.

Add(new BasicNameValuePair("Call Ext: ", textToString(callExt))); data. Add(new BasicNameValuePair("Computer Name: ", textToString(computerName))); data. Add(new BasicNameValuePair("Locations: ", textToString(location))); data.

Add(new BasicNameValuePair("Request: ", textToString(request))); sendData(data); String body = data. Get(0).toString() + " " + data. Get(1).toString() + " " + data.

Get(2).toString() + " " + data. Get(3).toString() + " " + data. Get(4).toString(); Mail mail = new Mail("an email", "email password"); mail.

SetTo(email); mail. SetBody(body); mail. SetFrom("an email"); mail.

SetSubject(serviceType); try { mail.send(); } catch (Exception e) { Log. E("Send Mail", e.getMessage(), e); } intent. PutExtra("serivceType", serviceType); startActivity(intent); } }.

It's line 34 in Main. The lines under "Caused by" in the logcat output are where the relevant info usually is. Okay, culprit should be the initializers Intent I = getIntent(); final String serviceType = i.

GetStringExtra("serviceType"); final EditText caller = (EditText) findViewById(R.id. CallerEnter), callExt = (EditText) findViewById(R.id. CallNumberEnter), computerName = (EditText) findViewById(R.id.

ComputerNameEnter), location = (EditText) findViewById(R.id. LocationEnter), request = (EditText) findViewById(R.id. RequestEnter); occurring in the Object constructor.

Since the onClickListener constructor will run before the onCreate() method, getIntent() will return null, and you will not have set your contentView so all your findViewById()'s will also return null. The correct way would be: private OnClickListener buttonListener = new OnClickListener() { ArrayList data = new ArrayList(5); public void onClick(View v) { Intent I = getIntent(); final String serviceType = i. GetStringExtra("serviceType"); final EditText caller = (EditText) findViewById(R.id.

CallerEnter), callExt = (EditText) findViewById(R.id. CallNumberEnter), computerName = (EditText) findViewById(R.id. ComputerNameEnter), location = (EditText) findViewById(R.id.

LocationEnter), request = (EditText) findViewById(R.id. RequestEnter); Intent intent = new Intent(Main. This, HelpDeskEnd.

Class); final String email = {"target emails"}; data. Add(new BasicNameValuePair("Caller: ", textToString(caller))); data. Add(new BasicNameValuePair("Call Ext: ", textToString(callExt))); data.

Add(new BasicNameValuePair("Computer Name: ", textToString(computerName))); data. Add(new BasicNameValuePair("Locations: ", textToString(location))); data. Add(new BasicNameValuePair("Request: ", textToString(request))); sendData(data); String body = data.

Get(0).toString() + " " + data. Get(1).toString() + " " + data. Get(2).toString() + " " + data.

Get(3).toString() + " " + data. Get(4).toString(); Mail mail = new Mail("an email", "email password"); mail. SetTo(email); mail.

SetBody(body); mail. SetFrom("an email"); mail. SetSubject(serviceType); try { mail.send(); } catch (Exception e) { Log.

E("Send Mail", e.getMessage(), e); } intent. PutExtra("serivceType", serviceType); startActivity(intent); } }.

That is what I thought, line 34 is: final String serviceType = i. GetStringExtra("serviceType"); If I comment this out and just add a serviceType variable down in the onClickListener and set it to null I still get the same error but with line 36 which is: final EditText caller = (EditText) findViewById(R.id. CallerEnter) Is there something wrong with my R.

Java file or something? – Mike Eaton Jul 13 at 18:15 I is null because the getIntent() will return null since the onClickListener is instantiated before onCreate(). Put the getIntent() and dependent variable instantiations in the onClick() – jqpubliq Jul 13 at 18:21 All of the findViewById()'s also need to be moved into the onclick.

They will not work where they are. OnCreate() will not have been called so you have not added your contentView. – jqpubliq Jul 13 at 18:32 Thanks so much for the help Finally got it to work and learned some more about how this onCreate() works.

– Mike Eaton Jul 13 at 18:42.

I. PutExtra("serviceType", serviceType); startActivity(i); That just gives me an error. Doesn't let the porgram run – Mike Eaton Jul 13 at 17:49.

Private OnClickListener buttonListener = new OnClickListener() { public void onClick(View v) { String serviceType = null; Intent I = new Intent(HelpDeskFront. This, Main. Class); switch (v.getId()) { case R.id.

ServiceButton : serviceType = "service"; break; case R.id. OrderButton : serviceType = "order"; break; case R.id. ProgrammingButton : serviceType = "programming"; break; } i.

PutExtra("serviceType", serviceType); startActivity(i);// change this to startActivityForResult(yourintent,Intent. FILL_IN_DATA) } }; Just a thought at first glance.

Still getting the same error – Mike Eaton Jul 13 at 17:58.

Try to use if(view==programmingButton)else if(view==another button) like this instead of switch,can you post the logcat and tell which code(as part of onclick listener or second activity) to null pointer exception...

The error is on line 34 final String serviceType = i. GetStringExtra("serviceType"); which means that "i" is probably not initialized properly. I would try this so that the contexts are right.

Intent I = Main.this.getIntent(); Or you could just create a new intent like you did in the other activity.

This fixed the NullPointerException that I was getting on line 34 so thanks! Now im getting a different error when doing same thing. Will post LogCat and edit the code changes I made – Mike Eaton Jul 13 at 18:27.

You have a lot of issues with how your app is even starting off in its main activity! You need to set the listeners in the onStart() method not in the onCreate() method. OnCreate() isn't always called every time the activity is shown.

You don't need to find out what button has been clicked using a switch, if/else, or anything so complex... each button has its own onClickListener(). In the listener call a private method to start the new activity. Pass whatever data you want through the intent using putExtra().

Here is an example... public class MainActivity extends Activity { @Override public void onCreate( final Bundle savedInstanceState ) { super. OnCreate( savedInstanceState ); this. SetContentView( R.layout.

Main ); } @Override protected void onStart() { super.onStart(); final Button _serviceButton = (Button)this. FindViewById( R.id. ButtonService ); final Button _orderButton = (Button)this.

FindViewById( R.id. ButtonOrder ); final Button _progButton = (Button)this. FindViewById( R.id.

ButtonProgramming ); _serviceButton. SetOnClickListener( new OnClickListener() { @Override public void onClick( final View v ) { MainActivity.this. DoSomeButtonClick( "service" ); } } ); _orderButton.

SetOnClickListener( new OnClickListener() { @Override public void onClick( final View v ) { MainActivity.this. DoSomeButtonClick( "order" ); } } ); _progButton. SetOnClickListener( new OnClickListener() { @Override public void onClick( final View v ) { MainActivity.this.

DoSomeButtonClick( "programming" ); } } ); } private void doSomeButtonClick( final String type ) { final Intent intent = new Intent( MainActivity. This, Activity1. Class ); intent.

PutExtra( "serviceType", type ); this. StartActivity( intent ); } }.

See I didn't even know that the OnStart() method existed. I followed one short tutorial on making some text appear on the screen and dove right into this app. Thanks so much; I just learned a lot.

I guess spending a year learning java does not an android developer make – Mike Eaton Jul 13 at 19:02 You add @Override decelerations before a lot of your methods. When I do this I always get errors. Are they that important or am I doing something wrong?

– Mike Eaton Jul 13 at 19:05 HAHA Yeah the whole "Android way" is interesting! I didn't look but I briefly noted you are making an HTTP request. I will STRONGLY suggest, if you are not already doing so, use an AsyncTask and run your request on a separate thread.

– DDoSAttack Jul 13 at 19:07 The @Override are the overridden methods of the Activity class. Developer.android. Com/guide/topics/fundamentals/activities.

Html – DDoSAttack Jul 13 at 19:08 btw- I'm saying all of this, especially about how you've written your intents and what-not, because your current error is only the tip of the iceburg! You are going to have a couple of more errors including an ANR when you make your HTTP connection after "correcting" the immediate NullPointerException. – DDoSAttack Jul 13 at 19:13.

So I am having problem I just can't get my head around in Android. My program uses the Application class for storing globals. Quite often in a deployed app (but never can reproduce myself!), it is falling into the "What do I do" portion of the code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions