Java 空指针异常:找不到解决方案

Java 空指针异常:找不到解决方案,java,android,sqlite,Java,Android,Sqlite,我的代码中出现错误,这是不可理解的。。请帮我找出是什么问题 我有数据库课程和主要活动。。它显示在日志中,但当它出现在我的模拟器屏幕上时,它会给我错误信息 我的数据库类: package com.example.nearby_places; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.content.Context; import

我的代码中出现错误,这是不可理解的。。请帮我找出是什么问题

我有数据库课程和主要活动。。它显示在日志中,但当它出现在我的模拟器屏幕上时,它会给我错误信息

我的数据库类:

package com.example.nearby_places;

import java.util.ArrayList;
import java.util.List;


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 Database extends SQLiteOpenHelper {

    //database name & version number
    private static final String db_name = "nearby_places";
    private static final int db_version = 1;

    //tables
    private static final String table_placetypes = "placetypes";
    private static final String table_places = "table_places";

    //column names
    private static final String type_id = "type_id";
    private static final String type_name = "type_name";
    private static final String place_id = "place_id";
    private static final String place_name = "place_name";
    private static final String place_address = "place_address";
    private static final String place_contact = "place_contact";


    public Database(Context context) {
        super(context, db_name, null, db_version);
        // TODO Auto-generated constructor stub
    }

    // create table queries
    String create_table_placetypes = "CREATE TABLE IF NOT EXISTS " + table_placetypes + "("
            + type_id + " INTEGER PRIMARY KEY NOT NULL," + type_name + " TEXT" + ")";



    String create_table_places = "CREATE TABLE IF NOT EXISTS table_places (place_id INTEGER PRIMARY KEY NOT NULL, place_name TEXT, place_address TEXT, place_contact TEXT, type_id INTEGER, FOREIGN KEY (type_id) REFERENCES table_placetypes(type_id))";



    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL(create_table_placetypes);

        Log.d("creating", "placetypes created");
        db.execSQL(create_table_places);
        Log.d("creating", "places created");


    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + table_placetypes);
        db.execSQL("DROP TABLE IF EXISTS " + table_places);
        onCreate(db);

    }
    // add placetypes 
    void addplacetypes (placetypes pt) {

        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(type_name, pt.getTypename());

        db.insert(table_placetypes, null, values);
        db.close();

    }

     // Getting single placetypes
    placetypes getPlacetypes(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(table_placetypes, new String[] {type_id,
                type_name }, type_id + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        placetypes pt = new placetypes(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1));
        // return contact
        return pt;
    }

 // Getting All placetypes
    public List<placetypes> getAllPlacetypes() {
        List<placetypes> placetypesList = new ArrayList<placetypes>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + table_placetypes;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                placetypes pt = new placetypes();
                pt.setTypeid(Integer.parseInt(cursor.getString(0)));
                pt.setTypename(cursor.getString(1));


                //String name = cursor.getString(1);

                //MainActivity.ArrayofName.add(name);
                // Adding contact to list
                placetypesList.add(pt);
            } while (cursor.moveToNext());
        }

        // return placetype list
        return placetypesList;
}

    // Getting placetypes Count
    public int getPlacetypesCount() {
        String countQuery = "SELECT  * FROM " + table_placetypes;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

    public void addplaces(places p) {
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(place_name, p.getPlace_name());
        values.put(place_address, p.getPlace_address());
        values.put(place_contact, p.getPlace_contact());
        values.put(type_id, p.getT_id());

        Log.d("Type ID", String.valueOf(p.getT_id()));
        db.insert(table_places, null, values);
        db.close();

    }

    places getPlaces(int pid) {

        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(table_places, new String[] {place_id, place_name, place_address, place_contact,type_id}, place_id + "=?", new String[] { String.valueOf(pid) } , null, null, null, null);

        if(cursor != null)
             cursor.moveToFirst();

        places p = new places(Integer.parseInt(cursor.getString(0)),
                Integer.parseInt(cursor.getString(1)),
                cursor.getString(2),
                cursor.getString(3),
                cursor.getString(4)
                );

        cursor.close();

        return p;

    }

    public List<places> getAllPlaces(String typeName) {

        List<places> placeList = new ArrayList<places>();
        //String selectQuery = "SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id "; 
        //String selectQuery = "SELECT * FROM table_places WHERE table_places.type_id="+Integer.toString(typeid);
        String selectQuery ="SELECT * FROM table_places WHERE placetypes.place_name="+typeName+" INNER JOIN placetypes ON placetypes.type_id=table_places.type_id";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);


        if(cursor.moveToFirst() )
        {
            do{
            places p = new places();
            /*p.setT_id(cursor.getColumnIndex(type_id));
            p.setPlace_id(cursor.getColumnIndex(place_id));
            p.setPlace_name(cursor.getColumnIndex(place_name));
            */
            p.setT_id(cursor.getInt(0));
            p.setPlace_id(cursor.getInt(1));
            p.setPlace_name(cursor.getString(2));
            p.setPlace_address(cursor.getString(3));
            p.setPlace_contact(cursor.getString(4));

            /*String t_id = cursor.getString(4);
            String p_name = cursor.getString(2);
            String p_address = cursor.getString(3);
            String p_contact = cursor.getString(1);*/

            placeList.add(p);
            }while(cursor.moveToNext());
        }

        cursor.close();
        return placeList;
    }

    public int getPlaceCount () {

        String selectQuery = "SELECT * FROM " +table_places;
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(create_table_places, null);
        cursor.close();

        return cursor.getCount();
    }

}

您对getAdapter的调用返回null,因为您在setAdapter之前调用它,请尝试以下操作:

listView.setAdapter(adapter);
int pos = listView.getAdapter().getCount() -1;
listView.getAdapter().getItemId(pos);

如果出现空指针异常,请深入查看日志。特别是在它说由引起的
的那一行。
学习如何阅读和使用您的LogCat,并尝试找到它提到您的类/包名称的那一行,并分析这一行

Caused by: java.lang.NullPointerException
10-11 17:21:51.894: E/AndroidRuntime(4932): at com.example.nearby_places.MainActivity.onCreate(MainActivity.java:62)
问题在于

List<placetypes> placetypes = db.getAllPlacetypes();

您的
main活动
中是哪一行号62。您能粘贴所有代码吗?解决了的。。谢谢你能在数据库类-getAllPlaces函数中检查我的选择查询吗?因为它给了我语法错误你说的粘贴是什么意思?这个问题已经解决了。请看一下数据库类中的select query@getAllPlace()函数好吗?它给了我数据库中INNERIn附近的语法错误:查询问题。。内部联接和mainActivity中的if条件if(getAllplaces(x)。
Caused by: java.lang.NullPointerException
10-11 17:21:51.894: E/AndroidRuntime(4932): at com.example.nearby_places.MainActivity.onCreate(MainActivity.java:62)
List<placetypes> placetypes = db.getAllPlacetypes();
`SELECT * FROM table_places INNER JOIN placetypes ON placetypes.type_id=table_places.type_id WHERE placetypes.place_name="+typeName+`"