Question

Good day every body !

I'm a beginner in android development and I have some troubles when I switch from my second activity to my third one : I got a force clause caused by a NullPointerExeception in the onCreate function in the third one. I read the stack trace and I figured out that my problem come from the line 60 in my second activity.

name_server = thisIntent.getExtras().getString("ip");

I don't understand why and so I don't know how to fix it :/

Here are sources :

First activity :

package Smart.Wifi.Mote.v2;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SmartWifiMotev22 extends Activity 
{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        


    /**********************************
           Clic sur "connexion"
    **********************************/
    OnClickListener clic = new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // déclaration de la nouvelle fenêtre
            // @1 : fenêtre actuelle
            // @2 : nouvelle fenêtre
            Intent newpage = new Intent(SmartWifiMotev22.this, mainActivityv22.class);
            System.out.println("lancement fenêtre");

            // récupération de la valeur contenue dans la zone de texte (adresse ip)
            EditText a_IP = (EditText) findViewById(R.id.T_IP);
            String adresse_IP = a_IP.getText().toString();

            System.out.println("adresse_IP : " + adresse_IP);


            // ajout des valeurs à l'intent (fenêtre)
            newpage.putExtra("ip", adresse_IP);

            startActivity(newpage); // lancement de la fenêtre
                    }


    } //


} // 

The second one :

package Smart.Wifi.Mote.v2;

import java.io.IOException;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class mainActivityv22 extends Activity 
{

public String name_server = null;
public Socket s = null;
public String message = null;

public float startXPosition;
public float startYPosition;
public float endXPosition;
public float endYPosition;
public int moveX, moveY;

public Thread cThread;
public Thread sendThread;
public Thread alive;
public Intent thisIntent;
public boolean stopconnection = false;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    thisIntent = getIntent();

    // récupération de l'adresse IP envoyé en paramètre via intent.putExtra()
    name_server = thisIntent.getExtras().getString("ip");
    System.out.println("adresse page2 : " + name_server);
} // public void onCreate(Bundle savedInstanceState) 

public boolean onCreateOptionsMenu(Menu menu) 
{
    //Création d'un MenuInflater qui va permettre d'instancier un Menu XML en un objet Menu
    MenuInflater inflater = getMenuInflater();
    //Instanciation du menu XML spécifier en un objet Menu
    inflater.inflate(R.menu.menu, menu);

    return true;
 }

   //Méthode qui se déclenchera au clic sur un item
  public boolean onOptionsItemSelected(MenuItem item) 
  {
     //On regarde quel item a été cliqué grâce à  son id et on déclenche une action
     switch (item.getItemId()) 
     {
        case R.id.Internet:
            //((TextView)findViewById(R.id.texte)).setText("Option"); 
            break;

        case R.id.Presentation:
            Intent intent_presentenation = new Intent(mainActivityv22.this, Presentationv22.class);
            startActivity(intent_presentenation);               
            break;


     }
     return true;
  }

And the last one :

package Smart.Wifi.Mote.v2;

import android.os.Bundle;
...
import android.widget.ImageButton;

public class Presentationv22 extends mainActivityv22
{

@Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("Activity Presentation §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§");
    System.out.println("Constructeur classe mère");
    super.onCreate(savedInstanceState);
    System.out.println("Affichage");
    setContentView(R.layout.presentation);

    /*********************************
           Appui sur Start
    *********************************/
    OnClickListener Start = new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub              
        }
    };

    ImageButton start = (ImageButton) findViewById(R.id.ibStart);
    start.setOnClickListener(Start);

    /*********************************
          Appui sur Stop
    *********************************/
    OnClickListener Stop = new OnClickListener()
       {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        }
    };

    ImageButton stop = (ImageButton) findViewById(R.id.ibStop);
    stop.setOnClickListener(Stop);

        }


        /**********************************
                       Menu
        **********************************/
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu, menu);
            return true;
        }
    }

Thanks for your help :)

P.S : the stack trace

02-19 16:54:43.085: I/System.out(16968): Activity Presentation §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§
02-19 16:54:43.085: I/System.out(16968): Constructeur classe mère
02-19 16:54:43.095: D/AndroidRuntime(16968): Shutting down VM
02-19 16:54:43.095: W/dalvikvm(16968): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
02-19 16:54:43.105: E/AndroidRuntime(16968): FATAL EXCEPTION: main
02-19 16:54:43.105: E/AndroidRuntime(16968): java.lang.RuntimeException: Unable to start activity ComponentInfo{Smart.Wifi.Mote.v2/Smart.Wifi.Mote.v2.Presentationv22}: java.lang.NullPointerException
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.os.Looper.loop(Looper.java:130)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.app.ActivityThread.main(ActivityThread.java:3701)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at java.lang.reflect.Method.invokeNative(Native Method)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at java.lang.reflect.Method.invoke(Method.java:507)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at dalvik.system.NativeStart.main(Native Method)
02-19 16:54:43.105: E/AndroidRuntime(16968): Caused by: java.lang.NullPointerException
02-19 16:54:43.105: E/AndroidRuntime(16968):    at Smart.Wifi.Mote.v2.mainActivityv22.onCreate(mainActivityv22.java:60)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at Smart.Wifi.Mote.v2.Presentationv22.onCreate(Presentationv22.java:19)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 16:54:43.105: E/AndroidRuntime(16968):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
02-19 16:54:43.105: E/AndroidRuntime(16968):    ... 11 more
Was it helpful?

Solution

Your third activity calls super.onCreate() which means mainActivityv22's onCreate() is being called where it tries to get the extra String from the Intent. But there isnt any because this activity, the third activity wasnt started with an intent that has one.

You should remove the super.onCreate() from your third activity and then try.

Also I dont really know why you extended mainActivityv22. It wouldve been simpler to just extend Activity. I cant see any code that you're reusing.

EDIT:

As Sam pointed out , I was wrong. Checking for null in mainActivityv22's onCreate() would be your best bet then.

OTHER TIPS

Not every Activity is started with an Intent or extras, you should check whether these values are null before trying to access them.

thisIntent = getIntent();
if(thisIntent != null && thisIntent.getExtras() != null) {
    name_server = thisIntent.getExtras().getString("ip");
}
else { // Add a default case like this if you want
    name_server = "";
}

In this section:

        case R.id.Presentation:
        Intent intent_presentenation = new Intent(mainActivityv22.this, Presentationv22.class);
        startActivity(intent_presentenation);               
        break;

You are starting the activity without the String extra "ip". I'm not sure but it's possible that when you call the Presentationv22 activity, the onCreate tries to access to this extra on super() method. Could you try to add the extra to the above intent for testing purposes?

To avoid getting an error like you're getting, you should first check your Intent for Extras like

Bundle getExtraData = getIntent().getExtras();
String blah;
if (getData != null) {
    blah= getExtraData .getString("blah");
}

Mostly the problem seems to be because you're instancing Presentationv22 without the correct extra content (which you should be testing for anyway). Since it derives from your other class, it is launching the exception:

case R.id.Presentation:
        Intent intent_presentenation = new Intent(mainActivityv22.this, Presentationv22.class);
        startActivity(intent_presentenation); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top