0

I want to access the Specific data of the certain function of one class to another.I can access the function of one class to another,can i access its public data?

  public class SessionManagement {

          public void createLoginSession(String accesstoken, String tokentype, String expiresin, String username, String masterId, String name, String access, String issued, String expires) {

        editor.putBoolean(IS_LOGIN, true);
        editor.putString(KEY_access_token, accesstoken);
        editor.putString(KEY_token_type, tokentype);
        editor.putString(Key_EXPIRES_IN, expiresin);
        editor.putString(KEY_USERNAME, username);
        editor.putString(KEY_MASTER_ID, masterId);
        editor.putString(KEY_Name, name);
        editor.putString(KEY_Access, access);
        editor.putString(KEY_Issued, issued);
        editor.putString(KEY_expires, expires);
        editor.apply();

        String user_new_access_token; = pref.getString(KEY_access_token, null);
        String user_new_access_tokentype = pref.getString(KEY_token_type, null);
        String user_name_expiresin = pref.getString(Key_EXPIRES_IN, null);
        String user_name_Username = pref.getString(KEY_USERNAME, null);
        String user_name_masterID = pref.getString(KEY_MASTER_ID, null);
        String user_name_name = pref.getString(KEY_Name, null);
        String user_name_access = pref.getString(KEY_Access, null);
        String user_name_issued = pref.getString(KEY_Issued, null);
        String user_name_expires = pref.getString(KEY_expires, null);


        Log.d("TAG", "Access Token :" + accesstoken + user_new_access_token);
        Log.d("TAG", "TokenType:" + user_new_access_tokentype);
        Log.d("TAG", "Expires in:" + user_name_expiresin);
        Log.d("TAG", "UserName:" + user_name_Username);
        Log.d("TAG", "MasterID:" + user_name_masterID);

        Log.d("TAG", "Name:" + user_name_name);
        Log.d("TAG", "Access:" + user_name_access);
        Log.d("TAG", "Issued:" + user_name_issued);
        Log.d("TAG", "Expires:" + user_name_expires);


        //  String user_name_new = pref.getString(KEY_access_token, null);

        //  Log.d("TAG", " :" + accesstoken + " user_name_new:" + user_name_new);


        //  Log.d(tokentype, "admin");
        //ad Log.d(expiresin, "expiresin");

        editor.commit();


    }
  public String getAccesstToken()
    {
        String user_new_access_token = pref.getString(KEY_access_token, null);
        return user_new_access_token;
    }
        }

Home class

public class Home extends AppCompatActivity{
     SessionManagement sessionManagement;
     protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        sessionManagement.createLoginSession();//i can access the function
    String user_new_access_token= sessionManagement.getAccesstToken();
     Log.d("",user_new_access_token);

    }
    }

Here i want to access the the data of function String user_new_access_token .I can access the function by .(dot) operator ,but not data of the function? why this can't be done?It will be helpful with reasons.

seon
  • 1,050
  • 2
  • 13
  • 27
  • Basically what you're asking is why not write you entire program in one function... I bet you can think of a few reasons! – Nir Alfasi Mar 10 '17 at 05:30
  • data of function is local to that function so you can not access it from out side.instead of this declare global variable and assign value to that variable and you can access it from out side of function. – santosh gore Mar 10 '17 at 05:31
  • The variable to scoped to that method. If you want it accessible elsewhere then move the scope to that of a field – Scary Wombat Mar 10 '17 at 05:32
  • @santosh if you have to access?how you do it? – seon Mar 10 '17 at 05:32
  • if you declare that variable as a global variable . so you can access it from anywhere. – santosh gore Mar 10 '17 at 05:33
  • @santoshgore does Java have global variables? http://stackoverflow.com/a/4646666/2310289 – Scary Wombat Mar 10 '17 at 05:36
  • the value is setted to function.So i want the setted value to be accessed.How can this be possible?@santoshgore – seon Mar 10 '17 at 05:36
  • @ScaryWombat java doesn't have global variable . – santosh gore Mar 10 '17 at 05:39
  • @seon make `user_new_access_token` a String variable outside of your methods but within the class like in my answer. And wherever `String user_new_access_token` appeared before, just make it `user_new_access_token` while keeping all of your original code. – Chris Gong Mar 10 '17 at 05:54

2 Answers2

1

Add this function in SessionManagement class:

public String getAccesstToken()
{
     String user_new_access_token = pref.getString(KEY_access_token, null);
     return user_new_access_token;
}

Fetch using :

    SessionManagement sessionManagement=new SessionManagement();
    String user_new_access_token= sessionManagement.getAccesstToken();
Komal12
  • 3,340
  • 4
  • 16
  • 25
  • Process: com.example.user.mis, PID: 6269 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.mis/com.example.user.mis.activity.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.user.mis.activity.SessionManagement.getAccesstToken()' on a null object reference – seon Mar 10 '17 at 05:46
  • You are not set setContentView(R.layout.activity_home) in oncreate.Set your home layout 1st and then call sessionManagement method – Komal12 Mar 10 '17 at 05:54
  • @seon how you can access method sessionManagement.createLoginSession();//i can access the function this without any parameter?..In login webservice you already set all value to createLoginSession fucntion.Now in home home only call getAccesstToken function – Komal12 Mar 10 '17 at 05:56
  • i had to send token on the header .for that i had to acess token only.thats what i cant able to do – seon Mar 10 '17 at 06:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/137727/discussion-between-seon-and-user2025187). – seon Mar 10 '17 at 06:01
  • can you please help in solving this problem. https://stackoverflow.com/questions/43931126/add-linear-layout-dynamically-inside-the-list-item/43931881#43931881 – seon Jun 07 '17 at 05:04
  • @seon please check given answer :https://stackoverflow.com/questions/43931126/add-linear-layout-dynamically-inside-the-list-item/43931881#43931881 – Komal12 Jun 09 '17 at 03:19
0

The method createLoginSession may initialize some variables that you wish to access, but these variables only exist within the scope of the method. Once the method is finished, these variables are garbage collected and thereby unaccessable. Therefore, you have to put the variables you want to access outside of the local scope of the method. What I'd recommend is making the variables you want to access instance variables within your SessionManagement class. And then retrieve like you would any instance variable (through the dot operator or a getter method).

public class SessionManagement {
    String user_new_access_token = "";
    //Other instace variables
    public void createLoginSession(String accesstoken, String tokentype, String expiresin, String username, String masterId, String name, String access, String issued, String expires) {
        //code before assignment of user_new_access_token
        user_new_access_token = pref.getString(KEY_access_token, null);
        //rest of the method body
    }
}

Then you can access a variable such as user_new_access_token in class Home like so,

public class Home extends AppCompatActivity{
    SessionManagement sessionManagement;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sessionManagement.createLoginSession();
        String temp = sessionManagement.user_new_access_token;
    }
}

As for why you can't write sessionManagement.createLoginSession().data(user_new_access_token), the reason is that createLoginSession() is a void method so it doesn't return anything. As a result there is no resulting object to perform the dot operator on. Also, there is no data method that would be appropriate to use here.

Chris Gong
  • 8,031
  • 4
  • 30
  • 51
  • showing java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.mis/com.example.user.mis.activity.Home}: java.lang.NullPointerException: Attempt to read from field 'java.lang.String com.example.user.mis.activity.SessionManagement.user_new_access_token' on a null object reference – seon Mar 10 '17 at 06:05
  • @seon check my edit, it's because you have to pre-initialize instance variables to guarantee they're not null. The difference is this line, `String user_new_access_token = "";` – Chris Gong Mar 10 '17 at 15:26