0

it's my first time working on android studio for a college project and i'm getiting an error, when I try to sing up i get an exception, this is the client activity which displays an error:

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ClientsList extends AppCompatActivity {
    ListView ls;
    Helper h=new Helper(ClientsList.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clients_list);
        ls.findViewById(R.id.clientList);

        Cursor c=h.getAllClient();
        SimpleCursorAdapter adapter=new SimpleCursorAdapter(ClientsList.this,R.layout.client_item,c,
                new String[]{c.getColumnName(1),c.getColumnName(2),
                        c.getColumnName(3)},new int[]{R.id.textViewNom,R.id.textViewPrenom,R.id.textViewCIN},1);
        ls.setAdapter(adapter);
        ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView t=view.findViewById(R.id.textViewNom);
                Intent x=new Intent(ClientsList.this,DetailClient.class);
                x.putExtra("nom",t.getText().toString());
                startActivity(x);
            }
        });
    }
}

the error i get is :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.ClientsList}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ListView.findViewById(int)' on a null object reference

on line 21 of my client code which is:

ls.findViewById(R.id.clientList);

Any help will be apperciated

forpas
  • 160,666
  • 10
  • 38
  • 76
  • Show me your layout.xml file – Usama Altaf Mar 30 '21 at 10:47
  • 2
    why are you using your list view to find itself ? `ls = findViewById(R.id.clientList);` is what you're looking for – a_local_nobody Mar 30 '21 at 10:49
  • 1
    as @a_local_nobody said When you do ls.findViewById(R.id.clientList) it looks inside ls(listview), that is an instance of view, if there is a view having list has id. If there is it return it, otherwise it returns null. – Usama Altaf Mar 30 '21 at 10:52

1 Answers1

0

Change the line ls.findViewById(R.id.clientList) to ls=findViewById(R.id.clientList) then it works

Lathesh
  • 340
  • 1
  • 7