Bonjour,
Je viens vers vous car cela fait plusieurs heures que je suis bloqué sur un problème. Mon application crash dès qu'il y a une écrire ou lecture dans la base de donnée
Erreur:
Voici mes 3 classes:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13 03-20 21:29:35.022 2723-2723/mypet.caranille.com.mypet E/AndroidRuntime: FATAL EXCEPTION: main Process: mypet.caranille.com.mypet, PID: 2723 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.database.sqlite.SQLiteDatabase.execSQL(java.lang.String)' on a null object reference at mypet.caranille.com.mypet.Class.PetManage.addPet(PetManage.java:36) at mypet.caranille.com.mypet.MainActivity$1.onClick(MainActivity.java:37) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:163) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5753) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package mypet.caranille.com.mypet.Class; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DDB extends SQLiteOpenHelper { private static final String name = "caranille.ddb"; private static final int version = 1; public DDB(Context context) { super(context, name, null, version); } public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE table Pet " + "(" + "Pet_ID integer primary key autoincrement," + "Pet_Name text not null," + "Pet_Hunger int not null," + "Pet_Thirst int not null" + ")"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 package mypet.caranille.com.mypet.Class; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import java.sql.SQLException; import mypet.caranille.com.mypet.Class.DDB; /** * Created by Jeremy on 12/03/2016. */ public class PetManage { private DDB ddb; private SQLiteDatabase database; public PetManage(Context context) { ddb = new DDB(context); } public void open() throws SQLException { database = ddb.getWritableDatabase(); } public void close() { ddb.close(); } public void addPet(String name) { database.execSQL("INSERT INTO Pet VALUES (null, '" + name + "', '100', '100')"); database.close(); } public int verifyPet() { int verify = 0; Cursor cursor = database.rawQuery("SELECT * FROM Pet", new String[]{new String()}); if (cursor.getCount() == 1) { verify = 1; } database.close(); return verify; } }Et voici l'activité principal:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 package mypet.caranille.com.mypet.Class; public class Pet { private int ID; private String Name; private int Hunger; private int Thirst; public void Pet(int id, String name, int hunger, int thirst) { ID = id; Name = name; Hunger = hunger; Thirst = thirst; } //GETTER public int getID() { return ID; } public String getName() { return Name; } public int getHunger() { return Hunger; } public int getThirst() { return Thirst; } //SETTER public void setID (int id) { ID = id; } public void setName (String name) { Name = name; } public void setHunger (int hunger) { Hunger = hunger; } public void setThirst (int thirst) { Thirst = thirst; } }
Merci de votre aide et toute critique et bonne à prendre
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 package mypet.caranille.com.mypet; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.EditText; import mypet.caranille.com.mypet.Class.PetManage; public class MainActivity extends AppCompatActivity { PetManage pm = new PetManage(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); int verify = pm.verifyPet(); if (verify == 0) { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("MyPet"); //Set Alert dialog title here alert.setMessage("Veuillez saisir le nom de votre nouveau compagnon !"); //Message here // Set an EditText view to get user input final EditText input = new EditText(this); alert.setView(input); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String name = input.getText().toString(); pm.addPet(name); } }); AlertDialog alertDialog = alert.create(); alertDialog.show(); } else { AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("MyPet"); //Set Alert dialog title here alert.setMessage("Animal existant"); //Message here AlertDialog alertDialog = alert.create(); alertDialog.show(); } } }![]()
Partager