0

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.

Aditi Parikh
  • 1,522
  • 3
  • 13
  • 34
Lithium
  • 23
  • 6
  • Means `textView_project ` and `textView_gebruiker ` TextView's are not inside `R.layout.activity_main` – ρяσѕρєя K Nov 23 '15 at 13:24
  • Maybe we should make an answer for "NPE caused by findViewById on xml layouts". much more precise than "What is a Null Pointer Exception..." – Nanoc Nov 23 '15 at 13:27
  • @ρяσѕρєяK is that or maybe is not executing those lines in onCreate since is coming from an interface. – Nanoc Nov 23 '15 at 13:29
  • Thanks for replying to this topic. The textviews are in the file content_main. I see what you mean, how can this work: setContentView(R.layout.activity_main) instead? I wrote the whole layout in content_main. If i do call the test function in the oncreate from the MainActivity it works. – Lithium Nov 23 '15 at 14:56
  • This is how the activity_main file looks like: – Lithium Nov 23 '15 at 15:02

0 Answers0