Problems with insert in an Android project in Java

0

I'm new as a Java programmer for Android, I'm creating an application that he signs up for an email, in the image below he will register the email below and above the user will log in.

However when I try to register it generates an error in my save method which is the insert in the SQlite database table as shown below

    12-15 20:35:37.375: I/Process(1830): Sending signal. PID: 1830 SIG: 9
12-15 20:35:40.026: E/Trace(1880): error opening trace file: No such file or directory (2)
12-15 20:35:41.376: D/gralloc_goldfish(1880): Emulator without GPU emulation detected.
12-15 20:37:52.375: D/AndroidRuntime(1880): Shutting down VM
12-15 20:37:52.375: W/dalvikvm(1880): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
12-15 20:37:52.385: E/AndroidRuntime(1880): FATAL EXCEPTION: main
12-15 20:37:52.385: E/AndroidRuntime(1880): java.lang.NullPointerException
12-15 20:37:52.385: E/AndroidRuntime(1880): at com.example.projetologin.repositorio.RepositorioUsuario.salvar(RepositorioUsuario.java:42)
12-15 20:37:52.385: E/AndroidRuntime(1880): at com.example.projetologin.repositorio.RepositorioUsuario.salvar(RepositorioUsuario.java:36)
12-15 20:37:52.385: E/AndroidRuntime(1880): at com.example.projetologin.xml.Login$2.onClick(Login.java:67)
12-15 20:37:52.385: E/AndroidRuntime(1880): at android.view.View.performClick(View.java:4204)
12-15 20:37:52.385: E/AndroidRuntime(1880): at android.view.View$PerformClick.run(View.java:17355)
12-15 20:37:52.385: E/AndroidRuntime(1880): at android.os.Handler.handleCallback(Handler.java:725)
12-15 20:37:52.385: E/AndroidRuntime(1880): at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 20:37:52.385: E/AndroidRuntime(1880): at android.os.Looper.loop(Looper.java:137)
12-15 20:37:52.385: E/AndroidRuntime(1880): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-15 20:37:52.385: E/AndroidRuntime(1880): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 20:37:52.385: E/AndroidRuntime(1880): at java.lang.reflect.Method.invoke(Method.java:511)
12-15 20:37:52.385: E/AndroidRuntime(1880): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-15 20:37:52.385: E/AndroidRuntime(1880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-15 20:37:52.385: E/AndroidRuntime(1880): at dalvik.system.NativeStart.main(Native Method)

Here is my project on Github

link

How do I fix this error?

    
asked by anonymous 16.12.2014 / 14:56

2 answers

1

The problem is at line 42 of RepositorioUsuario in function salvar :

long id = db.insert(NOME_TABELA, "", valores);

Because you have to initialize the database, you can not declare it:

protected SQLiteDatabase db;
    
16.12.2014 / 15:03
0

What you need @wladyband is just instantiate your SQLiteHelper class and use the getWriteableDatabase method. I'll put an example

The SQLiteOpenHelper class provides the getReadableDatabase () and getWriteableDatabase () methods to gain access to a SQLiteDatabase object; in the way of reading or writing.

Just instantiate as follows:

SQLiteHelper conexao = new SQLiteHelper(ctx);

Before your db.insert you use this method:

db = conexao.getWritableDatabase();

and in your constructor, put it like this:

public RepositorioUsuario(Context ctx) {
    this.ctx = ctx;
}
    
18.12.2014 / 19:18