Good day, At this moment i am stuck with my program. I know what the problem is but I don't know how to resolve it.
I have a Json request in my program and I know using Volley this is a Async task. So the things I want to do with my code should be in the onResponse part. I want to update some textView items with the response data I got. The error i am getting is this: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference.
Here is my MainActivity file:
package lichtverzorging.myapplication;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements RugzakAdapter.customButtonListener {
//Informatie over IMEI project naam en gebruiker
String IMEI;
public TextView project;
public TextView gebruiker;
Registration Check_registration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
project = (TextView) findViewById(R.id.textView_project);
gebruiker = (TextView) findViewById(R.id.textView_gebruiker);
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
this.IMEI = telephonyManager.getDeviceId();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public void onResume(){
super.onResume();
System.out.println("onResume....");
}
@Override
public void onStart() {
super.onStart();
System.out.println("onStart....");
Check_registration = new Registration(IMEI, this);
}
}
public void test(String username, String projectname){
project.setText(projectname);
gebruiker.setText(username);
}
}
Registration file
package lichtverzorging.myapplication;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class Registration extends MainActivity{
String IMEI;
String User_Name;
String Project_Name;
//boolean Is_Registered;
Context context;
RequestQueue requestQueue;
private final String BaseUrl = "url/;
private final String CheckRegistration_Url = BaseUrl + "Check_registration.php";
private final String Register_Url = BaseUrl + "Register.php";
public String getIMEI() {
return this.IMEI;
}
public String getUser_Name() {
return this.User_Name;
}
public String getProject_Name() {
return this.Project_Name;
}
public void setProject_Name(String project_Name) {
this.Project_Name = project_Name;
}
public void setUser_Name(String user_Name) {
this.User_Name = user_Name;
}
public void Is_Registered(){
//Controleer of de persoon voor dit project geregistreerd staat.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
CheckRegistration_Url + "?IMEI=" + this.IMEI, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
try {
System.out.println("Is_Registration: " + response.getBoolean("registration"));
if(response.getBoolean("registration")){
System.out.println("Project: " + response.getString("Project_Name"));
System.out.println("User_Name: " + response.getString("User_Name"));
Project_Name = response.getString("Project_Name");
User_Name = response.getString("User_Name");
test(Project_Name, User_Name);
}
else
{
Register();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
requestQueue.add(jsonObjectRequest);
}
public Registration(String IMEI, Context context) {
this.context = context;
this.IMEI = IMEI;
requestQueue = Volley.newRequestQueue(context);
}
}
It's about this line: test(Project_Name, User_Name); I want to set the project and user textView.
The logcat error is this:
11-23 13:56:26.092 4886-4886/lichtverzorging.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: lichtverzorging.myapplication, PID: 4886
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at lichtverzorging.myapplication.MainActivity.onButtonClickListner2(MainActivity.java:158)
at lichtverzorging.myapplication.Registration$3.onResponse(Registration.java:175)
at lichtverzorging.myapplication.Registration$3.onResponse(Registration.java:160)
at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
I hope you can help me with this problem.