Results 1 to 10 of 10

Thread: A bit of Java help please :)

777
  1. [translate]    #1
    Senior Member
    Join Date
    Jul 2010
    Location
    Manchester,UK
    Posts
    133

    Default A bit of Java help please :)

    Hi guys Im trying to get into a bit of java dev in android, Ive only done a small amount of java before but have found some really good tutorials on Open Source Training - Marakana
    Im making there micro bloging app from the android bootcamp series and have got stuck with a runtime error. Its a null pointer exception in the code that references how to post to twitter, i think my code is the same as in the video but im getting an error so any help would be appreciated.
    StatusActivity.java (main activity and where the error is happening)
    Code:
    package com.Yamba.fooforever;
    
    import winterwell.jtwitter.TwitterException;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class StatusActivity extends Activity implements OnClickListener {
    	EditText editStatus;
    	Button buttonUpdate;
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    
    		// Debug.startMethodTracing("yamba.trace");
    
    		setContentView(R.layout.status);
    
    		editStatus = (EditText) findViewById(R.id.editStatus);
    		buttonUpdate = (Button) findViewById(R.id.buttonUpdate);
    
    		buttonUpdate.setOnClickListener(this);
    
    	}
    
    	@Override
    	protected void onStop() {
    		super.onStop();
    
    		// Debug.stopMethodTracing();
    	}
    
    	public void onClick(View v) {
    		String status = editStatus.getText().toString();
    		new PostToTwitter().execute(status);
    
    		Log.d("statusActivity", "on click'd with status: " + status);
    	}
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		getMenuInflater().inflate(R.menu.menu, menu);
    		return true;
    	}
    
    	@Override
    	public boolean onOptionsItemSelected(MenuItem item) {
    		switch (item.getItemId()) {
    		case R.id.itemPrefs:
    			startActivity(new Intent(this, PrefsActivity.class));
    
    			break;
    
    		}
    		return true;
    	}
    
    	private class PostToTwitter extends AsyncTask<String, String, String> {
    		@Override
    		protected String doInBackground(String... status) {
    			String result;
    			// Update Status Online
    			try {
    			YambaApplication app = ((YambaApplication) StatusActivity.this
    					.getApplication());
    ERROR HAPPENS HERE ->app.getTwitter().setStatus(status[0]);
    				result = StatusActivity.this
    						.getString(R.string.toastSuccessful);
    				return null;
    			} catch (TwitterException e) {
    				e.printStackTrace();
    				result = StatusActivity.this
    						.getString(R.string.toastUnsuccessful);
    			}
    			return result;
    		}
    
    		@Override
    		protected void onPostExecute(String result) {
    			super.onPostExecute(result);
    			Toast.makeText(StatusActivity.this, result, Toast.LENGTH_LONG)
    					.show();
    		}
    
    	}
    }
    YambaApplication.java (application class where the referenced code is)
    Code:
    package com.Yamba.fooforever;
    
    import winterwell.jtwitter.Twitter;
    import android.app.Application;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
    import android.preference.PreferenceManager;
    
    public class YambaApplication extends Application {
    	SharedPreferences prefs;
    	private Twitter twitter = null;
    
    	@Override
    	public void onCreate() {
    		super.onCreate();
    
    		prefs = PreferenceManager.getDefaultSharedPreferences(this);
    	}
    
    REFERENCED CODE ->
    	public synchronized Twitter getTwitter(){
    		if (twitter == null) {
    			String username = prefs.getString("username", "");
    			String password = prefs.getString("password", "");
    			String api = prefs.getString("api", "");
    			twitter = new Twitter(username, password);
    			twitter.setAPIRootUrl(api);
    		}
    		return null;
    	}
    
    
    }
    Thanks for any help.
    Tomq3's CyanogenMod 7.4 Nightly
    Running quick and much better than froyo

  2. [translate]    #2
    Member
    Join Date
    Mar 2010
    Posts
    44

    Default

    read the exact line where your error is happening

  3. [translate]    #3
    Senior Member
    Join Date
    Jul 2010
    Location
    Manchester,UK
    Posts
    133

    Default

    Theres an arrow in the code :

    ERROR HAPPENS HERE ->app.getTwitter().setStatus(status[0]);
    found it via the debugger/adb
    Tomq3's CyanogenMod 7.4 Nightly
    Running quick and much better than froyo

  4. [translate]    #4
    Senior Member
    Join Date
    Aug 2010
    Location
    Holland
    Posts
    1,115

    Default

    Quote Originally Posted by fooforever View Post
    Theres an arrow in the code :

    ERROR HAPPENS HERE ->app.getTwitter().setStatus(status[0]);
    found it via the debugger/adb
    do you dev with eclipse?
    i reccomend that for java.
    If you have a nullpointer in that line, either 'app' is not initialised, or the 'status' array.
    Please give the actualy error.
    CyanogenMod-7.1-NIGHTLY-Spica-alpha7.2.4
    And proud of it.

  5. [translate]    #5
    Member
    Join Date
    Mar 2010
    Posts
    44

    Default

    ja i also asked for the actual error. i also like jcreator! willhelp tommorow its still party time in SA

  6. [translate]    #6
    Senior Member
    Join Date
    Jul 2010
    Location
    Manchester,UK
    Posts
    133

    Default

    sorry misunderstood, yes im using eclipse with ADT the exact error im getting is :
    Code:
    E/AndroidRuntime(  366): Caused by: java.lang.NullPointerException
    E/AndroidRuntime(  366):        at com.Yamba.fooforever.StatusActivity$PostToTwitter.doInBackground(StatusActivity.java:77)
    E/AndroidRuntime(  366):        at com.Yamba.fooforever.StatusActivity$PostToTwitter.doInBackground(StatusActivity.java:1)
    E/AndroidRuntime(  366):        at android.os.AsyncTask$2.call(AsyncTask.java:185)
    E/AndroidRuntime(  366):        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    E/AndroidRuntime(  366):        ... 4 more
    W/ActivityManager(   70):   Force finishing activity com.Yamba.fooforever/.StatusActivity
    Now its the one that points to line 77 of status activity thats making it FC, i think everything is initialised the app class is in the manifest xml the app variable is defined the line above the error and status is defined in onclick method of status activity, thats why i cant figure out whats wrong.

    thanks for your help guys
    Tomq3's CyanogenMod 7.4 Nightly
    Running quick and much better than froyo

  7. [translate]    #7
    Senior Member
    Join Date
    Aug 2010
    Location
    Holland
    Posts
    1,115

    Default

    - those '4more' is not expandable?
    - and the 'StatusActivity$PostToTwitter'-activity is defined in the manifest as well?
    CyanogenMod-7.1-NIGHTLY-Spica-alpha7.2.4
    And proud of it.

  8. [translate]    #8
    Senior Member
    Join Date
    Jul 2010
    Location
    Manchester,UK
    Posts
    133

    Default

    No i cant seem to find anymore info and 'StatusActivity$PostToTwitter' is a method with the activity statusactivity so it doesnt need to be does it?
    Tomq3's CyanogenMod 7.4 Nightly
    Running quick and much better than froyo

  9. [translate]    #9
    Senior Member
    Join Date
    Aug 2010
    Location
    Holland
    Posts
    1,115

    Default

    Quote Originally Posted by fooforever View Post
    No i cant seem to find anymore info and 'StatusActivity$PostToTwitter' is a method with the activity statusactivity so it doesnt need to be does it?
    ah no, then it doesnt.
    Pff... idk, what i normaly do when debugging an app, is a lot of 'System.out.prinln()'
    CyanogenMod-7.1-NIGHTLY-Spica-alpha7.2.4
    And proud of it.

  10. [translate]    #10
    Member
    Join Date
    Mar 2010
    Posts
    44

    Default

    check first that when you define yamba as app that it is done correct. Maybe call another method out of the yamba class

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •