Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying to implement a login screen where username and password are validated with the help of a rest service.

i have created a global class like this

Java
public class GlobalClass extends Application{
    String url="myRestService";
    String newURL;

    public String getName() {
        return url;
    }

    public void setName(String aName) {
        url = aName;
    }

    public String getNewURL() {
        return newURL;
    }

    public void setNewURL(String bName) {
        newURL = bName;
    }
}


I have created a screen where the superadmin can change the rest service like
this class is intented to update the rest service
Java
final GlobalClass globalVariable = new GlobalClass();
        String restMethod=globalVariable.getName();
        et.setText(restMethod);
        b= (Button) findViewById(R.id.button);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userEnteredMethod=et.getText().toString();
                globalVariable.setNewURL(userEnteredMethod);
                Toast.makeText(getApplicationContext(),userEnteredMethod,Toast.LENGTH_SHORT).show();
                Intent login=new Intent(getApplicationContext(),Login.class);
                startActivity(login);
            }
        });


Now in my login
i called the global class like this and get the url
Java
final GlobalClass gb = new GlobalClass();
       userEnteredMethod = gb.getNewURL();

Java
String newURL = userEnteredMethod.concat("/MethodResult");
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(newURL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                try {
                   // prgDialog.hide();
                    JSONObject obj = new JSONObject(response);
                    if (obj != null) {
                        JSONArray data = obj.getJSONArray("ValidateSubscriberLoginResult");
                        for (int i = 0; i < data.length(); i++) {
                            JSONObject user = data.getJSONObject(i);
                            //
                            if (!user.getString("ID").equals("0")) {
                               Toast.makeText(getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
                            } else
                            {
                                Toast.makeText(getApplicationContext(),"Check again",Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                } catch (JSONException e) {
                    Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();

                    e.printStackTrace();
                }
                super.onSuccess(response);
            }


on executing the code i'm getting a null pointer exception in String newURL = userEnteredMethod.concat("/MethodResult");
while debuging i found out that i am getting a null value at userEnteredMethod = gb.getNewURL();

please help me to rectify the problem
Posted
Comments
Krunal Rohit 8-Oct-15 2:41am    
Share the getNewURL() method code.

-KR
Rahul Ramakrishnan 8-Oct-15 3:00am    
to get the new url i just passed it like this
final GlobalClass gb = new GlobalClass();
userEnteredMethod = gb.getNewURL();
Krunal Rohit 8-Oct-15 3:18am    
Sorry, didnt see that.

-KR

1 solution

Of course it won't work.
final GlobalClass gb = new GlobalClass();<br />
String userEnteredMethod = gb.getNewURL();


You have instantiated a new object. Hence, it'll give you null. :)
Java
// obn button click, before the startACtivity()
login.putExtra("URL", globalVariable.getNewURL);


// in login
Bundle extras = getIntent().getExtras();
String url = extras.getString("URL");
String newURL = url.concat("/MethodResult");
AsyncHttpClient client = new AsyncHttpClient();
// your rest of the code

-KR
 
Share this answer
 
v2
Comments
Rahul Ramakrishnan 8-Oct-15 3:32am    
So how do i make it work? i am totally new to this
Krunal Rohit 8-Oct-15 5:18am    
Check the updated answer.

-KR
Rahul Ramakrishnan 8-Oct-15 5:28am    
thanks you very much.. it works fine but what if i have lots of java class that has to be implemented in the same way. will the result be consistent across all the class?
Krunal Rohit 8-Oct-15 5:57am    
If you want to psersist the data between activity, you can surely go like this.
And one advice, for details like: username, password, url needs to be stored using SharedPrefrences.

-KR

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900