Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
Getting "Null Object Reference" While Trying Encapsulation

It's probably stupidly simple, but I can't seem to figure out what's wrong. I'm trying to create a demo for showcasing Encapsulation.

Encapsulation.class:

public class Encapsulation {
// 'numberOfEggs' is an instance variable
// it was made 'private', because we do NOT want it to be directly accessed by other classes
// being 'private' also means that only code within the class can read or write its value
private int numberOfEggs = 10;

// getter
public int getNumberOfEggs() {
    return numberOfEggs;
}

// setter
public void setNumberOfEggs(int numberOfEggs) {
    if (numberOfEggs > 0) {
        // we used the 'this' keyword to differentiate between the method parameter 'currentNumberOfEggs' and
        //  the instance variable 'currentNumberOfEggs'
        this.numberOfEggs = numberOfEggs;
    }
}
}

MainActivity.class:

public class MainActivity extends AppCompatActivity {

TextView defaultNumberOfEggs;
Encapsulation encapsulation;

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

    defaultNumberOfEggs = (TextView) findViewById(R.id.default_number_of_eggs);
    defaultNumberOfEggs.setText(encapsulation.getNumberOfEggs());
}
}

activity_main.xml:

<RelativeLayout android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.vladp.studyandroid1.MainActivity">

<TextView
    android:id="@+id/default_number_of_eggs"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

And I get a "null object reference" error, when starting the app and setting the text via defaultNumberOfEggs.setText(encapsulation.getNumberOfEggs());, which should display "10" (without the quotation marks).

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You forgot to initialize your encapsulation object. By default, it points to null.

So you should either define it on class level:

Encapsulation encapsulation = new Encapsulation();

Or in constructor:

MainActivity() {
    encapsulation = new Encapsulation();
}

Or inside onCreate method after calling super:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    encapsulation = new Encapsulation();
    setContentView(R.layout.activity_main);

    ....
}
over 4 years ago · Santiago Trujillo Denunciar

0

In the onCreate method, add a first line as

encapsulation = new Encapsulation();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda