help-me

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

help-me

Postby pedro » Mon Jan 28, 2008 2:53 pm

Help me? I have two class, one is the main and the other is pra persist data in the database. But not ta working:

I'm getting a NullPointerException ...
Can see what is wrong in my code?

TesteDB.java (main)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test.DB;
  2.  
  3. import android.app.Activity;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import android.util.Log;
  8.  
  9. import android.view.View;
  10.  
  11. import android.widget.Button;
  12.  
  13. import android.widget.EditText;
  14.  
  15.  
  16.  
  17. public class TesteDB extends Activity {
  18.  
  19.      private EditText nome;
  20.  
  21.      private Banco banco = null;
  22.  
  23.    
  24.  
  25.     public void onCreate(Bundle icicle) {
  26.  
  27.         super.onCreate(icicle);
  28.  
  29.         setContentView(R.layout.main);
  30.  
  31.         Button botao = (Button)findViewById(R.id.ok);
  32.  
  33.         nome = (EditText)findViewById(R.id.nome);
  34.  
  35.         botao.setOnClickListener(new View.OnClickListener(){
  36.  
  37.           public void onClick(View arg0){
  38.  
  39.                //Log.e("Erro:",nome.getText().toString());
  40.  
  41.  
  42.  
  43.                banco.insereNome(nome.getText().toString());
  44.  
  45.           }
  46.  
  47.         });
  48.  
  49.     }
  50.  
  51. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



Banco.java (Database)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.test.DB;
  3.  
  4.  
  5.  
  6. import java.io.FileNotFoundException;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.content.ContentValues;
  13.  
  14. import android.database.sqlite.SQLiteDatabase;
  15.  
  16. import android.util.Log;
  17.  
  18.  
  19.  
  20. public class Banco extends Activity{
  21.  
  22.      
  23.  
  24.      private static final String nome_do_banco = "www.portalandroid.org";
  25.  
  26.      private static final int versao_db = 1;
  27.  
  28.      private static final String cria_tabela_pessoa = "create table pessoa (id integer primary key autoincrement, nome text not null);";
  29.  
  30.      private SQLiteDatabase db;  
  31.  
  32.  
  33.  
  34.      public Banco(){
  35.  
  36.           try{
  37.  
  38.                db=this.openDatabase(nome_do_banco, null);
  39.  
  40.           }catch (FileNotFoundException e) {
  41.  
  42.                try{
  43.  
  44.                     db = this.createDatabase(nome_do_banco, versao_db, 0, null);
  45.  
  46.                     db.execSQL(cria_tabela_pessoa);
  47.  
  48.                }catch (FileNotFoundException e1) {
  49.  
  50.                     db = null;
  51.  
  52.                }
  53.  
  54.           }
  55.  
  56.      }
  57.  
  58.  
  59.  
  60. public void insereNome(String nome){
  61.  
  62.      ContentValues valores = new ContentValues();
  63.  
  64.      valores.put("nome", nome);
  65.  
  66.      //conectaBanco();
  67.  
  68.      //String sql = "INSERT INTO pessoa (nome) VALUES ('"+nome+"');";
  69.  
  70.      try {
  71.  
  72.         //db.execSQL(sql);
  73.  
  74.           db.insert("pessoa", "nome", valores);
  75.  
  76.         Log.i("Inseriu",nome);
  77.  
  78.     } catch (Exception e) {
  79.  
  80.         Log.e("ERROR", e.toString());
  81.  
  82.     }
  83.  
  84.      
  85.  
  86. }
  87.  
  88.  
  89.  
  90. }
Parsed in 0.046 seconds, using GeSHi 1.0.8.4



thank's
Last edited by pedro on Fri Feb 01, 2008 3:05 am, edited 1 time in total.
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Top

Postby cabernet1976 » Thu Jan 31, 2008 3:30 am

Hi pedro,

You may want this tutorials: viewtopic.php?p=1852
About your code, you should have a method onCreate for Banco, for it extends Activity.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby pedro » Thu Jan 31, 2008 4:10 am

cabernet1976 wrote:Hi pedro,

You may want this tutorials: viewtopic.php?p=1852
About your code, you should have a method onCreate for Banco, for it extends Activity.


Ok, I know is wrong, the more my problem is how I think I continue to access the data? I have a main activity, there I have a textfield and when I enter an information and when I click a button, go to class bank and saves the data. You know how can I make this work?
É usuário de língua portuguesa? então visite: http://www.portalandroid.org <-- Visit
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Postby cabernet1976 » Fri Feb 01, 2008 2:32 am

Hi pedro,
You can open database in your class bank, and save text to table. In fact, you can operate database everywhere.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby pedro » Fri Feb 01, 2008 3:03 am

cabernet1976 wrote:Hi pedro,
You can open database in your class bank, and save text to table. In fact, you can operate database everywhere.


Can solve this problem for me? Already tried several ways and not satisfied
É usuário de língua portuguesa? então visite: http://www.portalandroid.org <-- Visit
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Postby plusminus » Fri Feb 01, 2008 11:28 am

Hello pedro,

you want to make your Banco.java do just DataBase-Stuff and no GUI, right :?:
:arrow: So extending Banco from Activity makes no sense (just remove "extends Activity"):!:

You receive a NullPointerException because you try to call a method (.insereNome()) on a Object that is still null:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.      private Banco banco = null;
  2.    
  3.     public void onCreate(Bundle icicle) {
  4.     //...
  5.           public void onClick(View arg0){
  6.                
  7.                banco.insereNome(nome.getText().toString());
  8.           }
  9.     //..
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

:arrow: So you need to create a Banco-Object somewhere:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private Banco banco = null;
  2. // Change to:
  3. private Banco banco = new Banco();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby pedro » Fri Feb 01, 2008 2:18 pm

If I remove Activity Bank of class it does not detect this part of the code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. db=this.openDatabase(nome_do_banco, null); //error openDatabase
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. db = this.createDatabase(nome_do_banco, versao_db, 0, null); //error createDatabase
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
É usuário de língua portuguesa? então visite: http://www.portalandroid.org <-- Visit
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Postby plusminus » Fri Feb 01, 2008 4:38 pm

Hello pedro,

I didn't get what you meant with "it does not detect this part of the code".
Post your current code and I'll have a look...

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby pedro » Fri Feb 01, 2008 5:24 pm

plusminus wrote:Hello pedro,

I didn't get what you meant with "it does not detect this part of the code".
Post your current code and I'll have a look...

Regards,
plusminus



I want to do the following! I have my class activity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.teste.DB;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8.  
  9. public class TesteDB extends Activity {
  10.         private EditText nome;
  11.         private Banco banco = new Banco();
  12.    
  13.     public void onCreate(Bundle icicle) {
  14.         super.onCreate(icicle);
  15.         setContentView(R.layout.main);
  16.         Button botao = (Button)findViewById(R.id.ok);
  17.         nome = (EditText)findViewById(R.id.nome);
  18.         botao.setOnClickListener(new View.OnClickListener(){
  19.                 public void onClick(View arg0){
  20.                         banco.insereNome(nome.getText().toString());
  21.                 }
  22.         });
  23.     }
  24. }
  25.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


class of database:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.teste.DB;
  2.  
  3. import java.io.FileNotFoundException;
  4.  
  5. import android.app.Activity;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.util.Log;
  10.  
  11. public class Banco{
  12.        
  13.         private static final String nome_do_banco = "teste";
  14.         private static final int versao_db = 1;
  15.         private static final String cria_tabela_pessoa = "create table pessoa (id integer primary key autoincrement, nome text not null);";
  16.         private SQLiteDatabase db;     
  17.  
  18.         public Banco(){
  19.                 try{
  20.                         db=this.openDatabase(nome_do_banco, null);        //   Here is a error
  21.                 }catch (FileNotFoundException e) {
  22.                         try{
  23.                                 db = this.createDatabase(nome_do_banco, versao_db, 0, null);     //Here is a error
  24.                                 db.execSQL(cria_tabela_pessoa);
  25.                         }catch (FileNotFoundException e1) {
  26.                                 db = null;
  27.                         }
  28.                 }
  29.         }
  30.  
  31. public void insereNome(String nome){
  32.         ContentValues valores = new ContentValues();
  33.         valores.put("nome", nome);
  34.         try {
  35.                 db.insert("pessoa", "nome", valores);
  36.         Log.i("Inseriu",nome);
  37.     } catch (Exception e) {
  38.         Log.e("ERROR", e.toString());
  39.     }
  40.        
  41. }
  42.        
  43. }
  44.  
  45.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4

In that class I have a textfield and a button, I want to enter a textfield in the text and click the save. When I do this, I want go to class bank and saves the text in the database. Someone looking at my code can solve this?
É usuário de língua portuguesa? então visite: http://www.portalandroid.org <-- Visit
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Postby plusminus » Fri Feb 01, 2008 8:34 pm

aaah Compile-Errors,

try the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public Banco(Context ctx) {
  2.                 try {
  3.                         db = ctx.openDatabase(nome_do_banco, null); //   Here is a error
  4.                 } catch (FileNotFoundException e) {
  5.                         try {
  6.                                 db = ctx.createDatabase(nome_do_banco, versao_db, 0, null); //Here is a error
  7.                                 db.execSQL(cria_tabela_pessoa);
  8.                         } catch (FileNotFoundException e1) {
  9.                                 db = null;
  10.                         }
  11.                 }
  12.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


I'm not 100% sure, but in the create-table sql statement it should be: "AUTO_INCREMENT" instead of "autoincrement". I would also use an "IF NOT EXISTS".
So final SQL-String is:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static final String cria_tabela_pessoa = "CREATE TABLE IF NOT EXISTS pessoa (id INTEGER PRIMARY KEY AUTO_INCREMENT, nome TEXT NOT NULL);";
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby pedro » Sat Feb 02, 2008 1:19 am

My code was thus further presents mistake when they do.

The emulator complains of context.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class TesteDB extends Activity {
  3.  
  4.         private EditText nome;
  5.  
  6.         private Banco banco = new Banco(this);                // <---- I can do that???????
  7.  
  8.    
  9.  
  10.     public void onCreate(Bundle icicle) {
  11.  
  12.         super.onCreate(icicle);
  13.  
  14.         setContentView(R.layout.main);
  15.  
  16.         Button botao = (Button)findViewById(R.id.ok);
  17.  
  18.         nome = (EditText)findViewById(R.id.nome);
  19.  
  20.         botao.setOnClickListener(new View.OnClickListener(){
  21.  
  22.                 public void onClick(View arg0){
  23.  
  24.                         //Log.e("Erro:",nome.getText().toString());
  25.  
  26.                         //banco.criaBanco();
  27.  
  28.                         banco.insereNome(nome.getText().toString());
  29.  
  30.                 }
  31.  
  32.         });
  33.  
  34.     }
  35.  
  36.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Banco{
  3.  
  4.        
  5.  
  6.         private static final String nome_do_banco = "portalandroid";
  7.  
  8.         private static final int versao_db = 1;
  9.  
  10.         private static final String cria_tabela_pessoa = "CREATE TABLE IF NOT EXISTS pessoa (id INTEGER PRIMARY KEY AUTO_INCREMENT, nome TEXT NOT NULL);";
  11.  
  12.         private SQLiteDatabase db;     
  13.  
  14.  
  15.  
  16.         public Banco(Context con){
  17.  
  18.                 try{
  19.  
  20.                         db=con.openDatabase(nome_do_banco, null);
  21.  
  22.                 }catch (FileNotFoundException e) {
  23.  
  24.                         try{
  25.  
  26.                                 db = con.createDatabase(nome_do_banco, versao_db, 0, null);
  27.  
  28.                                 db.execSQL(cria_tabela_pessoa);
  29.  
  30.                         }catch (FileNotFoundException e1) {
  31.  
  32.                                 db = null;
  33.  
  34.                         }
  35.  
  36.                 }
  37.  
  38.         }
  39.  
  40.  
  41.  
  42. public void insereNome(String nome){
  43.  
  44.         ContentValues valores = new ContentValues();
  45.  
  46.         valores.put("nome", nome);
  47.  
  48.         //conectaBanco();
  49.  
  50.         //String sql = "INSERT INTO pessoa (nome) VALUES ('"+nome+"');";
  51.  
  52.         try {
  53.  
  54.         //db.execSQL(sql);
  55.  
  56.                 db.insert("pessoa", "nome", valores);
  57.  
  58.         Log.i("Inseriu",nome);
  59.  
  60.     } catch (Exception e) {
  61.  
  62.         Log.e("ERROR", e.toString());
  63.  
  64.     }
  65.  
  66.        
  67.  
  68. }
  69.  
  70.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
É usuário de língua portuguesa? então visite: http://www.portalandroid.org <-- Visit
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Postby plusminus » Sat Feb 02, 2008 11:26 am

Hello pedro,

found some minutes to put your code to my Eclipse and fixed it works with me now :!: (would have done that earlier but I'm pretty busy this weeks):
Proof:
Image

This is the code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.TesteDB;
  2.  
  3. import java.io.FileNotFoundException;
  4.  
  5. import android.app.Activity;
  6. import android.content.ContentValues;
  7. import android.content.Context;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.os.Bundle;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14.  
  15. public class TesteDB extends Activity {
  16.  
  17.         private EditText nome;
  18.         private Banco banco; // <---- I can do
  19.  
  20.         // that???????
  21.  
  22.         public void onCreate(Bundle icicle) {
  23.                 super.onCreate(icicle);
  24.                 setContentView(R.layout.main);
  25.                  this.banco = new Banco(this);
  26.                 Button botao = (Button) findViewById(R.id.ok);
  27.                 nome = (EditText) findViewById(R.id.nome);
  28.                 botao.setOnClickListener(new View.OnClickListener() {
  29.  
  30.                         public void onClick(View arg0) {
  31.                                 // Log.e("Erro:",nome.getText().toString());
  32.                                 // banco.criaBanco();
  33.                                 banco.insereNome(nome.getText().toString());
  34.                         }
  35.                 });
  36.         }
  37. }
  38.  
  39. class Banco {
  40.  
  41.         private static final String nome_do_banco = "portalandroid";
  42.         private static final int versao_db = 1;
  43.         private static final String cria_tabela_pessoa = "CREATE TABLE IF NOT EXISTS pessoa (id INTEGER PRIMARY KEY AUTOINCREMENT, nome TEXT NOT NULL);";
  44.         private SQLiteDatabase db;
  45.  
  46.         public Banco(Context con) {
  47.                 try {
  48.                         db = con.openDatabase(nome_do_banco, null);
  49.                 } catch (FileNotFoundException e) {
  50.                         try {
  51.                                 db = con.createDatabase(nome_do_banco, versao_db, 0, null);
  52.                                 db.execSQL(cria_tabela_pessoa);
  53.                         } catch (FileNotFoundException e1) {
  54.                                 db = null;
  55.                         }
  56.                 }
  57.         }
  58.  
  59.         public void insereNome(String nome) {
  60.                 ContentValues valores = new ContentValues();
  61.                 valores.put("nome", nome);
  62.                 // conectaBanco();
  63.                 // String sql = "INSERT INTO pessoa (nome) VALUES ('"+nome+"');";
  64.                 try {
  65.                         // db.execSQL(sql);
  66.                         db.insert("pessoa", "nome", valores);
  67.                         Log.i("Inseriu", nome);
  68.                 } catch (Exception e) {
  69.                         Log.e("ERROR", e.toString());
  70.                 }
  71.  
  72.         }
  73. }
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby pedro » Sat Feb 02, 2008 2:28 pm

I wanted to use the class bank separate from any other code, how can I do that?
É usuário de língua portuguesa? então visite: http://www.portalandroid.org <-- Visit
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Postby plusminus » Sat Feb 02, 2008 2:33 pm

Hello pedro,

put a public in front of "class Banco{" and put it into its own file "Banco.java".

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby pedro » Sat Feb 02, 2008 2:35 pm

plusminus wrote:Hello pedro,

put a public in front of "class Banco{" and put it into its own file "Banco.java".

Regards,
plusminus


But I doing this way you spoke the code that you did not work!!!
É usuário de língua portuguesa? então visite: http://www.portalandroid.org <-- Visit
pedro
Developer
Developer
 
Posts: 29
Joined: Mon Jan 28, 2008 2:48 pm

Top
Next

Return to General

Who is online

Users browsing this forum: Google Feedfetcher and 2 guests