0

I'm getting an error of:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable com.rengwuxian.materialedittext.MaterialEditText.getText()' on a null object reference
        at com.example.batangasbeachhousesfinalserver.HomeFragment$16$1.onSuccess(HomeFragment.java:318)
        at com.example.batangasbeachhousesfinalserver.HomeFragment$16$1.onSuccess(HomeFragment.java:312)
        at com.google.android.gms.tasks.zzn.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6816)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1567)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1455)

I have updated the code* can't add any details at it. below is the onCreateView code and upload code into my database. Please help badly needed for my project. I tried to look for other solutions which doesn't really work so.

Here is the code in Home Fragment:

imageFolder.putFile(saveUri)
    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            mDialog.dismiss();
            Toast.makeText(getActivity(), "Uploaded", Toast.LENGTH_SHORT).show();
            imageFolder.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    newBeach = new Beach();
                    newBeach.setDescription(beach_description.getText().toString());
                    newBeach.setImage(uri.toString());
                    newBeach.setName(beach_name.getText().toString());
                    newBeach.setPrice(beach_price.getText().toString());
                }
            });
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            mDialog.dismiss();
            Toast.makeText(getActivity(),""+e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    })
    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
            double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
            mDialog.setMessage("Uploaded " + progress + "%");
        }
    });

Here is the onCreateView:

    @Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    getActivity().setTitle("HOME");

    homeView= inflater.inflate(R.layout.fragment_home, container, false);

    recycler_menu = (RecyclerView)homeView.findViewById(R.id.recycler_menu);
    recycler_menu.setLayoutManager(new LinearLayoutManager(getContext()));


    database = FirebaseDatabase.getInstance();
    storage = FirebaseStorage.getInstance();
    storageReference = storage.getReference();
    beach =database.getReference("Beach Houses");
    cottages =database.getReference("Cottages");
    bigvenues =database.getReference("Big Venues");
    glamping =database.getReference("Glamping");


    beachHouseBTN = (Button) homeView.findViewById(R.id.beachHouseBTN);
    beachHouseBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menuID.equals("01");
            loadMenuBeach();

        }
    });

    cottagesBTN = (Button) homeView.findViewById(R.id.cottagesBTN);
    cottagesBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menuID.equals("02");
            loadMenuCottages();

        }
    });

    bigVenuesBTN = (Button) homeView.findViewById(R.id.bigVenuesBTN);
    bigVenuesBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menuID.equals("03");
            loadMenuBigVenues();

        }
    });


    return homeView;

}

add_new_item_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fbutton="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="4dp">

<LinearLayout
    android:layout_margin="20dp"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/beach_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="name"
        android:text=""
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="18sp"
        app:met_baseColor="@color/colorPrimary"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true" />

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/beach_description"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:gravity="fill_horizontal"
        android:hint="description"
        android:inputType="textMultiLine"
        android:text=""
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="18sp"
        app:met_baseColor="@color/colorPrimary"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true" />

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/beach_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="price"
        android:inputType="number"
        android:text=""
        android:textColor="@color/colorPrimary"
        android:textColorHint="@color/colorPrimary"
        android:textSize="18sp"
        app:met_baseColor="@color/colorPrimary"
        app:met_floatingLabel="highlight"
        app:met_primaryColor="@color/colorPrimary"
        app:met_singleLineEllipsis="true" />

    <LinearLayout
        android:weightSum="2"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btnSelect"
            android:text="SELECT"
            android:textColor="@android:color/white"
            android:layout_marginRight="8dp"
            android:layout_marginLeft="8dp"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="8dp"
            />
        <Button
            android:id="@+id/btnUpload"
            android:text="UPLOAD"
            android:textColor="@android:color/white"
            android:layout_marginRight="8dp"
            android:layout_marginLeft="8dp"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_margin="8dp"

            />

    </LinearLayout>
</LinearLayout>

</android.support.v7.widget.CardView>

HomeFragment extends Fragment:

public class HomeFragment extends Fragment
{
private View homeView;
private RecyclerView recycler_menu;
Button beachHouseBTN, cottagesBTN, bigVenuesBTN, glampingBTN;
String menuID="";
FirebaseDatabase database;
FirebaseStorage storage;
StorageReference storageReference;
DatabaseReference beach, glamping, bigvenues, cottages;
FirebaseRecyclerAdapter<Beach, MenuViewHolder> adapter1;
FirebaseRecyclerAdapter<Cottages, MenuViewHolder> adapter2;
FirebaseRecyclerAdapter<BigVenues, MenuViewHolder> adapter3;
FirebaseRecyclerAdapter<Glamping, MenuViewHolder> adapter4;

//Add New Item Layout
MaterialEditText beach_name, beach_description, beach_price;
Button btnSelect, btnUpload;

FloatingActionButton fab;

Beach newBeach;
Cottages newCottage;
BigVenues newBigVenues;

Uri saveUri;
DrawerLayout drawer;
}

1 Answers1

0

The beach description variable is not being initiated inside your on onCreate.

You need to have something like

MaterialEditText beach_description = (MaterialEditText) getActivity().findViewByID(R.id.beach_description);

And that should solve your issue.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
  • MaterialEditText beach_name = (MaterialEditText) getActivity().findViewById(R.id.beach_name); gives me an error of unexpected cast to MaterialEditText – Christian De Silva Feb 20 '19 at 03:21
  • Well, the null error is solved. What else does the stack trace tell you? – Francislainy Campos Feb 20 '19 at 03:23
  • I've got no error on this two: MaterialEditText beach_description = (MaterialEditText) getActivity().findViewById(R.id.beach_description); MaterialEditText beach_price = (MaterialEditText) getActivity().findViewById(R.id.beach_price); but won't work on MaterialEditText beach_name = (MaterialEditText) getActivity().findViewById(R.id.beach_name); gives me an error of unexpected cast to MaterialEditText why is that so? when the declaration is all the same? – Christian De Silva Feb 20 '19 at 03:24
  • Unexpected cast to MaterialEditText: layout tag was TextView (Id bound to a TextView in activity_details.xml) – Christian De Silva Feb 20 '19 at 03:25
  • I'm kinda new to this stuff. so I'm kinda sorry that I'm not understanding most of it – Christian De Silva Feb 20 '19 at 03:27
  • That's okay. I just think it may be worth watching some tutorials on binding views against layouts and how to use fragments. I don't know why your error is talking about a textview on activity_details.xml if you're using the fragment_home layout but then you posted here as being called add_new_item_layout.xml. Unless we know for sure what's the name of the layout you're using it may get a bit hard to provide much help. – Francislainy Campos Feb 20 '19 at 03:33