0

When i try to open a activity in my app which its suposed to show some saved messages(that i saved in another activity in the same app) it Force to Close and shows me these errors in LogCat:

  10-08 18:22:17.243: E/AndroidRuntime(269): FATAL EXCEPTION: main
  10-08 18:22:17.243: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mensagem/com.example.mensagem.Mensagenssalvas}: java.lang.NullPointerException
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.os.Handler.dispatchMessage(Handler.java:99)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.os.Looper.loop(Looper.java:123)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.app.ActivityThread.main(ActivityThread.java:4627)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at java.lang.reflect.Method.invoke(Method.java:521)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at dalvik.system.NativeStart.main(Native Method)
  10-08 18:22:17.243: E/AndroidRuntime(269): Caused by: java.lang.NullPointerException
  10-08 18:22:17.243: E/AndroidRuntime(269):    at com.example.mensagem.Mensagenssalvas.Mensagens(Mensagenssalvas.java:41)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at com.example.mensagem.Mensagenssalvas.onCreate(Mensagenssalvas.java:26)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  10-08 18:22:17.243: E/AndroidRuntime(269):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  10-08 18:22:17.243: E/AndroidRuntime(269):    ... 11 more

Before i use the code in this LINK it was normal but after i used the code, my databasecolumn is now not opening in the activity i try to open.(which is meant to show the messages).

Mensagenssalvas.java

 public class Mensagenssalvas extends Activity {

    public SQLiteDatabase db;

    ArrayList<String> list = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mensagenssalvas);
    db = openOrCreateDatabase("banco.db", Context.MODE_WORLD_WRITEABLE, null);

    Mensagens();
}

private void Mensagens() {
    // TODO Auto-generated method stub

    ListView user = (ListView) findViewById(R.id.lvMensagens);

    String[] campos = new String[] {"mensagemsalva", "mensagemenviada"};

    list = new ArrayList<String>();
    Cursor c = db.query( "mensagens", campos, null, null, null, null, null);
    c.moveToFirst();
    if(c.getCount() > 0) {
        while(true) {
           list.add(c.getString(c.getColumnIndex("mensagemsalva")).toString());
            if(!c.moveToNext()) break;
        }
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, list);

    user.setAdapter(adapter);

   }
}
Community
  • 1
  • 1
baTimá
  • 554
  • 1
  • 10
  • 28
  • Well its a NullPointerException. Would need to see the actual code behind it to know more. – Connor Tumbleson Oct 08 '12 at 18:33
  • alright, but i havent changed the code in the activity, i have only setted the column to NULL i guess, check the link to know what i have done to the column that saves/shows the messages in the question. – baTimá Oct 08 '12 at 18:34
  • What is the code at line 41 of `Mensagenssalvas.java`? – Renato Lochetti Oct 08 '12 at 18:34
  • 1
    `at com.example.mensagem.Mensagenssalvas.Mensagens(Mensagenssalvas.java:41)` - find 41 line in Mensagenssalvas file. Something is null there. – Michał Klimczak Oct 08 '12 at 18:34
  • OH, so check the LINK in the question, i setted the column to NULL (trying to clear all the value in it, but something went wrong then i guess), so now the question will be, how to fix it? – baTimá Oct 08 '12 at 18:35
  • To detect what's wrong with your code, post your code. Is it so hard? – azizbekian Oct 08 '12 at 18:39
  • have you read what i said? I created a button to set my column to NULL, therefore the column is NULL, the question now will be how to fix it, and i will post the code. – baTimá Oct 08 '12 at 18:40
  • Sir, how can we find out, where is the mysterious 41th line? – azizbekian Oct 08 '12 at 18:46
  • In LogCat or in the .Java Code? – baTimá Oct 08 '12 at 18:47
  • In logcat we have `at com.example.mensagem.Mensagenssalvas.Mensagens(Mensagenssalvas.java:41)` , I guess we need `Mensagenssalvas.java`'s 41th line. You can comment the 41th line, just for the users of stackoverflow to know that it is the 41th line. – azizbekian Oct 08 '12 at 18:49
  • the 41th line is: `list.add(c.getString(c.getColumnIndex("mensagemsalva")).toString());` but as I said and im saying again for the 4th time, i SETTED my column to NULL through a button click, check the answer below, now i setted the column to `""` and it works fine, thanks for trying to help. – baTimá Oct 08 '12 at 18:53

1 Answers1

1

You need to check line 41 of your Mensagenssalvas.java and find out what caused the NullPointerException.

In many cases you just forgot to initialize it.

to fix: Do not set what ever you want to use to NULL, in stead, set it to 0, or "", depending on what type it is. You need to set to something that has a "EMPTY" or "ZERO" value, but not NULL.

fangmobile
  • 839
  • 8
  • 23
  • 1
    read comments. it shows why it shows Null. so now i the question will be how to fix it. – baTimá Oct 08 '12 at 18:38
  • Thanks, i setted the Column to `""` through a button, so now it works properly, thank you for you help. But now when i try to save the messages(in another activity) is not showing in the activity i setted the column to `""` – baTimá Oct 08 '12 at 18:52