vendredi 29 avril 2016

SQLite Database Nullpointer Exception?

I need some helps with extracting my data out of my SQLite Database. I Can't find where are errors at. Can someone help me? I appreciate it.

Here is the Main Activity:

enter cpublic class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
public WebView web;
public DBHelper AsongHelper;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initial();
    Insertdata();
}
public void initial() {
    ListView ml = (ListView) findViewById(R.id.listView1);
    List<listview_item>Songlist = AsongHelper.getSongsName();
    listview_adapter ada = new listview_adapter(this, Songlist);
    ml.setAdapter(ada);
    ml.setOnItemClickListener(this);
    web = (WebView) findViewById(R.id.webView1);
    WebSettings webSettings = web.getSettings();
    webSettings.setJavaScriptEnabled(true);
    web.getSettings().setCacheMode(webSettings.LOAD_CACHE_ELSE_NETWORK);
    web.setWebViewClient(
            new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return false;
                }
            }
    );
    web.loadUrl("http://youtu.be/bMqKM9lqX7M");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    List<String> UrlList =AsongHelper.geturl();
    web.loadUrl(UrlList.get(position));}
public void Insertdata(){
    AsongHelper.insertdata("小幸运", "https://youtu.be/bMqKM9lqX7M", "Jason Chen");
    AsongHelper.insertdata("Love Yourself","https://youtu.be/oyEuk8j8imI","Justin Bieber");
    AsongHelper.insertdata("Gentleman","https://youtu.be/lGVKrj6gpnI","Will Jay");
}
}

Here is my database helper class:

public class DBHelper extends SQLiteOpenHelper{

public static final String DATABASE_NAME = "ASong.op";
public static final String ASONG_TABLE_NAME = "Asongs";
public static final String ASONG_COLUMN_ID = "id";
public static final String ASONG_COLUMN_SONGSNAME = "SongsName";
public static final String ASONG_COLUMN_URL = "url";
public static final String ASONG_COLUMN_ARTIST = "artist";
private HashMap hp;
public DBHelper(Context context)
{
    super(context, DATABASE_NAME , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(
            "create table Asongs " + "(id integer primary key, SongsName text,url text,artist text)"
    );
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS Asongs");
    onCreate(db);
}
public boolean insertdata  (String songsname, String url, String artist)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(ASONG_COLUMN_SONGSNAME,songsname);
    contentValues.put(ASONG_COLUMN_URL,url);
    contentValues.put(ASONG_COLUMN_ARTIST, artist);
    db.insert("Asongs", null, contentValues);
    return true;
}
public List<listview_item> getSongsName(){
    SQLiteDatabase db = this.getReadableDatabase();
    List<listview_item> SongsNameList = new ArrayList<>();
    String[] SongsName={ASONG_COLUMN_SONGSNAME};
    Cursor cusor =  db.query(ASONG_TABLE_NAME,SongsName,null,null,null,null,null);
    StringBuffer buffer = new StringBuffer();
    while(cusor.moveToNext()){
        int index1 = cusor.getColumnIndex(ASONG_COLUMN_SONGSNAME);
        String cid = cusor.getString(index1);
        buffer.append(cid);
        SongsNameList.add(new listview_item(buffer.toString()));
    }
    cusor.close();
    return SongsNameList;
}
public List<String> geturl(){
    List<String> SongsUrlList = new ArrayList<>();
    SQLiteDatabase db = this.getReadableDatabase();
    String[] ColumnUrl={ASONG_COLUMN_URL};
    Cursor cusor =  db.query(ASONG_TABLE_NAME,ColumnUrl,null,null,null,null,null);
    StringBuffer buffer = new StringBuffer();
    while(cusor.moveToNext()){
        int index2 = cusor.getColumnIndex(ASONG_COLUMN_URL);
        String cid2 = cusor.getString(index2);
        buffer.append(cid2);
        SongsUrlList.add(buffer.toString());
    }
    cusor.close();
    return SongsUrlList;
}
}

CatLog is right here:

04-29 19:56:48.673 27116-27116/com.example.jinyu.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.jinyu.myapplication, PID: 27116
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jinyu.myapplication/com.example.jinyu.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.jinyu.myapplication.DBHelper.getReadableDatabase()' on a null object reference
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                              Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.jinyu.myapplication.DBHelper.getReadableDatabase()' on a null object reference
                                                                                 at com.example.jinyu.myapplication.MainActivity.initial(MainActivity.java:29)
                                                                                 at com.example.jinyu.myapplication.MainActivity.onCreate(MainActivity.java:25)
                                                                                 at android.app.Activity.performCreate(Activity.java:6251)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-29 19:56:50.553 27116-27116/com.example.jinyu.myapplication I/Process:     Sending signal. PID: 27116 SIG: 9

Aucun commentaire:

Enregistrer un commentaire