简体   繁体   中英

java.lang.IllegalStateException: Could not execute method of the activity , Caused by: java.lang.NullPointerException

I see that Trip and Person details are persisted in the database and then app crashes. It does not return back to MainActivity.java after inserting data into database

03-29 21:19:50.277: E/Trace(1288): error opening trace file: No such file or directory (2)
    03-29 21:20:34.477: E/AndroidRuntime(1288): FATAL EXCEPTION: main
    03-29 21:20:34.477: E/AndroidRuntime(1288): java.lang.IllegalStateException: Could not execute method of the activity
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.view.View$1.onClick(View.java:3599)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.view.View.performClick(View.java:4204)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.view.View$PerformClick.run(View.java:17355)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.os.Handler.handleCallback(Handler.java:725)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.os.Handler.dispatchMessage(Handler.java:92)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.os.Looper.loop(Looper.java:137)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invoke(Method.java:511)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at dalvik.system.NativeStart.main(Native Method)
    03-29 21:20:34.477: E/AndroidRuntime(1288): Caused by: java.lang.reflect.InvocationTargetException
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at java.lang.reflect.Method.invoke(Method.java:511)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at android.view.View$1.onClick(View.java:3594)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     ... 11 more
    03-29 21:20:34.477: E/AndroidRuntime(1288): Caused by: java.lang.NullPointerException
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at com.example.assignment1.controller.CreateTripActivity.persistTrip(CreateTripActivity.java:104)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     at com.example.assignment1.controller.CreateTripActivity.createTrip(CreateTripActivity.java:82)
    03-29 21:20:34.477: E/AndroidRuntime(1288):     ... 14 more
    03-29 21:25:36.305: E/Trace(1756): error opening trace file: No such file or directory (2)

CreateTripActivity.java

package com.example.assignment1.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

import com.example.assignment1.R;
import com.example.assignment1.model.Person;
import com.example.assignment1.model.Trip;

public class CreateTripActivity extends Activity {
    static final String TRIP_KEY="TRIP";
    static final String LOG="LOG";
    private static final int REQUEST_CONTACT = 2;
    Person[] person=new Person[10];
    int i=0;
    String plist="";  
    EditText name;
    EditText place;
    EditText date;
    EditText starttime;
    EditText endtime;
    EditText list;
    EditText description;
    Calendar c = Calendar.getInstance();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createtrip);
        name=(EditText)findViewById(R.id.nametext);
        place=(EditText)findViewById(R.id.placetext);
        date=(EditText)findViewById(R.id.datetext);
        starttime=(EditText)findViewById(R.id.starttimetext);
        endtime=(EditText)findViewById(R.id.endtimetext);
        description=(EditText)findViewById(R.id.descriptiontext);
        list=(EditText)findViewById(R.id.personlist);
        // handling incoming google maps intent
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleSendText(intent); // Handle text being sent
            }
        } 
    }

    /**
     * This method should be used to
     * instantiate a Trip model.
     * 
     * @return The Trip as represented
     * by the View.
     */
    public Trip createTrip(View v) {
        String tripname=name.getText().toString();
        String tripplace=place.getText().toString();
        String tripdate=date.getText().toString();
        String tripstarttime=starttime.getText().toString();
        String tripendtime=endtime.getText().toString();
        String tripdescription=description.getText().toString();
        Trip trip=new Trip(tripname,tripdate,tripstarttime,tripendtime,tripplace,tripdescription,c.getTime().toString());
        persistTrip(trip,person);
        Log.i("LOG","returning to main");
        Intent i=new Intent(this,MainActivity.class);
        startActivity(i);
        return null;
    }

    //persist person
     public void persistPerson(String name, String phone){
          person[i] =new Person(name,phone);
          Log.i(LOG,person[i].getName());
          i +=1;
      }
    /*
     persist trip to database
     */
    public void persistTrip(Trip trip,Person[] person) {
        DBAdapter db=new DBAdapter(this);
        db.open();
        db.insertTrip(trip.getName(),trip.getPlace(), trip.getDate(), trip.getStartTime(),trip.getEndTime(),trip.getDescription(),trip.getCreatetime());
        for(i=0;i<person.length;i++)
        {
        Log.i(LOG,"inserting person"+person[i].getName()+person[i].getPhone());
        db.insertPerson(person[i].getName(),person[i].getPhone());
        }
        db.close();
    }

    /**
     * This method should be used when a
     * user wants to cancel the creation of
     * a Trip.
     * 
     * Note: You most likely want to call this
     * if your activity dies during the process
     * of a trip creation or if a cancel/back
     * button event occurs. Should return to
     * the previous activity without a result
     * using finish() and setResult().
     */
    public void cancelTripCreation(View v) {
        Intent back=new Intent(this,MainActivity.class);
        setResult(RESULT_OK,back);
        finish();
    }

    //Datepickerlistener
         DatePickerDialog.OnDateSetListener datepick = new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet( DatePicker view, int year, int monthOfYear, int dayOfMonth ) {
                  c.set( Calendar.YEAR, year );
                  c.set( Calendar.MONTH, monthOfYear );
                  c.set( Calendar.DAY_OF_MONTH, dayOfMonth );
                  setCurrentDateOnView();
                }
              };
    //Date picker
    public void dateOnClick( View view ) {
        Log.i(LOG,"Date clicked");
        new DatePickerDialog(CreateTripActivity.this, datepick,
            c.get( Calendar.YEAR ), c.get( Calendar.MONTH ), c.get( Calendar.DAY_OF_MONTH ) ).show();
      }

    //listener for timepickerdialog
    TimePickerDialog.OnTimeSetListener timepick = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet( TimePicker view, int hourOfDay, int minute ) {
          c.set( Calendar.HOUR_OF_DAY, hourOfDay );
          c.set( Calendar.MINUTE, minute );
          setCurrentDateOnView();
        }
      };

      //time picker
      public void timeOnClick( View view ) {
            new TimePickerDialog( CreateTripActivity.this, timepick,
                c.get( Calendar.HOUR ), c.get( Calendar.MINUTE ), false ).show();
          }


      public void setCurrentDateOnView() {
        String dateFormat = "MM/dd/yyyy";
        SimpleDateFormat sdf = new SimpleDateFormat( dateFormat, Locale.US );
        date.setText( sdf.format( c.getTime() ) );

        String timeFormat = "hh:mm a";
        SimpleDateFormat stf = new SimpleDateFormat( timeFormat, Locale.US );
        starttime.setText( stf.format( c.getTime() ) );
      }

    //listener for timepickerdialog
        TimePickerDialog.OnTimeSetListener endtimepick = new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet( TimePicker view, int hourOfDay, int minute ) {
              c.set( Calendar.HOUR_OF_DAY, hourOfDay );
              c.set( Calendar.MINUTE, minute );
              setCurrentDateOnView1();
            }
          };

          //time picker
          public void endtimeOnClick( View view ) {
                new TimePickerDialog( CreateTripActivity.this, endtimepick,
                    c.get( Calendar.HOUR ), c.get( Calendar.MINUTE ), false ).show();
              }


          public void setCurrentDateOnView1() {
            String dateFormat = "MM/dd/yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat( dateFormat, Locale.US );
            date.setText( sdf.format( c.getTime() ) );

            String timeFormat = "hh:mm a";
            SimpleDateFormat stf = new SimpleDateFormat( timeFormat, Locale.US );
            endtime.setText( stf.format( c.getTime() ) );
          }


      // add persons to trip via phonebook
      public void contacts(View v) {
          Intent i = new Intent(Intent.ACTION_PICK,
               ContactsContract.Contacts.CONTENT_URI);
          startActivityForResult(i, REQUEST_CONTACT); 
          Log.i(LOG,"went");
       }

      public void onActivityResult(int requestCode, int resultCode, Intent data) {
             if (requestCode == REQUEST_CONTACT) {
                 Uri uri = data.getData();
                 String[] projection    = new String[] {ContactsContract.Contacts.DISPLAY_NAME
                        // ContactsContract.CommonDataKinds.Phone.NUMBER
                         };

                 Cursor people = getContentResolver().query(uri, projection, null, null, null);

                 int indexName = people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                 //int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

                 people.moveToFirst();
                 do {
                     String name   = people.getString(indexName);
                    String number = null;
                     persistPerson(name,number);
                     plist +=name+"\n";
                // plist +=number;
                 } while (people.moveToNext());


            }
            list.setText(plist);
        }



      public void googleMaps(View v){
        // Build the intent
          Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
            // Verify it resolves
            PackageManager packageManager = getPackageManager();
            List<ResolveInfo> activities = packageManager.
                                           queryIntentActivities(mapIntent, 0);
        boolean isIntentSafe = activities.size() > 0;
        // Start an activity if it's safe
        if (isIntentSafe) {startActivity(mapIntent);}

      }

      //handling incoming intent from google map
      void handleSendText(Intent intent) {
            String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
            if (sharedText != null) {
                // Update UI to reflect text being shared
                place.setText(sharedText);
            }
        }

}

DBAdapter.java

// ------------------------------------ DBADapter.java ---------------------------------------------

// TODO: Change the package to match your project.
package com.example.assignment1.controller;

import com.example.assignment1.model.Person;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter {

    // For logging:
    private static final String TAG = "DBAdapter";

    // Trip table Fields
      public static final String TABLE_NAME = "trip";
      public static final String TRIP_ID = "_id";
      public static final String TRIP_NAME = "tripname";
      public static final String TRIP_LOCATION = "tripplace";
      public static final String TRIP_DATE = "tripdate";
      public static final String TRIP_START = "tripstarttime";
      public static final String TRIP_END = "tripendtime";
      public static final String TRIP_CREATED = "tripcreatetime";
      public static final String TRIP_DESCRIPTION = "tripdescription";

      //Person Table Fields
      public static final String PERSON_TABLE = "person";
      public static final String PERSON_ID = "P_id";
      public static final String PERSON_NAME = "personname";
      public static final String PERSON_NUMBER = "personnumber";

      private static final String DATABASE_NAME = "tripplanner.db";
      private static final int DATABASE_VERSION = 2;

      private static final String DATABASE_CREATE = "create table " + TABLE_NAME + 
              "(" +TRIP_ID+ " integer primary key autoincrement, " 
              + TRIP_NAME+ " text not null, "
              + TRIP_LOCATION+ " text not null, "
              + TRIP_DATE+ " text not null, "
              + TRIP_START+ " text not null, "
              + TRIP_END+ " text not null, "
              + TRIP_CREATED+ " text not null, "
              + TRIP_DESCRIPTION+ " text"
              + ");";

      private static final String PERSON_CREATE = "create table " + PERSON_TABLE + 
              "(" +TRIP_ID+ " integer not null, " 
              + PERSON_ID+ " integer primary key autoincrement,  "
              + PERSON_NAME+ " text not null, "
              + PERSON_NUMBER+ " text, "
              + " FOREIGN KEY ("+TRIP_ID+") REFERENCES "+TABLE_NAME+" ("+TRIP_ID+")"
              + ");";
    // Context of application who uses us.
    private final Context context;

    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;

    //  Public methods:
    public DBAdapter(Context ctx) {
        this.context = ctx;
        myDBHelper = new DatabaseHelper(context);
    }

    // Open the database connection.
    public DBAdapter open() {
        db = myDBHelper.getWritableDatabase();
        return this;
    }

    // Close the database connection.
    public void close() {
        myDBHelper.close();
    }

    // Add a new trip attributes to database.
    public void insertTrip(String tripname,String location,String tripdate,String tripstarttime, String tripendtime,String tripdescription,String tripcreatetime) {
        // Create row's data:
        ContentValues initialValues = new ContentValues();
        initialValues.put(TRIP_NAME, tripname);
        initialValues.put(TRIP_LOCATION,location);
        initialValues.put(TRIP_START,tripstarttime);
        initialValues.put(TRIP_END,tripendtime);
        initialValues.put(TRIP_DATE,tripdate);
        initialValues.put(TRIP_CREATED,tripcreatetime);
        initialValues.put(TRIP_DESCRIPTION,tripdescription);
        // Insert it into the database.
        db.insert(TABLE_NAME, null, initialValues);
        Log.i(TAG,"insert into"+TABLE_NAME+"values"+tripname+","+location+","+tripdate+","+tripstarttime+","+tripendtime+","+tripdescription+","+tripcreatetime);
        return;
    }

    // Add a new person attributes to database.
        public void insertPerson(String personname,String personnumber) {
            // Create row's data:
            Log.i(TAG,"inserting person....");
            ContentValues initialValues = new ContentValues();
            initialValues.put(PERSON_NAME,personname);
            initialValues.put(PERSON_NUMBER,personnumber);
            Log.i("LOG","fetching max tripid");
            initialValues.put(TRIP_ID,getTripId());
            Log.i("LOG","max tripid fetched");
            // Insert it into the database.
            db.insert(PERSON_TABLE, null, initialValues);
            Log.i(TAG,"insert into"+PERSON_TABLE+"values"+","+getTripId()+personname+","+personnumber);
            return;
        }

        public String getTripId(){
            Log.i("LOG","gettripid");
            Cursor cur=db.rawQuery("select max(_id) from trip",null);
            if(cur.moveToFirst()){
            String tid=cur.getString(0);
            Log.i("LOG",tid);
            }
            return cur.getString(0);    
        }

    /*// Delete a row from the database, by rowId (primary key)
    public boolean deleteRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        return db.delete(DATABASE_TABLE, where, null) != 0;
    }

    public void deleteAll() {
        Cursor c = getAllRows();
        long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
        if (c.moveToFirst()) {
            do {
                deleteRow(c.getLong((int) rowId));              
            } while (c.moveToNext());
        }
        c.close();
    }
    */
    // Return all data in the database.
    public Cursor getAllRows() {
        String where = null;
        Cursor c =  db.query(true,TABLE_NAME,null, 
                            where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // Get a specific row (by rowId)
    public Cursor getRow(long rowId) {
        String where = TRIP_ID + "=" + rowId;
        Cursor c =  db.query(true, TABLE_NAME, null, 
                        where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

/*  
 Change an existing row to be equal to new data.

    public boolean updateRow(long rowId, String name, int studentNum, String favColour) {
        String where = KEY_ROWID + "=" + rowId;

        // TODO: Update data in the row with new fields.
        // TODO: Also change the function's arguments to be what you need!
        // Create row's data:
        ContentValues newValues = new ContentValues();
        newValues.put(KEY_NAME, name);
        newValues.put(KEY_STUDENTNUM, studentNum);
        newValues.put(KEY_FAVCOLOUR, favColour);

        // Insert it into the database.
        return db.update(DATABASE_TABLE, newValues, where, null) != 0;
    }

*/  
    /**
     * Private class which handles database creation and upgrading.
     * Used to handle low-level database access.
     */
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(PERSON_CREATE);
            _db.execSQL(DATABASE_CREATE);

            Log.i(TAG,DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading application's database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data!");

            // Destroy old database:
            _db.execSQL("DROP TABLE IF EXISTS "+DATABASE_NAME);

            // Recreate new database:
            onCreate(_db);
        }
    }

}

You have initialised the person array with

Person[] person = new Person[10];

However, in the persistTrip method, you iterate over this array and the first element (person[0]) is null, hence the NPE on the line

Log.i(LOG,"inserting person"+person[i].getName()+person[i].getPhone());

This is because the default value for an object is null.

To properly initialise an array of Person objects, you should use:

for (int i = 0; i < 10; i++) {
    person[i] = new Person();
}

or wrap the use of the array with a null check.

as you got NPE on line 104 that is:

Log.i(LOG,"inserting person"+person[i].getName()+person[i].getPhone());

3 possibility is exists.

1- one or more Name is null so person[i].getName() throw NPE

2- one or more Phone is null so person[i].getPhone() throw NPE

3- one or more Person is not initialize,

how can find?

use log and print i in for statement and find which position is null,

Better Way

use Arraylist and write your code like

ArrayList<Person> persons = new ArrayList<Person>()

ya, you're iterating over your person array and assuming it is full of 10 initialized objects - which it isnt since you are getting a NPE.

For a quick fix, change your persistTrip method to this...

    public void persistTrip(Trip trip,Person[] person) {
       DBAdapter db=new DBAdapter(this);
       db.open();
       db.insertTrip(trip.getName(),trip.getPlace(), trip.getDate(), trip.getStartTime(),trip.getEndTime(),trip.getDescription(),trip.getCreatetime());
       for(i=0;i<person.length;i++)
       {
          if (person[i] != null) {
             Log.i(LOG,"inserting person"+person[i].getName()+person[i].getPhone());
             db.insertPerson(person[i].getName(),person[i].getPhone());
          }
       }
    db.close();
}

you just need to ensure there is an initialized object in that location in the array instead of taking it for granted ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM