简体   繁体   中英

Getting null object reference If i start 2nd activity before 3rd activity (No error if I move from 1st to 3rd directly)

I know its a common error , and i know lots of topics here were asking about the same error, but i tried alot of solutions and non works. My application is like this:

1st activity is a sign in activity, 2nd is a menu to navigate where to go, 3rd is the customer's details.

I think i know where the problem is but i don't whats causing it

In the 2nd activity i am calling a function to get the customer id (the same function i am calling in the 3rd activity but without taking all the details i am only taking it's ID because i need it in other activities )

So result i am getting second time is always null , which is causing this error

so if i jump directly from 1st to 3rd app doesn't crash.

but (1st 2nd 3rd ) then the function will be called twice (even though i am storing data in a different object) and works only at the first time it's called

Hope i explained it well

now my code for 2nd activity:

public class AfterLogin extends AppCompatActivity {
    @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new AsyncpkAbone().execute(SharedValues.AboneKod);
        setContentView(R.layout.activity_after_login);


    }
    public void AboneBilgiPressed(View v){
        Intent i = new Intent(AfterLogin.this, UserDetailsActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(i);


    }


    protected class AsyncpkAbone extends AsyncTask<String,Void,UserDetailsTable>
    {

        @Override
        protected UserDetailsTable doInBackground(String... params) {
            // TODO Auto-generated method stub
            UserDetailsTable userDetail2=null;
            RestAPI api = new RestAPI();
            try {

                JSONObject jsonObj = api.GetUserDetails(params[0]);
                JSONParser parser = new JSONParser();
                userDetail2 = parser.parseUserDetails(jsonObj);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncUserDetails", e.getMessage());

            }

            return userDetail2;
        }

        @Override
        protected void onPostExecute(UserDetailsTable result2) {
            // TODO Auto-generated method stub

            SharedValues.AboneKod =result2.getAboneKod();
            SharedValues.pkAbone = result2.getPkAbone();

        }

    }

the Code for the 3rd activity (user details)

public class UserDetailsActivity extends AppCompatActivity {

    TextView tvAdres, tvTelefon,tvpkAbone;
    String Adres;
    String WEBParola;
    String Tel1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_details);
        new AsyncUserDetails().execute(SharedValues.AboneKod);


        tvAdres = (TextView) findViewById(R.id.tv_firstname);
        tvAdres.setTextIsSelectable(true);

        tvTelefon = (TextView) findViewById(R.id.tv_lastname);
        tvTelefon.setTextIsSelectable(true);

        tvpkAbone = (TextView) findViewById(R.id.tv_pkAbone);
        tvpkAbone.setTextIsSelectable(true);

        tvAdres.setText(Adres);
        tvTelefon.setText(Tel1);
        tvpkAbone.setText(String.valueOf( SharedValues.pkAbone));

    }

    protected class AsyncUserDetails extends AsyncTask<String,Void,UserDetailsTable>
    {

        @Override
        protected UserDetailsTable doInBackground(String... params) {
            // TODO Auto-generated method stub
            UserDetailsTable userDetail=null;
            RestAPI api = new RestAPI();
            try {

                JSONObject jsonObj = api.GetUserDetails(params[0]);
                JSONParser parser = new JSONParser();
                userDetail = parser.parseUserDetails(jsonObj);

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncUserDetails", e.getMessage());

            }

            return userDetail;
        }

        @Override
        protected void onPostExecute(UserDetailsTable result) {
            // TODO Auto-generated method stub

            tvAdres.setText(result.getAdres());
            tvTelefon.setText(result.getTelefon());

        }

    }

the data i get from the function is stored in a object of type (userdetails tables)

the code for the Userdetailstable is (might be needed)

package com.artyazilim.art;

public class UserDetailsTable {

    String Adres,Tel1,AboneKod,WEBParola;
    int pkAbone;
    public UserDetailsTable(String Adres, String Tel1, String AboneKod,
                            String WEBParola,int pkAbone) {
        super();
        this.Adres = Adres;
        this.Tel1 = Tel1;
        this.AboneKod = AboneKod;
        this.WEBParola = WEBParola;
        this.pkAbone = pkAbone;
    }

    public UserDetailsTable() {
        super();
        this.Adres = null;
        this.Tel1 = null;
        this.AboneKod = null;
        this.WEBParola = null;
        this.pkAbone = 0;
    }

    public String getAdres() {
        return Adres;
    }

    public void setAdres(String adres) {
        Adres = adres;
    }

    public String getTelefon() {
        return Tel1;
    }

    public void setTelefon(String telefon) {
        Tel1 = telefon;
    }

    public String getAboneKod() {
        return AboneKod;
    }

    public void setAboneKod(String aboneKod) {
        AboneKod = aboneKod;
    }

    public String getWEBParola() {
        return WEBParola;
    }

    public void setWEBParola(String WEBParola) {
        this.WEBParola = WEBParola;
    }

    public int getPkAbone() {
        return pkAbone;
    }

    public void setPkAbone(int pkAbone) {
        this.pkAbone = pkAbone;
    }
}

the function which i am calling in the both Async is this:

 public JSONObject GetUserDetails(String AboneKod) throws Exception {
        JSONObject result = null;
        JSONObject o = new JSONObject();
        JSONObject p = new JSONObject();
        o.put("interface","Service1");
        o.put("method", "GetUserDetails");
        p.put("AboneKod",mapObject(AboneKod));
        o.put("parameters", p);
        String s = o.toString();
        String r = load(s);
        result = new JSONObject(r);
        return result;
    }

and in the web service this is the GetUserDetails function:

public DataTable GetUserDetails(string AboneKod)
{       
    DataTable userDetailsTable = new DataTable();
    userDetailsTable.Columns.Add(new DataColumn("Adres", typeof(String)));
    userDetailsTable.Columns.Add(new DataColumn("Tel1", typeof(String)));
    userDetailsTable.Columns.Add(new DataColumn("pkAbone", typeof(String)));

    if (dbConnection.State.ToString() == "Closed")
    {
        dbConnection.Open();
    }

    string query = "SELECT Adres,Tel1,pkAbone FROM r_Abone WHERE AboneKod='" + AboneKod + "';";

    SqlCommand command = new SqlCommand(query, dbConnection);
    SqlDataReader reader = command.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            userDetailsTable.Rows.Add(reader["Adres"], reader["Tel1"], reader["pkAbone"]);
        }
    }

    reader.Close();
    dbConnection.Close();
    return userDetailsTable;

}

the error i am getting when going from 2nd to 3rd is

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.artyazilim.art.UserDetailsTable.getAdres()' on a null object reference

10-30 05:33:13.410 24881-24881/com.artyazilim.art E/AndroidRuntime:
at com.artyazilim.art.UserDetailsActivity$AsyncUserDetails.onPostExecute(UserDetailsActivity.java:74)

10-30 05:33:13.410 24881-24881/com.artyazilim.art E/AndroidRuntime:
at com.artyazilim.art.UserDetailsActivity$AsyncUserDetails.onPostExecute(UserDetailsActivity.java:47) 10

i know it seems like a duplicate and I know the rules search before ask,I have spent lots of time trying other's solutions but the reason i might didn't find the answer else where is because i don't know whats is actually causing this error so not knowing what to search for.

thanks in advance :)

In you second activity check if result2.getAboneKod(); is not returning a null object.

I think this is why when you open the 3rd activity from the 2nd, you have the NullPointerException .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM