Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Obtener "Referencia de objeto nulo" al intentar la encapsulación

Probablemente sea estúpidamente simple, pero parece que no puedo entender qué es lo que está mal. Estoy tratando de crear una demostración para mostrar Encapsulation.

Encapsulación.clase:

 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.clase:

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

actividad_principal.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>

Y recibo un error de "referencia de objeto nulo" cuando inicio la aplicación y configuro el texto a través defaultNumberOfEggs.setText(encapsulation.getNumberOfEggs()); , que debe mostrar "10" (sin las comillas).

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Olvidó inicializar su objeto de encapsulation . Por defecto, apunta a nulo.

Así que deberías definirlo a nivel de clase:

Encapsulation encapsulation = new Encapsulation();

O en constructor:

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

O dentro del método onCreate después de llamar a super:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); encapsulation = new Encapsulation(); setContentView(R.layout.activity_main); .... }
over 4 years ago · Santiago Trujillo Report

0

En el método onCreate , agregue una primera línea como

 encapsulation = new Encapsulation();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!