0

I am working in a Page2 activity in Android Studio and I am using php file pause_in, below is java code using Volley class.It gives me the error: java.lang.NullPointerException: Attempt to invoke virtual method 'com.android.volley.Request com.android.volley.RequestQueue.add(com.android.volley.Request)' on a null object reference. But after I have added the line for initializing the request,it gives me that:(I have to mention that I used getExtra method to get the variables of email and password from the first screen).

12-08 12:49:03.446 1346-1370/? W/audio_hw_generic: Not supplying enough 
data to HAL, expected position 455229 , only wrote 455040
12-08 12:49:03.446 2245-2245/? D/key: valuee
12-08 12:49:03.514 2245-2245/? W/System.err: org.json.JSONException: 
End of input at character 1 of 
12-08 12:49:03.514 2245-2245/? W/System.err:     at 
org.json.JSONTokener.syntaxError(JSONTokener.java:449)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 
org.json.JSONTokener.nextValue(JSONTokener.java:97)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 
org.json.JSONObject.<init>(JSONObject.java:156)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 
org.json.JSONObject.<init>(JSONObject.java:173)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 
com.example.a.mysql_php_register_login.Page2$1$1.
onResponse(Page2.java:96)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 
com.example.a.mysql_php_register_login.Page2$1$1.
onResponse(Page2.java:89)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 
com.android.volley.toolbox.StringRequest.
deliverResponse(StringRequest.java:67)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 

com.android.volley.toolbox.StringRequest.
deliverResponse(StringRequest.java:30)
12-08 12:49:03.514 2245-2245/? W/System.err:     at 

   com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable
  .run(ExecutorDelivery.java:99)
  12-08 12:49:03.514 2245-2245/? W/System.err:     at 
  android.os.Handler.handleCallback(Handler.java:751)
  12-08 12:49:03.514 2245-2245/? W/System.err:     at 
  android.os.Handler.dispatchMessage(Handler.java:95)
  12-08 12:49:03.514 2245-2245/? W/System.err:     at 
  android.os.Looper.loop(Looper.java:154)
  12-08 12:49:03.514 2245-2245/? W/System.err:     at 
 android.app.ActivityThread.main(ActivityThread.java:6077)
 12-08 12:49:03.514 2245-2245/? W/System.err:     at 
 java.lang.reflect.Method.invoke(Native Method)
  12-08 12:49:03.514 2245-2245/? W/System.err:     at 
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.
  run(ZygoteInit.java:866)
  12-08 12:49:03.514 2245-2245/? W/System.err:     at 
  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 12-08 12:49:06.079 1586-1596/? I/ActivityManager: Killing 
 3211:com.android.gallery3d/u0a49 (adj 906): empty #17

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;



public class Page2 extends AppCompatActivity {

private EditText emailI,passwordI;

DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBarDrawerToggle actionBarDrawerToggle;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;

Button pauseIn;
EditText pause;


private RequestQueue requestQueue;
private static final String URL = "http://10.31.16.22/pause_in.php";
private StringRequest request;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page2_activity);



    emailI=(EditText)findViewById(R.id.emailiImprovizum);
    passwordI=(EditText)findViewById(R.id.passImprovizum);





    pauseIn = (Button) findViewById(R.id.btnPauseIn);
    pauseIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            request = new StringRequest(Request.Method.POST, URL, new 
    Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                         JSONObject jsonObject = new 
     JSONObject(response);
                        if 
     (jsonObject.names().get(0).equals("success1")) {
                          //  Log.d("dfr","a po bon");
                            Toast.makeText(getApplicationContext(), 
      "SUCCESS " + jsonObject.getString("success1"), 
      Toast.LENGTH_SHORT).show();




                            Intent intent = getIntent();
                            String e = intent.getStringExtra("Email");
                            String p = 
       intent.getStringExtra("Password");

                            emailI.setText(e);
                            passwordI.setText(p);

                            startActivity(new 
        Intent(getApplicationContext(), about.class));


                        } else if 
       (jsonObject.names().get(0).equals("fail1")) {
                            Toast.makeText(getApplicationContext(), 
     "Error" + jsonObject.getString("fail1"), 
     Toast.LENGTH_SHORT).show();
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {


                }
            }){
                @Override
                protected Map<String, String> getParams() throws 
         AuthFailureError {
                    HashMap<String,String> hashMap = new 
          HashMap<String, String>();
                    hashMap.put("pause_in","pauseIn");

                    hashMap.put("emailI",emailI.getText().ToString());

               hashMap.put("passwordI",password.getText().ToString());



                    return hashMap;
                }
            };

            requestQueue.add(request);
            Log.d("key","valuee");
        }
    });

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawableT);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, 
    drawerLayout, toolbar,
            R.string.drawer_open, R.string.drawer_close);








actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources()
.getColor(R.color.white));

    drawerLayout.addDrawerListener(actionBarDrawerToggle);

    fragmentTransaction = 
 getSupportFragmentManager().beginTransaction();

    fragmentTransaction.commit();

    navigationView = (NavigationView) 
findViewById(R.id.navigation_view);
    navigationView.setNavigationItemSelectedListener(new 
 NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) 
  {

            switch (item.getItemId()) {

                case R.id.about_id:
                    Intent i = new Intent(Page2.this, about.class);
                    startActivity(i);

                    break;


                case R.id.id_instructions:

                    Intent i3 = new Intent(Page2.this, about.class);
                    startActivity(i3);

                    break;
            }

            return false;
        }
    });


}

@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarDrawerToggle.syncState();
    }
}


<?php
include_once 'connection.php';




class User1{

private $db;
private $connection;

function __construct() {
        $this -> db = new DB_Connection();
        $this -> connection = $this->db->getConnection();
    }

public function pause($email,$password)
    {
        $pauseIn=time();
        $query = "Select * from users where email='$email' and password 
     ='$password'";

        $result = mysqli_query($this->connection, $query);
        if(mysqli_num_rows($result)>0){
        $query2="update users set pause_in='$pauseIn' where 
      email='$email' ";
        $inserted = mysqli_query($this -> connection, $query2);
        if($inserted ==1 )
        {
            $json['success1'] = 'welcome';
            echo json_encode($json);
            mysqli_close($this -> connection);
        }
        else{

                $json['fail1'] = ': Email or password is wrong';

            echo json_encode($json);

        }

             }


        }
   }
   $user = new User1();



   if(isset($_POST['email'],$_POST['password'])) {
    $email=$_POST['email'];
    $password=$_POST['password'];

    $user-> pause($email,$password);

        }
        else{

        }
    ?>
andu
  • 413
  • 1
  • 4
  • 10

2 Answers2

1

You haven't initialised the RequestQueue object in you class.

RequestQueue queue = Volley.newRequestQueue(getContext());

Try it...

Community
  • 1
  • 1
Paras Verma
  • 129
  • 9
  • I added this line,but now it says org.json.JSONException: Value
    – andu Dec 07 '17 at 13:44
  • And it shows the lines : request = new StringRequest(Request.Method.POST, URL, new Response.Listener() and JSONObject jsonObject = new JSONObject(response); – andu Dec 07 '17 at 13:54
  • Log the response under the try catch statment, before it is converted to JSONObject. and post the response here.. public void onResponse(String response) { try { Log.i("Response",response); JSONObject jsonObject = new JSONObject(response); – Paras Verma Dec 08 '17 at 05:51
0

you are forget to initialize private StringRequest request; in onCreate(); like below code

 RequestQueue mRequestQueue;

// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024); // 1MB cap

// Set up the network to use HttpURLConnection as the HTTP client.
Network network = new BasicNetwork(new HurlStack());

// Instantiate the RequestQueue with the cache and network.
mRequestQueue = new RequestQueue(cache, network);

// Start the queue
mRequestQueue.start();

for more info check this link.

Omkar
  • 3,040
  • 1
  • 22
  • 42