-2

When I call this Activity, a Null Pointer Exception occurs. Why does this happen?

package com.andrd.gps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class User_Detail_ListActivity extends Activity{
TransportData tdata;
private TextView drivernametxt,agetxt,addresstxt,liecencetxt,contactNotxt,driverTypetxt,truckNotxt,truckPermittxt,truckTypetxt,fromtxt,totxt;
Button editBtn,deleteBtn,cancelBtn;
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.user_list);


    drivernametxt = (TextView) findViewById(R.id.nameTextView);
    agetxt=(TextView) findViewById(R.id.ageTextView);
    addresstxt=(TextView) findViewById(R.id.addressTextView);
    liecencetxt=(TextView) findViewById(R.id.licenseNoTextView);
    contactNotxt=(TextView) findViewById(R.id.contactNoTextView);
    driverTypetxt=(TextView) findViewById(R.id.employmetnTypeTextView);
    truckNotxt=(TextView) findViewById(R.id.truckNoTextView);
    truckPermittxt=(TextView) findViewById(R.id.truckPermitTextView);
    truckPermittxt=(TextView) findViewById(R.id.truckTypeTextView);
    fromtxt=(TextView) findViewById(R.id.fromTextView);
    totxt=(TextView) findViewById(R.id.toTextView);

    editBtn =(Button) findViewById(R.id.editBtn);
    deleteBtn = (Button) findViewById(R.id.deleteBtn);
    cancelBtn = (Button) findViewById(R.id.cancelBtn);

    drivernametxt.setText(getIntent().getExtras().getString("DriverName"));
    agetxt.setText(getIntent().getExtras().getString("Age"));
    addresstxt.setText(getIntent().getExtras().getString("Address"));
    liecencetxt.setText(getIntent().getExtras().getString("LiecenseNo"));

    contactNotxt.setText(getIntent().getExtras().getString("ContactNo"));
    driverTypetxt.setText(getIntent().getExtras().getString("DriverType"));
    truckNotxt.setText(getIntent().getExtras().getString("TruckNo"));
    truckPermittxt.setText(getIntent().getExtras().getString("TruckPermit"));
    truckTypetxt.setText(getIntent().getExtras().getString("TruckType"));
    fromtxt.setText(getIntent().getExtras().getString("FromLocation"));
    totxt.setText(getIntent().getExtras().getString("ToLocation"));


    editBtn.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            Intent intent = new Intent(User_Detail_ListActivity.this, EditUserDetailActivity.class);
            intent.putExtra("TruckNo", getIntent().getExtras().getString("TruckNo"));
            startActivity(intent);
        }

    });

    deleteBtn.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            try {
                tdata.deleteUser(getIntent().getExtras().getString("TruckNo"));
                finish();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "database Error", Toast.LENGTH_LONG).show();
            }

        }

    });

    cancelBtn.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {
            finish();
        }

    });
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mukesh Ninama
  • 59
  • 2
  • 6

4 Answers4

1

Make sure that you add the activity to you Application Manifest

<activity android:name=".EditUserDetailActivity"></activity>
Corey Sunwold
  • 10,194
  • 6
  • 51
  • 55
1

Generally in Java the reasons for NullPointerException is:

  1. you accessed the uninitialized variables.

But in this case you may not set the Extras in the previous Activity or your getString() method argument value maybe wrong

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Balaji.K
  • 8,745
  • 5
  • 30
  • 39
0

You never initialized truckTypetxt... You instead initialized this one twice.

truckPermittxt=(TextView) findViewById(R.id.truckPermitTextView);
truckPermittxt=(TextView) findViewById(R.id.truckTypeTextView);

But yet you use truckTypetxt

truckTypetxt.setText(getIntent().getExtras().getString("TruckType"));
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Hi Mukesh I thing you made a mistake in initializing the TextView.

Please check that you have initialized truckPermittxt twice. Initialize truckTypetxt there & then try using truckTypetxt.setText(getIntent().getExtras().getString("TruckType"));

This main reason of NullPointerException.

Shashank_Itmaster
  • 2,465
  • 5
  • 30
  • 56