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