[英]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 ) 在第二个活动中,我正在调用获取客户ID的函数(在第三个活动中,我正在调用相同的函数,但没有获取所有详细信息,我只获取了它的ID,因为在其他活动中我需要它)
So result i am getting second time is always null , which is causing this error 因此,我第二次得到的结果始终为null,这导致此错误
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 但是(1st 2nd 3rd)然后该函数将被调用两次(即使我将数据存储在另一个对象中)并且仅在第一次调用时起作用
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) 我从函数中获取的数据存储在类型的对象(userdetails表)中
the code for the Userdetailstable is (might be needed) Userdetailstable的代码是(可能需要)
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: 在Web服务中,这是GetUserDetails函数:
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 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.artyazilim.art.UserDetailsTable.getAdres()'
10-30 05:33:13.410 24881-24881/com.artyazilim.art E/AndroidRuntime: 10-30 05:33:13.410 24881-24881 / com.artyazilim.art E / Android运行时:
at com.artyazilim.art.UserDetailsActivity$AsyncUserDetails.onPostExecute(UserDetailsActivity.java:74) 在com.artyazilim.art.UserDetailsActivity $ AsyncUserDetails.onPostExecute(UserDetailsActivity.java:74)10-30 05:33:13.410 24881-24881/com.artyazilim.art E/AndroidRuntime: 10-30 05:33:13.410 24881-24881 / com.artyazilim.art E / Android运行时:
at com.artyazilim.art.UserDetailsActivity$AsyncUserDetails.onPostExecute(UserDetailsActivity.java:47) 10 在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();
在您的第二个活动中,检查result2.getAboneKod();
is not returning a null
object. 没有返回null
对象。
I think this is why when you open the 3rd activity from the 2nd, you have the NullPointerException
. 我认为这就是为什么当您从第二个打开第三个活动时,会出现NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.