0

I want to create a class that has a variable, that this variable can be modified and obtained from other activities and this varable always be saved.

I've tried to do this class with the variable "TipoAnuncio":

public class Filter_Object  {
    private String TipoAnuncio ="Perdidos";

    public String getTipoAnuncio(){
        return TipoAnuncio;
    }
    public void setTipoAnuncio(String TipoAnuncio){
        this.TipoAnuncio = TipoAnuncio;
    }
}

And i've tried to get and to set this variable from other Activities, for example:

public class FilterActivity extends AppCompatActivity {

    private Filter_Object filter;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);


        Log.i("anuncioahora", filter.getTipoAnuncio());
}
}

An error appears which says :java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.julianrc1.petracecitm/com.example.julianrc1.petracecitm.fragments.Intents.FilterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.julianrc1.petracecitm.Objects.Filter_Object.getTipoAnuncio()' on a null object reference

Thanks for your help :)

All Might
  • 1
  • 4

1 Answers1

0

Use a static variable. Example: public static String TipoAnuncio = "Perdidos";

Access/modify it as: Filter_Object.TipoAnuncio

Lucas Cabrales
  • 2,073
  • 1
  • 12
  • 21