2013-06-06 4 views
0

SQLiteExample.javaNullPointer 예외 오류

package com.example.myfirstapp; 

import android.app.Activity; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class SQLiteExample extends Activity implements OnClickListener { 

EditText sqlName, sqlHotness; 
    Button sqlUpdate, sqlView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sqliteexample); 

    sqlName = (EditText) findViewById(R.id.etName); 
    sqlHotness = (EditText) findViewById(R.id.etSQLHotness); 
    sqlUpdate = (Button) findViewById(R.id.bSQLUpdate); 
    sqlView = (Button) findViewById(R.id.bSQLOpenView); 

    sqlUpdate.setOnClickListener(this); 
    sqlView.setOnClickListener(this); 

} 

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    switch (arg0.getId()) { 
    case R.id.bSQLUpdate: 

     boolean didItWork = true; 
     try { 
      String name; 
      name = sqlName.getText().toString(); 
      String hot; 
      hot = sqlHotness.getText().toString(); 
      HotOrNot entry = new HotOrNot(SQLiteExample.this); 
      entry.open(); 
      entry.createEntry(name, hot); 
      entry.close(); 
     } catch (Exception e) { 
      didItWork = false; 
      String error; 
      error = e.toString(); 
      Dialog d = new Dialog(this); 
      TextView tv = new TextView(this); 
      d.setTitle("Nop"); 
      tv.setText(error); 
      d.setContentView(tv); 
      d.show(); 
     } finally { 

      if (didItWork) { 
       Dialog d = new Dialog(this); 
       TextView tv = new TextView(this); 
       d.setTitle("YAY"); 
       tv.setText("Success"); 

       d.setContentView(tv); 
       d.show(); 
      } 
     } 


     /* 
     * 
     * boolean didwork = true; try{ String name = 
     * sqlName.getText().toString(); String hotness = 
     * sqlHotness.getText().toString(); 
     * 
     * HotOrNot entry = new HotOrNot(SQLiteExample.this); entry.open(); 
     * entry.createEntry(name,hotness); entry.close(); }catch (Exception 
     * e){ didwork = false; }finally{ if (didwork){ /*Dialog d = new 
     * Dialog(this); d.setTitle("Heck Ya!!"); TextView tv = new 
     * TextView(this); tv.setText("Success"); d.setContentView(tv); 
     * d.show(); 
     */ 
     /* 
     * Toast t = Toast.makeText(SQLiteExample.this, "Success", 
     * Toast.LENGTH_LONG); t.show(); } else{ Toast t = 
     * Toast.makeText(SQLiteExample.this, "Fail", Toast.LENGTH_LONG); 
     * t.show(); } } 
     */ 
     break; 
    case R.id.bSQLOpenView: 
     break; 
    } 
} 
    } 

HotOrNot.java

package com.example.myfirstapp; 

import android.app.Activity; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class HotOrNot extends Activity{ 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "persons_name"; 
public static final String KEY_HOTNESS = "persons_hotness"; 

private static final String DATABASE_NAME = "HotOrNotdb.db"; 
private static final String DATABASE_TABLE = "peopleTable"; 
private static final int DATABASE_VERSION = 1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

private static class DbHelper extends SQLiteOpenHelper { 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID 
       + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME 
       + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);"); 
    } 

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

} 

public HotOrNot(Context c) { 

    ourContext = c; 
} 

public HotOrNot open() throws SQLException { 

    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 

} 

public void close() { 
    ourHelper.close(); 
} 

public long createEntry(String name, String hotness) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_NAME, name); 
    cv.put(KEY_HOTNESS, hotness); 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 

} 
} 

sqliteexample

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView21" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Name" /> 

<EditText 
    android:id="@+id/etSQLName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" > 

</EditText> 

<TextView 
    android:id="@+id/textView12" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hotness scale 1-10" /> 

<EditText 
    android:id="@+id/etSQLHotness" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

<Button 
    android:id="@+id/bSQLUpdate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Update SQLite Database" /> 

<Button 
    android:id="@+id/bSQLOpenView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="View" /> 

</LinearLayout> 

나는이 두 클래스 파일과 아무런 오류와 XML을 가지고있다. 그러나 SQLite 데이터베이스를 업데이트하려고 할 때 설정 한 대화 상자에서 java.lang.NullPointerException 오류가 발생합니다. 나는 이것에 아주 새롭고 youtube에 자습서를 따르고있다. (http://www.youtube.com/watch?v=9ew_Ajpqwqg&list=EC2F07DBCDCC01493A)

도움을 주시면 대단히 감사하겠습니다.

+5

logcat 오류도 게시 할 수 있습니까? –

+0

NPE를 던지는 줄을 알 수 있습니까? – Abubakkar

+0

답변 해 주셔서 감사합니다. 그런 초보자에게는 유감이지만 logcat은 오류를 전혀보고하지 않습니다. 오류는 try-catch로 설정 한 대화 상자의 형태로 표시됩니다. – ronny3050

답변

0

sqlName = (EditText) findViewById(R.id.etName); 

해야

당신이 작동 내가 내 장치에 코드를 시도 이보다

<EditText 
android:id="@+id/etSQLName" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:ems="10" > 

다른 etSQLName로 글고 ID를 가지고 당신의 XML로

sqlName = (EditText) findViewById(R.id.etSQLName); 

. 나는 어떤 오류도 내지 않는다. 대화 상자에 succes 메시지가 나타납니다.