Results 1 to 6 of 6
  1. #1
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default Starting new activity through Options menu (null pointer?)

    Hello All!

    Sorry to bother, but I have been learning how to program on the android operating system, going through tutorials and such, and decided to make my own feature rich application. I have been using the taskreminder application from android for dummies as a template, but I have run into an issue that has been... tasking me! HAHA And, I was hoping someone would have an opportunity to assist with this.

    The goal is to have an activity, MillParameterList, which displays a list of rows stored in an sqlite database. To add an item to the database I have a separate activity, EditMillParamList. It is accessed though an options menu.

    Now, I have done this while going through the Android for Dummies tutorials, but for some reason, it is giving me a null pointer exception when I am trying to put this into practice for myself. At least I believe it is a null pointer exception. Here is the errors from LogCat (using Eclipse)

    09-18 04:27:11.066: W/KeyCharacterMap(425): No keyboard for id 0
    09-18 04:27:11.066: W/KeyCharacterMap(425): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

    09-18 04:27:12.746: D/AndroidRuntime(425): Shutting down VM
    09-18 04:27:12.756: W/dalvikvm(425): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
    09-18 04:27:12.756: E/AndroidRuntime(425): FATAL EXCEPTION: main
    09-18 04:27:12.756: E/AndroidRuntime(425): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.Sendzimir.RollGap/com.Sendzimir.RollGap.EditMillParamList}: java.lang.NullPointerException
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2585)
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2679)
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.ActivityThread.access$2300(ActivityThr ead.java:125)
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.ActivityThread$H.handleMessage(Activit yThread.java:2033)
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.os.Handler.dispatchMessage(Handler.java:99 )
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.os.Looper.loop(Looper.java:123)
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.ActivityThread.main(ActivityThread.jav a:4627)
    09-18 04:27:12.756: E/AndroidRuntime(425): at java.lang.reflect.Method.invokeNative(Native Method)
    09-18 04:27:12.756: E/AndroidRuntime(425): at java.lang.reflect.Method.invoke(Method.java:521)
    09-18 04:27:12.756: E/AndroidRuntime(425): at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:868)
    09-18 04:27:12.756: E/AndroidRuntime(425): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626)
    09-18 04:27:12.756: E/AndroidRuntime(425): at dalvik.system.NativeStart.main(Native Method)
    09-18 04:27:12.756: E/AndroidRuntime(425): Caused by: java.lang.NullPointerException
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.Activity.findViewById(Activity.java:16 37)
    09-18 04:27:12.756: E/AndroidRuntime(425): at com.Sendzimir.RollGap.EditMillParamList.<init>(Edi tMillParamList.java:16)
    09-18 04:27:12.756: E/AndroidRuntime(425): at java.lang.Class.newInstanceImpl(Native Method)
    09-18 04:27:12.756: E/AndroidRuntime(425): at java.lang.Class.newInstance(Class.java:1429)
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.Instrumentation.newActivity(Instrument ation.java:1021)
    09-18 04:27:12.756: E/AndroidRuntime(425): at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2577)
    09-18 04:27:12.756: E/AndroidRuntime(425): ... 11 more
    if I am reading it correctly, the fatal exception occurs when trying to open EditMillParamlist.

    Here is the code I am trying to execute on MillParameterList.

    Java Code:
    package com.Sendzimir.RollGap;
    
    import android.app.ListActivity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.ContextMenu;
    import android.view.ContextMenu.ContextMenuInfo;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView.AdapterContextMenuInfo;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;
    
    public class MillParameterList extends ListActivity {
    	
    	private RollGapDBAdapter mDbHelper;
    	private static final int ACTIVITY_CREATE = 0;
    	private static final int ACTIVITY_EDIT = 1;
    	
    	/** called when activity is created */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    	    setContentView(R.layout.mill_param_list);
    	    mDbHelper = new RollGapDBAdapter(this);
    	    mDbHelper.open();
    	    fillData();
    	    registerForContextMenu(getListView());
    	}
    
    //adds option menu for adding a param
    //added menu folder to res folder, created insert_millparam.xml for this method
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		super.onCreateOptionsMenu(menu);
    		MenuInflater mi = getMenuInflater();
    		mi.inflate(R.menu.insert_millparam, menu);
    		return true;
    	}
    
    //if item in option menu was clicked perform action
    //only option is add a mill param.  so only one item in the switch
    	@Override
    	public boolean onMenuItemSelected(int featureId, MenuItem item) {
    		switch(item.getItemId()) {
    			case R.id.insert_millparam:
    				createMillParam();
    				return true;
    		
    		}
    		return super.onMenuItemSelected(featureId,  item);
    	}
    //once add mill param is clicked from option menu, move to EditMillParamList 
    //to create/not edit a mill parameter. 
    //expect to return a result, so that we can repopulate the mill parameter list
    
    	private void createMillParam() {
            Intent i = new Intent(this, EditMillParamList.class);
            startActivityForResult(i, ACTIVITY_CREATE);
    	}
    
    //After EditMillParamList is complete it will return values this class is waiting for
    //this will receive the date and repopulate the list
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    		super.onActivityResult(requestCode,  resultCode, intent);
    		fillData();
    	}
    }
    I removed fillData() and some other stuff that was extending the length of all this. Stuff like what to do when long clicking and short clicking on an individual item that appears on this activity. I dont know if I am giving all the information needed, but I think I am.

    note: I checked the manifest to be sure that both this and EditMillParamList exists, and it is there. So, that shouldnt be the issue.

    Thank you for reading this!
    WillRiker

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: Starting new activity through Options menu (null pointer?)

    The NPE stack trace shows
    Caused by: java.lang.NullPointerException
    at com.Sendzimir.RollGap.EditMillParamList.<init>(Edi tMillParamList.java:16)
    so that's the class you need to show us. Not MillParameterList.

    What's on line 16 of EditMillParamList?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default Re: Starting new activity through Options menu (null pointer?)

    Thank you for being willing to take a look at what I am doing wrong. Once I get home, I will post that area of EditMillParamList. Though, I did take a brief look at this area of the class before heading to work.

    It is where I declare/instantiate my first class wide vaiable.

    I was thinking that the null pointer was occuring because it couldnt find EditMillParamList. Apparently, that is not the issue. The Null Pointer issue is occurring within EditMillParamList. HAHA I was way off.

    Ill post actual coding once I return home,
    Willriker
    Last edited by Willriker; 09-18-2012 at 02:39 PM.

  4. #4
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default Re: Starting new activity through Options menu (null pointer?)

    I'm back!

    here is a portion of EditMillParam, line 16 is where I first start to declare/instantiate my class wide variables starting with mMillModel. I cut out a lot of this class for brevity, stuff like the onResume, onPause, and so on. down below, I included a snippet from the xml that pertains to line 16.

    Java Code:
    package com.Sendzimir.RollGap;
    
    import java.sql.SQLException;
    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    
    public class EditMillParamList extends Activity {
    	
    	private EditText mMillModel = (EditText) findViewById(R.id.editmodel);
    	private EditText mAX = (EditText) findViewById(R.id.ax);
    	private EditText mAY = (EditText) findViewById(R.id.ay);
    	private EditText mBX = (EditText) findViewById(R.id.bx);
    	private EditText mBY = (EditText) findViewById(R.id.by);
    	private EditText mCX = (EditText) findViewById(R.id.cx);
    	private EditText mCY = (EditText) findViewById(R.id.cy);
    	private EditText mDX = (EditText) findViewById(R.id.dx);
    	private EditText mDY = (EditText) findViewById(R.id.dy);
    	private EditText mEX = (EditText) findViewById(R.id.dx);
    	private EditText mEY = (EditText) findViewById(R.id.dy);
    	private EditText mFX = (EditText) findViewById(R.id.cx);
    	private EditText mFY = (EditText) findViewById(R.id.cy);
    	private EditText mGX = (EditText) findViewById(R.id.bx);
    	private EditText mGY = (EditText) findViewById(R.id.by);
    	private EditText mHX = (EditText) findViewById(R.id.ax);
    	private EditText mHY = (EditText) findViewById(R.id.ay);
    	private Button mSave = (Button) findViewById(R.id.save);
    	private RollGapDBAdapter mDbHelper;
    	private Long mRowId;
    	
    	/** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.millparam);
            mDbHelper = new RollGapDBAdapter(this);
            mRowId = savedInstanceState != null 
            		? savedInstanceState.getLong(RollGapDBAdapter.KEY_ROWID) : null;
            registerButtonListenersAndSetDefaultText();
        }
        
        private void registerButtonListenersAndSetDefaultText() {
        	mSave.setOnClickListener(new View.OnClickListener() {
        		public void onClick(View view) {
        			saveState();
        			setResult(RESULT_OK);
        			Toast.makeText(EditMillParamList.this,
        					       getString(R.string.param_saved_message),
        					       Toast.LENGTH_SHORT).show();
        			finish();
        		}
        	});
        }
    
    /** registerButtonListenersAndSetDefaultText() listens for when the save button is pressed.
    *   This method calls saveState().  SaveState() pulls the information from the fields and
    *   sends them to the CreateParameter() method in RollGapDBAdapter.java.  note that everything
    *   is saved to the database as a string.  When pulled for calculation, they will need to
    *   be cast as double.*/
        private void saveState() {
        	String millSection = mMillModel.getText().toString();
        	String AX = mAX.getText().toString();
        	String AY = mAY.getText().toString();
        	String BX = mBX.getText().toString();
        	String BY = mBY.getText().toString();
        	String CX = mCX.getText().toString();
        	String CY = mCY.getText().toString();
        	String DX = mDX.getText().toString();
        	String DY = mDY.getText().toString();
        	String EX = mEX.getText().toString();
        	String EY = mEY.getText().toString();
        	String FX = mFX.getText().toString();
        	String FY = mFY.getText().toString();
        	String GX = mGX.getText().toString();
        	String GY = mGY.getText().toString();
        	String HX = mHX.getText().toString();
        	String HY = mHY.getText().toString();
        	
        	if (mRowId == null) {
        		long id = mDbHelper.createParameter(millSection, AX, AY, BX, BY, CX, CY,
        			                            	DX, DY, EX, EY, FX, FY, GX,
        			                            	GY, HX, HY);
        		if (id>0) {
        			mRowId = id;
        		}
        	} else {
        		mDbHelper.updateParameter(mRowId, millSection, AX, AY, BX, BY,
    			CX, CY, DX, DY, EX, EY, FX, 
    			FY, GX, GY, HX, HY);
        	}
        }
        }
    }
    this is a peice of the xml file that relates to line number 16. The file this came out of is located in the layout folder of eclipse.

    Java Code:
        <EditText
            android:id="@+id/editmodel"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/model"
            android:layout_alignBottom="@+id/model"
            android:layout_alignRight="@+id/title"
            android:inputType="textNoSuggestions" />
    Really, thank you for taking a look at this. I already felt like i was having difficulties, and you pointed out that I wasnt even looking at the correct place to resolve the issues I was having. Thank you!

  5. #5
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default Re: Starting new activity through Options menu (null pointer?)

    omg, i am such a noob...

    I guess I needed to declare the variable just after the class is defined, but provide the value to the class wide variable within the onCreate() method... I cannot do both on the same line.

    Thank you for pointing me in the correct direction. If you had not directed me towards that location, it would've taken me weeks to figure out what was wrong.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,256
    Rep Power
    19

    Default Re: Starting new activity through Options menu (null pointer?)

    Glad you sorted it out. Remember to read through the wrapped exceptions' stack trace; the first error comes from the innermost exception.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Having menu options make a new Panel
    By Wnt2bsleepin in forum AWT / Swing
    Replies: 2
    Last Post: 03-02-2012, 11:58 PM
  2. Replies: 0
    Last Post: 10-01-2011, 04:14 AM
  3. Null Pointer
    By theen3my in forum AWT / Swing
    Replies: 3
    Last Post: 10-03-2009, 02:10 PM
  4. null pointer help
    By mayhewj7 in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 11:51 PM
  5. Menu Options For User
    By mnand in forum New To Java
    Replies: 1
    Last Post: 02-02-2009, 03:12 AM

Posting Permissions

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