2014-05-12 6 views
0

S'il vous plaît pourriez-vous m'aider avec l'erreur "java.lang.NullPointerException". J'essaie d'enregistrer des données (nom et adresse e-mail) dans la base de données, puis de le charger. Mon application se bloque dans l'émulateur lorsque j'essaie de le démarrer. J'ai cherché des réponses et je suis tombé sur ce fil: Error java.lang.NullPointerException on method getReadableDatabase() mais cela n'a pas résolu le problème.Erreur java.lang.NullPointerException dans SQLiteDatabase Android

MainActivity.java

package com.example.test; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.os.Build; 

public class MainActivity extends ActionBarActivity { 
Button save, load; 
EditText name, email; 
DataHandler handler; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    save = (Button)findViewById(R.id.save); 
    load = (Button)findViewById(R.id.load); 
    name = (EditText)findViewById(R.id.name); 
    email = (EditText)findViewById(R.id.email); 

    save.setOnTouchListener(new OnTouchListener(){ 
     public void onClick(View v){ 

     } 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      String getName = name.getText().toString(); 
      String getEmail = email.getText().toString(); 
      handler = new DataHandler(getBaseContext()); 
      handler.open(); 
      long id = handler.insertData(getName, getEmail); 
      Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show(); 
      handler.close(); 
      return false; 
     } 
    }); 

    load.setOnTouchListener(new OnTouchListener(){ 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      // TODO Auto-generated method stub 
      String getName, getEmail; 
      getName = ""; 
      getEmail = ""; 
      handler = new DataHandler(getBaseContext()); 
      handler.open(); 
      Cursor C = handler.returnData(); 
      if(C.moveToFirst()) 
      { 
       do 
       { 
        getName = C.getString(0); 
        getEmail = C.getString(1); 
       }while(C.moveToNext()); 
      } 
      handler.close(); 
      Toast.makeText(getBaseContext(), "Name : "+getName+" and email : "+getEmail, Toast.LENGTH_LONG).show(); 
      return false; 
     } 

    }); 

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()) 
       .commit(); 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 

    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     return rootView; 
    } 
} 

} 

DataHandler.java

package com.example.test; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DataHandler { 
public static final String NAME = "name"; 
public static final String EMAIL = "email"; 
public static final String TABLE_NAME = "mytable"; 
public static final String DATA_BASE_NAME = "mydatabase"; 
public static final int DATABASE_VERSION = 1; 
public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null);"; 

DataBaseHelper dbhelper; 
private static Context ctx; 
SQLiteDatabase db; 
public DataHandler(Context ctx) 
{ 
    this.ctx = ctx; 
    dbhelper = new DataBaseHelper(ctx); 
} 

private static class DataBaseHelper extends SQLiteOpenHelper 
{ 
    public DataBaseHelper(Context ctx) 
    { 
     super(ctx, DATA_BASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     try{ 
     db.execSQL("TABLE_CREATE"); 
     } 
     catch(SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     db.execSQL("DROP TABLE IF EXISTS mytable"); 
     onCreate(db); 

    } 

} 

public DataHandler open() 
{ 
    db = dbhelper.getWritableDatabase(); 
    return this; 
} 

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

public long insertData(String name, String email) 
{ 
    ContentValues content = new ContentValues(); 
    content.put(NAME, name); 
    content.put(EMAIL, email); 
    return db.insertOrThrow(TABLE_NAME, null, content); 
} 

public Cursor returnData() 
{ 
    return db.query(TABLE_NAME, new String[] {NAME, EMAIL}, null, null, null, null, null); 
} 
} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.test.MainActivity" 
tools:ignore="MergeRootFrame" > 

<EditText 
    android:id="@+id/name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="36dp" 
    android:ems="10" 
    android:hint="Enter name here" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/load" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignRight="@+id/save" 
    android:layout_marginBottom="81dp" 
    android:text="LOAD DATA" /> 

<EditText 
    android:id="@+id/email" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/name" 
    android:layout_below="@+id/name" 
    android:layout_marginTop="41dp" 
    android:ems="10" 
    android:hint="Enter email here" /> 

<Button 
    android:id="@+id/save" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/load" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="42dp" 
    android:text="SAVE DATA" /> 

</RelativeLayout> 

LogCat

05-11 14:09:06.363: E/AndroidRuntime(2259): FATAL EXCEPTION: main 
05-11 14:09:06.363: E/AndroidRuntime(2259): Process: com.example.test, PID: 2259 
05-11 14:09:06.363: E/AndroidRuntime(2259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.os.Handler.dispatchMessage(Handler.java:102) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.os.Looper.loop(Looper.java:136) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at java.lang.reflect.Method.invoke(Method.java:515) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at dalvik.system.NativeStart.main(Native Method) 
05-11 14:09:06.363: E/AndroidRuntime(2259): Caused by: java.lang.NullPointerException 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at com.example.test.MainActivity.onCreate(MainActivity.java:34) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.Activity.performCreate(Activity.java:5231) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
05-11 14:09:06.363: E/AndroidRuntime(2259):  ... 11 more 

Reposting DataHandler.java après modification

package com.example.test; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DataHandler { 
public static final String NAME = "name"; 
public static final String EMAIL = "email"; 
public static final String TABLE_NAME = "mytable"; 
public static final String DATA_BASE_NAME = "mydatabase"; 
public static final int DATABASE_VERSION = 1; 
public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null)"; 

DataBaseHelper dbhelper; 
Context ctx; 
SQLiteDatabase db; 
public DataHandler(Context ctx) 
{ 
    this.ctx = ctx; 
    dbhelper = new DataBaseHelper(ctx); 
} 

private static class DataBaseHelper extends SQLiteOpenHelper 
{ 
    public DataBaseHelper(Context ctx) 
    { 
     super(ctx, DATA_BASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) 
    { 
     try{ 
     db.execSQL(TABLE_CREATE); 
     } 
     catch(SQLException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     db.execSQL("DROP TABLE IF EXISTS mytable"); 
     onCreate(db); 

    } 

} 

public DataHandler open() 
{ 
    db = dbhelper.getWritableDatabase(); 
    return this; 
} 

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

public long insertData(String name, String email) 
{ 
    ContentValues content = new ContentValues(); 
    content.put(NAME, name); 
    content.put(EMAIL, email); 
    return db.insertOrThrow(TABLE_NAME, null, content); 
} 

public Cursor returnData() 
{ 
    return db.query(TABLE_NAME, new String[] {NAME, EMAIL}, null, null, null, null, null); 
} 
} 
+0

Je suppose qu'il est missplace de ''} support dans la méthode onCreate() – Kedarnath

+0

Vérifier si 'activity_main' la disposition contient le bouton 'save'. Utilisez également 'return true;' dans votre 'OnTouchListener'. Enfin, supprimez 'public void onClick (View v) { }' depuis votre 'save.setOnTouchListener' – SathishKumar

+0

@SathishKumar Cela aide à démarrer l'application. Mais dès que j'entre le nom et l'email et que je clique sur save, ça plante. Sur LogCat, il est dit 05-12 00: 19: 47.393: E/AndroidRuntime (3048): android.database.sqlite.SQLiteException: pas une telle table: mytable (code 1):, lors de la compilation: INSERT INTO mytable (email, nom) VALUES (?,?) – rde

Répondre

0

Toutes les choses que vous avez fait correctement, mais manqué une petite chose dans votre onCreate() de DataHandler.java.

@Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      try{ 
      //db.execSQL("TABLE_CREATE"); 
      // Here you passing `TABLE_CREATE` string instead of passing `TABLE_CREATE` variable's string. So kindly remove quotes surrounded by 'TABLE_CREATE' and try. 
       db.execSQL(TABLE_CREATE); 
      } 
      catch(SQLException e) 
      { 
       e.printStackTrace(); 
      } 
     } 

Et changer cela,

public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null);";

à

public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null)";

+0

Cela donne toujours la même erreur. – rde

+0

Supprimer ';' à la fin de 'TABLE_CREATE' – SathishKumar

+0

J'ai supprimé le; à la fin de la requête TABLE_CREATE, j'obtiens pourtant la même erreur: 05-12 00: 51: 48.673: E/AndroidRuntime (3225): android.database.sqlite.SQLiteException: aucune table de ce type: mytable (code 1):, lors de la compilation: INSERT INTO mytable (email, nom) VALEURS (?,?) – rde

1

Il ne semble pas un problème lié à SQLite.

La trace que vous dit l'erreur courant se produit à la ligne 34:

save.setOnTouchListener(new OnTouchListener(){ 

Ainsi, votre bouton save doit être null lorsque vous essayez d'appliquer l'événement.

Vérifiez si la ligne 29 est en fait, vous et l'instance de retour:

save = (Button)findViewById(R.id.save); 
+0

Merci.Je ne suis pas sûr de comprendre ce que vous suggérez. – rde

+0

@rde A partir du code que vous avez posté, je pensais que le bouton Enregistrer est nul lorsque vous essayez d'appliquer l'événement, sur la ligne 34. Ce que je suggère est que vous assurez que le bouton Enregistrer n'est pas nul lorsque vous appliquez le événement, puisque la trace le suggère, et le bouton est dinamiquement référencé. – BonanzaOne

+0

En fait, il y avait un problème avec mon activity_main.xml, c'est pourquoi le bouton Save est devenu nul. Je l'ai édité. – rde

0

s'il vous plaît utiliser ce ..

save.setOnTouchListener(new View.OnTouchListener() { 

    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     String getName = name.getText().toString(); 
     String getEmail = email.getText().toString(); 
     handler = new DataHandler(getBaseContext()); 
     handler.open(); 
     long id = handler.insertData(getName, getEmail); 
     Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show(); 
     handler.close(); 
     return false; 
      } 
     }); 

Je pense que votre OnTouch() semble problem.please mettre mon code au lieu de votre code .. il peut être travail ..

+0

Merci. Mais cela ne semble pas aider. Je reçois toujours la même erreur. – rde

+0

qui vous ligne obtenir l'erreur? – dipali

Questions connexes