0

Im trying to run my application but every time i get this error, i tried a lot of solutions from stack overflow but none worked for me. i really need some help with that. i want to display in screen two text zone so the user can add name and class of the student then it will display all the student in a list, there is a lot of other activities in this project but the problem appears in this one.

This is my main activity:

package com.example.ds2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText nom,classe;
    Button add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nom=findViewById(R.id.nom);
        classe=findViewById(R.id.classe);
        add=findViewById(R.id.add);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i= new Intent(getApplicationContext(),StudentsActivity.class);
                i.putExtra("nome",nom.getText().toString());
                i.putExtra("classe",classe.getText().toString());
                startActivity(i);

            }
        });

    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/nom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Nom étudiant"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/classe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="classe étudiant"
        android:inputType="textPersonName"
        android:minHeight="48dp" />

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Ajouter" />
</LinearLayout>

StudentActivity:

package com.example.ds2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.HashMap;

public class StudentsActivity extends AppCompatActivity {

    ListView ls;
    String nom,classe;
    HashMap<String,String> map;
    Params p= new Params();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_students);

        ls.findViewById(R.id.lst);

        Bundle extras=getIntent().getExtras();
        if(extras!=null)
        {
            nom=extras.getString("nom");
            classe= extras.getString("classe");
            map=new HashMap<String,String>();
            map.put("nom",nom);
            map.put("classe",classe);
            p.values.add(map);
        }

        SimpleAdapter adapter= new SimpleAdapter(StudentsActivity.this,p.values,R.layout.item,
                new String[]{"nom","classe"},
                new int[]{R.id.nom,R.id.classe}
                );
        ls.setAdapter(adapter);

        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                Intent i = new Intent(getApplicationContext(),DetailActivity.class);
                i.putExtra("position",position);
                startActivity(i);

            }
        });

    }
}

activity_students.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StudentsActivity">

    <ListView
        android:id="@+id/lst"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Log error:

2022-05-28 17:37:12.163 28932-28932/com.example.ds2 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ds2, PID: 28932
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ds2/com.example.ds2.StudentsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ListView.findViewById(int)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3539)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3699)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2135)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:236)
        at android.app.ActivityThread.main(ActivityThread.java:8037)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ListView.findViewById(int)' on a null object reference
        at com.example.ds2.StudentsActivity.onCreate(StudentsActivity.java:26)
        at android.app.Activity.performCreate(Activity.java:8157)
        at android.app.Activity.performCreate(Activity.java:8129)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3512)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3699) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2135) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:236) 
        at android.app.ActivityThread.main(ActivityThread.java:8037) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
Ammar Abdullah
  • 802
  • 4
  • 8
  • 21
  • 2
    You are calling `findViewById()` on `ls`. `ls` is `null`, because you never assigned a value to it. Presumably, you should call `findViewById()` on the activity itself, the way that you are in `MainActivity`. – CommonsWare May 28 '22 at 16:49
  • thank you for your response but i didn't understand you well, you mean should i put `ls.findViewById(R.id.lst);` in the main activity? – ayoub touti May 28 '22 at 16:57
  • 1
    No, I mean that probably you should have `ls = findViewById(R.id.lst);`. – CommonsWare May 28 '22 at 16:58
  • now it worked perfectly ! i really appreciate it thank you so much you saved me. – ayoub touti May 28 '22 at 17:04
  • i wonder why you made this mistake, you did it correctly `nom=findViewById(R.id.nom);` `classe=findViewById(R.id.classe);` `add=findViewById(R.id.add);` at least 3 times previously, this is no different – a_local_nobody May 28 '22 at 17:07
  • i think its lack of attention mistake, coding at late night is tiring. – ayoub touti May 28 '22 at 17:11
  • 1
    that's fine, my comment was just there to point out that it works the same for all views, in case you were confused about why it wasn't working here – a_local_nobody May 28 '22 at 17:12
  • i got it, thanks for your concern brother – ayoub touti May 28 '22 at 17:14

0 Answers0