-1

Hello I am trying to start activity from one to activity to other one but i get error

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bsine_000.baseinas/com.example.bsine_000.baseinas.registracija}: java.lang.NullPointerException

here is the code:

        public class MainActivity extends AppCompatActivity implements View.OnClickListener {

Button etprisijungti;
EditText etelpastas, etslaptazodis;
TextView reglink;
saugykla saugykla;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etprisijungti = (Button) findViewById(R.id.etprisijungti);
    etelpastas = (EditText) findViewById(R.id.etelpastas);
    etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
    reglink = (TextView) findViewById(R.id.reglink);
    reglink.setOnClickListener(this);
    etprisijungti.setOnClickListener(this);
    saugykla = new saugykla(this);
}

@Override
public void onClick(View v) {
    switch(v.getId()){

        case R.id.etprisijungti:
            vartotojas vartotojas = new vartotojas(null,null);
            saugykla.saugoti(vartotojas);
            saugykla.vartotojas_prisijunges(true);
            startActivity(new Intent(this, pagrindinis.class));
            break;

        case R.id.reglink:
            startActivity(new Intent(this, registracija.class));
            break;
    }

}
}

code where app crashes is

case R.id.reglink:

startActivity(new Intent(this, registracija.class));

break;

first case works fine code is exactly the same here is registracija class:

public class registracija extends AppCompatActivity implements View.OnClickListener {

Button etregistruotis;
EditText etelpastas, etslaptazodis;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_registracija);
    etregistruotis = (Button) findViewById(R.id.reglink)
    etelpastas = (EditText) findViewById(R.id.etelpastas);
    etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
    etregistruotis.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch(v.getId()){

        case R.id.etregistruotis:
            String elpastas = etelpastas.getText().toString();
            String slaptazdodis = etslaptazodis.getText().toString();
            break;

    }

}
}

Logcat :

    05-25 10:45:36.401  23721-23721/com.example.bsine_000.baseinas W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41241438)
05-25 10:45:36.411  23721-23721/com.example.bsine_000.baseinas E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bsine_000.baseinas/com.example.bsine_000.baseinas.registracija}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4960)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.bsine_000.baseinas.registracija.onCreate(registracija.java:23)
            at android.app.Activity.performCreate(Activity.java:5203)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4960)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

activity main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:padding="10dp"
    android:text="El.pastas"/>

<EditText
    android:id="@+id/etelpastas"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:padding="10dp"
    android:text="slaptazodis"/>
<EditText
    android:id="@+id/etslaptazodis"
    android:inputType="textPassword"
    android:layout_marginBottom="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<Button
    android:id="@+id/etprisijungti"
    android:text="Prisijungti"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:enabled="true" />
<TextView
    android:id="@+id/reglink"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:textStyle="bold"
    android:padding="10dp"
    android:layout_gravity="center_horizontal"
    android:text="Registruotis"
    android:clickable="true"
    android:linksClickable="true" />

Activity registracija

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:text="El.pastas"/>

    <EditText
        android:id="@+id/etelpastas"
        android:layout_width="match_parent"
        android:layout_marginBottom="10dp"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:padding="10dp"
        android:text="slaptazodis"/>
    <EditText
        android:id="@+id/etslaptazodis"
        android:inputType="textPassword"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/etregistruotis"
        android:text="Registruotis"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
JiTHiN
  • 6,548
  • 5
  • 43
  • 69
ben
  • 39
  • 1
  • 8
  • 1
    `etregistruotis` is null in the `onCreate()` method of `registracija`. The View you're setting the `OnClickListener` on is not the one you initialized in the previous line. – Mike M. May 25 '15 at 07:41
  • Can u give me more detail about this? Code is exactly the same but one works other does not. – ben May 25 '15 at 07:43
  • 1
    You need to initialize `etregistruotis` with `findViewById()`, like you did with the `EditText`s, before you call `setOnClickListener()` on it. – Mike M. May 25 '15 at 07:44
  • It has nothing to do with that. – ben May 25 '15 at 07:55
  • Stop downvoting. If u can solve my problem it does not mean u can downvote. – ben May 25 '15 at 07:57
  • You've mis-diagnosed which line is giving you the error - your stack trace shows that the null pointer is within the `onCreate()` method : `at com.example.bsine_000.baseinas.registracija.onCreate(registracija.java:23)` Work out what line that is (line 23) and you find what is giving you your error - whatever can be null on that line – J Richard Snape May 25 '15 at 08:25

3 Answers3

0

Try this: In your MainActivity replace this code:

startActivity(new Intent(this, registracija.class));

With this code:

startActivity(new Intent(MainActivity.this, registracija.class));

Hope it helps, if it didn't please post the code of activity_registracija.xml.

Edit: Reglink doesn't exist in your activity_registracija.xml.

Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
-1

As said in one comment, etregistruotis variable is not initialized. You need to do it like you do to the other two variables above and then only call the setOnClickListener.

If it still crashes after that please update your answer with the code you are using.

Bonus : your activity class starts with a lower case letter. By convention it should start with a capital letter.

Update: Reglink doesn't exist in the activity xml

Miguel Ribeiro
  • 8,057
  • 20
  • 51
  • 74
-1
     public class registracija extends AppCompatActivity implements              

View.OnClickListener {

         Button etregistruotis;
        EditText etelpastas, etslaptazodis;

       @Override
     protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registracija);
      etregistruotis=(Button)findViewById(R.id. etregistruotis);
      etelpastas = (EditText) findViewById(R.id.etelpastas);
     etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
           etregistruotis.setOnClickListener(this);

                }

    @Override
      public void onClick(View v) {

        String elpastas = etelpastas.getText().toString();
        String slaptazdodis = etslaptazodis.getText().toString();
        break;

    }

 }

please intialise the button on onCreate(). as you are making click on that same button no need to giva the id again in registracija.java.ithink this will resolve your error

NIDHI GABA
  • 35
  • 2