I have a problem in my e-commerce apps

Chaymae Benayyad 21 Reputation points
2021-12-07T14:52:37.78+00:00

public class Prevalent {
public static Users currentOnlineUser;
//unique key for all users

public static final String UserPhoneKey = "UserPhone";
public static final String UserPasswordKey = "UserPassword";

}

public class Users {
//get the name passeword, all the user information
private String name, phone, password;

public Users(){

};
public Users(String name, String phone, String password) {
    this.name = name;
    this.phone= phone;
    this.password = password;
}



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;

} public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

}

public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private AppBarConfiguration mAppBarConfiguration;


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

    Paper.init(this);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("Home");
    setSupportActionBar(toolbar);

    FloatingActionButton fab = 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();
        }
    });

    DrawerLayout drawer = findViewById(R.id.drawer_layout);


    NavigationView navigationView = findViewById(R.id.nav_view);

    View headerView = navigationView.getHeaderView(0);
    TextView userNameTextView = headerView.findViewById(R.id.user_profile_name);
    CircleImageView profileImageView = headerView.findViewById(R.id.user_profile_image);

    userNameTextView.setText(Prevalent.currentOnlineUser.getName());



    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
            .setOpenableLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_home);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_home);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();




    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.nav_cart) {

    } else if (id == R.id.nav_orders) {

    } else if (id == R.id.nav_categories) {
    } else if (id == R.id.nav_settings) {
    } else if (id == R.id.nav_logout) {
        Paper.book().destroy();

        Intent intent = new Intent(HomeActivity.this,MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

I would like to get the name object but the exception told me than i can't call it because it's null can any one help me to find a solution thank you

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,751 Reputation points Microsoft Vendor
    2021-12-08T06:53:59.917+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!
    From the code you provided, I did not find this Users instance, the Users has to be instantiated.

     userNameTextView.setText(Prevalent.currentOnlineUser.getName());  
    

    You could try to new the user and set a name to this user, then you could get its name, refer to the following code:

    Users user= new Users();  
    user.setName("name");  
    Prevalent.currentOnlineUser = user;  
    //get  
    String _value = prevalent.currentOnlineUser.getName();  
    

    In addition, the code looks in java language, not c#. May I know if you are using Native Android or Xamarin.Android ?
    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Chaymae Benayyad 21 Reputation points
    2021-12-08T12:04:55.533+00:00

    actually the Users is a class in the model package i import it, it worked know i just uninstall and install the aplication again thenks for your reply :)

    0 comments No comments