Problems with Insert in an Android project in Java

Asked

Viewed 85 times

0

I am new as Java programmer for Android, I am creating an application that he register an email, in the image below he will register the email and above the user will login.

But when I try to register it generate an error in my save method that is the Insert in the table of the Sqlite database 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

https://github.com/wladyband/CadastroEmail

how do I correct this error?

inserir a descrição da imagem aqui

  • Your code does not appear here, put your save function and indicate which line 25.

  • @Jorgeb. Why line 25?? Didn’t you mean 42? 42 is the answer to everything, forgot?

  • I was wrong @Math ;)

  • java.lang.NullPointerException at com.example.projetologin.repositorio.RepositorioUsuario.salvar(RepositorioUsuario.java:42), yes, 42 is the answer to everything hehehe.

  • 24 is also a cabalistic number.

2 answers

1

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

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

Because if you don’t initialize the database, it’s not enough to declare it:

protected SQLiteDatabase db;
  • Sorry Jorge B you put the answer being the solution or just reported something I already knew? I do not know who came to read carefully in my post, but in my post I had already made it clear that I already knew where the problem was.

  • @wladyband the solution is to initialize db as I told you in the reply.

  • Okay, all right, I’ll ask you a question of reflection! you stopped to watch my project on Gitgub https://github.com/wladyband/CadastroEmail if you are looking at my project you will notice that I started the db variable please take a closer look at my project.

  • @wladyband in class RepositorioUsuario didn’t initialize no, you should be something like protected SQLiteDatabase db = new SQLiteDatabase();

  • Jorge B is not possible to initialize the way you have guided because the Sqlitedatabase class does not contain a constructor, and when I entered the Sqlitedatabase class to create a constructor the eclipse does not allow me to change the Sqlitedatabase class. try to help me please Jorge B

Show 1 more comment

0

What Voce needs @wladyband is to just instantiate its Sqlitehelper class and use the getWriteableDatabase method. I’ll put an example

The Sqliteopenhelper class provides getReadableDatabase() and getWriteableDatabase() methods to gain access to a Sqlitedatabase object; in read or write mode.

All you have to do is instantiate in the following way:

SQLiteHelper conexao = new SQLiteHelper(ctx);

before your db.Insert you use this method:

db = conexao.getWritableDatabase();

and in your builder, put like this:

public RepositorioUsuario(Context ctx) {
    this.ctx = ctx;
}
  • There are some considerations I must address, and I will speak with respect. It will not advance these suggestions, any suggestion that is put forward will have no enlightening effect if the people who want to help me do not download my application and test what they are guiding me, if it is not done it will be a help in vain, as is the case with this suggestion, putting the above lines of code has no practical effect, and it didn’t work, another thing is that I believe I will only try to find the solution by myself, because I need to understand how the integration of Java with the database works.

  • But I am waiting for someone who can give a significant help, and do not think that solving the problem for me will not learn. but will only be able to help download my application and test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.