0

First of all I'm very new with coding just trying and also new with the features of Firebase. The problem is that, I'm trying to get specific value from firebase by putting number associated with the value. As I have designed it as my first activity (Buffer2) to put the serial number with EditText and thus pass the value from this activity to next activity (Buffer3). and assigned this intent.getExtra string and then making this as int to put the serial number. But the process getting crash (or stopped as to saying it null). Is there, another way to get this done? any help will be appreciated.

Here is my code (xml and java) for buffer2 and buffer3

buffer2 xml----------------------------

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Buffer2">

    <LinearLayout
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/etGo"
            android:hint="Go To Page No"
            android:inputType="number"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/btnGo"
            android:text="Go"
            android:textStyle="bold"
            android:textSize="18sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>
</LinearLayout>

Buffer2 java---------------------------------------

public class Buffer2 extends AppCompatActivity {

    Button BtnGo;
    EditText EtGo;

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

        BtnGo=findViewById(R.id.btnGo);
        EtGo=findViewById(R.id.etGo);
        String number = Buffer2.this.EtGo.getText().toString();

        BtnGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Buffer2.this, Buffer3.class);
                intent.putExtra("numberto", number);
                startActivity(intent);
            }
        });


    }
}

buffer3 xml-------------------------------

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Buffer3">

    <TextView
        android:id="@+id/nameTv"
        android:textSize="28sp"
        android:gravity="center"
        android:text="Loading..."
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

    <TextView
        android:id="@+id/professionTv"
        android:textSize="28sp"
        android:gravity="center"
        android:text="Loading..."
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

</LinearLayout>

Buffer3 java------------------------------

public class Buffer3 extends AppCompatActivity {

    TextView Name, Profession;
    DatabaseReference reference;
    String Number = getIntent().getStringExtra("numberto");

    int count = Integer.parseInt(String.valueOf(Number));

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

        Name = findViewById(R.id.nameTv);
        Profession = findViewById(R.id.professionTv);

        reference=FirebaseDatabase.getInstance().getReference().child("What")
                .child(String.valueOf(count));
    }

    protected void onStart() {
        super.onStart();

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String name = dataSnapshot.child("name").getValue().toString();
                String type = dataSnapshot.child("type").getValue().toString();
                Name.setText(name);
                Profession.setText(type);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

Logcat------------------------

2020-04-22 18:39:48.939 11231-11231/com.potato.manpower I/Timeline: Timeline: Activity_launch_request time:419024820 intent:Intent { cmp=com.potato.manpower/.Buffer3 (has extras) }
2020-04-22 18:39:48.969 11231-11231/com.potato.manpower D/AndroidRuntime: Shutting down VM
2020-04-22 18:39:48.970 11231-11231/com.potato.manpower E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.potato.manpower, PID: 11231
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.potato.manpower/com.potato.manpower.Buffer3}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2649)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:165)
        at android.app.ActivityThread.main(ActivityThread.java:6375)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
        at com.potato.manpower.Buffer3.<init>(Buffer3.java:17)
        at java.lang.Class.newInstance(Native Method)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2639)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:165) 
        at android.app.ActivityThread.main(ActivityThread.java:6375) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802) 
2020-04-22 18:39:48.999 11231-11231/com.potato.manpower I/Process: Sending signal. PID: 11231 SIG: 9

Firebase Structure enter image description here

Ryan M
  • 18,333
  • 31
  • 67
  • 74

1 Answers1

1

It looks like getIntent() returns null in this line:

String Number = getIntent().getStringExtra("numberto");

I expect that problem will disappear if you move the initialization of Number into the onCreate method:


public class Buffer3 extends AppCompatActivity {
    TextView Name, Profession;
    DatabaseReference reference;
    String Number;

    int count = Integer.parseInt(String.valueOf(Number));

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

        Number = getIntent().getStringExtra("numberto");

        Name = findViewById(R.id.nameTv);
        Profession = findViewById(R.id.professionTv);

        reference=FirebaseDatabase.getInstance().getReference().child("What")
                .child(String.valueOf(count));
    }
    ...

A few other notes:

  • It is idiomatic to have the names of member fields start with lowercase letters, so number instead of Number.
  • When you get a NullPointerException, follow the advice from this post to learn how to troubleshoot it yourself: What is a NullPointerException, and how do I fix it?
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for your suggestion. I have followed the procedures (placing in onCreate and also with lowercase letters) you have said but the situation is as before. nothing have changed. – Ki Bujhla hmm Apr 22 '20 at 15:37
  • Searching for "when can getIntent() return null" leads to https://stackoverflow.com/questions/37856407/can-activity-getintent-ever-return-null – Frank van Puffelen Apr 22 '20 at 16:18
  • Hay, Thanks again. I dont know what is the problem but I think... It happens. The data through the intet.getExtra method I obtained it can be shown by the textview (as string or int) but it can't passes into code to incorporate in this case... as the value of count. reference=FirebaseDatabase.getInstance().getReference().child("What") .child(String.valueOf(count)); Just not in count but also if I want to change get the 'What' value from string which is available cant incorporate the logic. why? Is there anything need to be taken care to incorporate such logic? – Ki Bujhla hmm Apr 23 '20 at 04:56