Android Eclipse NullPointerException

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Android Eclipse NullPointerException

Postby dewitrydan » Thu Feb 03, 2011 10:38 pm

Hey there.
I keep getting a NullPointerException when I run click the confirmButton button:
Can you please help me fix this??
Oh and, how can find out which line number this error occurs? thanks
Code: Select all
02-03 21:21:29.908: ERROR/AndroidRuntime(574): FATAL EXCEPTION: main
02-03 21:21:29.908: ERROR/AndroidRuntime(574): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.demo.notepad3/com.android.demo.notepad3.NoteEdit}: java.lang.NullPointerException
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.os.Looper.loop(Looper.java:123)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at dalvik.system.NativeStart.main(Native Method)
02-03 21:21:29.908: ERROR/AndroidRuntime(574): Caused by: java.lang.NullPointerException
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at com.android.demo.notepad3.NoteEdit.populateFiels(NoteEdit.java:66)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at com.android.demo.notepad3.NoteEdit.onCreate(NoteEdit.java:51)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-03 21:21:29.908: ERROR/AndroidRuntime(574):     ... 11 more
02-03 21:26:41.299: ERROR/AndroidRuntime(605): FATAL EXCEPTION: main
02-03 21:26:41.299: ERROR/AndroidRuntime(605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.demo.notepad3/com.android.demo.notepad3.NoteEdit}: java.lang.NullPointerException
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.os.Looper.loop(Looper.java:123)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at dalvik.system.NativeStart.main(Native Method)
02-03 21:26:41.299: ERROR/AndroidRuntime(605): Caused by: java.lang.NullPointerException
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at com.android.demo.notepad3.NoteEdit.populateFiels(NoteEdit.java:67)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at com.android.demo.notepad3.NoteEdit.onCreate(NoteEdit.java:52)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-03 21:26:41.299: ERROR/AndroidRuntime(605):     ... 11 more
02-03 21:27:08.438: ERROR/AndroidRuntime(614): FATAL EXCEPTION: main
02-03 21:27:08.438: ERROR/AndroidRuntime(614): java.lang.RuntimeException: Unable to pause activity {com.android.demo.notepad3/com.android.demo.notepad3.NoteEdit}: java.lang.NullPointerException
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.ActivityThread.access$2500(ActivityThread.java:125)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.os.Looper.loop(Looper.java:123)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at java.lang.reflect.Method.invoke(Method.java:521)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at dalvik.system.NativeStart.main(Native Method)
02-03 21:27:08.438: ERROR/AndroidRuntime(614): Caused by: java.lang.NullPointerException
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at com.android.demo.notepad3.NoteEdit.saveState(NoteEdit.java:97)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at com.android.demo.notepad3.NoteEdit.onPause(NoteEdit.java:86)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.Activity.performPause(Activity.java:3842)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1190)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3335)
02-03 21:27:08.438: ERROR/AndroidRuntime(614):     ... 12 more


Here is my code:
Code: Select all
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.android.demo.notepad3;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class NoteEdit extends Activity {

   private EditText mTitleText;
   private EditText mBodyText;
   private Long mRowId;
   private NotesDbAdapter mDbHelper;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // setContentView(R.layout.note_edit);
      mDbHelper = new NotesDbAdapter(this);
      mDbHelper.open();
      setContentView(R.layout.note_edit);

      Button confirmButton = (Button) findViewById(R.id.confirm);
      mRowId = null;
      mRowId = (savedInstanceState == null) ? null
            : (Long) savedInstanceState
                  .getSerializable(NotesDbAdapter.KEY_ROWID);
      if (mRowId == null) {
         Bundle extras = getIntent().getExtras();
         mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID)
               : null;
      }

      populateFiels();
      confirmButton.setOnClickListener(new View.OnClickListener() {

         public void onClick(View view) {
            setResult(RESULT_OK);
            finish();
         }

      });
   }

   private void populateFiels() {
      if (mRowId != null) {
         Cursor note = mDbHelper.fetchNote(mRowId);
         startManagingCursor(note);
         mTitleText.setText(note.getString(note
               .getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
         mBodyText.setText(note.getString(note
               .getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
      }
   }

   @Override
   protected void onSaveInstanceState(Bundle outState) {
      // TODO Auto-generated method stub
      super.onSaveInstanceState(outState);
      saveState();
      outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
   }

   @Override
   protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      saveState();
   }

   @Override
   protected void onResume() {
      // TODO Auto-generated method stub
      super.onResume();
      populateFiels();
   }

   private void saveState() {
      String title = mTitleText.getText().toString();
      String body = mBodyText.getText().toString();

      if (mRowId == null) {
         long id = mDbHelper.createNote(title, body);
         if (id > 0) {
            mRowId = id;
         } else {
            mDbHelper.updateNote(mRowId, title, body);
         }
      }
   }
}
dewitrydan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Sep 06, 2010 12:03 pm

Top

Re: Android Eclipse NullPointerException

Postby dewitrydan » Mon Feb 07, 2011 8:14 am

please help :(
this is sooo annoying
thank you
dewitrydan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Sep 06, 2010 12:03 pm

Top

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 16 guests