0

so I'm getting error when i try to upload data in firebase.i want in following format enter image description here

I tried to do changes and it done for 1st time but...after some days it's showing me error.

I think i've to give litte more details here.So basically I'm trying to upload the data

Upload Data before scrolling down

Next Part after scroll down

but as soon as i cliked save button it give me following error error

I'm using firebase realtime database and firebase storage

this are some code :

textActivity.java(code for uploading the data and img

public class textActivity extends AppCompatActivity {
    TextView ProductName, ProductBrand, ProductExpiry, ProductQty, ProductCalories, ProductSugar,         ProductFibers, ProductCarbs;

    ImageView uploadbtn, productImage;
    Button submit;
    Uri ImageUri;
    RelativeLayout relativeLayout;
    ProgressDialog dialog;
    BottomNavigationView navigationView;
    public static final String NOTIFICATION_CHANNEL_ID = "10001";
    private final static String default_notification_channel_id = "default";
    final Calendar myCalendar = Calendar.getInstance();


     private  FirebaseDatabase database;
    //   private DatabaseReference reference;
     private FirebaseStorage firebaseStorage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState)  
    setContentView(R.layout.activity_text);
    dialog = new ProgressDialog(this);
    dialog.setMessage("Please wait...")
    dialog.setCancelable(false);
    dialog.setTitle("Product Uploading");
    dialog.setCanceledOnTouchOutside(false);

      
    uploadbtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    UploadImage();
    relativeLayout.setVisibility(View.VISIBLE);
    uploadbtn.setVisibility(View.GONE);
    }});
    submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
                // saveData();
    dialog.show();
    final StorageReference reference =   firebaseStorage.getReference().child("Products").child(System.currentTimeMillis() + "");

    reference.putFile(ImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot> () {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
    ProductDetailHelperClass model = new ProductDetailHelperClass();
    model.setProductImage(uri.toString());

    model.setProductName(ProductName.getText().toString());
    model.setProductBrand(ProductBrand.getText().toString());
    model.setProductExpiry(ProductExpiry.getText().toString());
    model.setProductQty(ProductQty.getText().toString());
    model.setProductCalories(ProductCalories.getText().toString());
    model.setProductSugar(ProductSugar.getText().toString());
    model.setProductFibers(ProductFibers.getText().toString());
    model.setProductCarbs(ProductCarbs.getText().toString());
                                
                                   FirebaseDatabase.getInstance().getReference("CompleteData").child("UserProductDetails").setValue(model).addOnSuccessListener(new OnSuccessListener<Void>() {
    @Override
    public void onSuccess(Void unused) {
    Toast.makeText(textActivity.this, "Product Upload Successfully", Toast.LENGTH_SHORT).show();
                                                dialog.dismiss();

    } }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
    dialog.dismiss();
    Toast.makeText(textActivity.this, "Error occured", Toast.LENGTH_SHORT).show();
    }});  }  });} }); } });

   

     ProductExpiry.setOnClickListener(new View.OnClickListener() {
     @Override

     public void onClick(View v) {
     final Calendar c = Calendar.getInstance();

    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    DatePickerDialog datePickerDialog = new DatePickerDialog(textActivity.this,
    new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
     // on below line we are setting date to our edit text.
    ProductExpiry.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
      }},// on below line we are passing year,  // month and day for selected date in our date picker.
     year, month, day);
     datePickerDialog.show();
       }}); }
    private void UploadImage() {

    Dexter.withContext(this)
    .withPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
    .withListener(new PermissionListener() {
    @Override
    public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent,101);
    }
    @Override
     public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
                        Toast.makeText(textActivity.this,"Permission    Denied",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {

    permissionToken.continuePermissionRequest();
    } }).check();



}

    @Override
    protected void onActivityResult(int requestCode, int resulttCode, @Nullable Intent data){
    super.onActivityResult(requestCode,resulttCode,data);
    if(requestCode==101 && resulttCode ==RESULT_OK){
          ImageUri =  data.getData();
            productImage.setImageURI(ImageUri);

        }
    }

ProductDetailHelperClass(Helper Class)

package com.example.myapplication;

public class ProductDetailHelperClass {

    public String ProductName, ProductBrand, ProductExpiry, ProductQty,ProductCalories,ProductSugar,ProductFibers,ProductCarbs;
    public String productImage;

    public ProductDetailHelperClass() {
    }




    public ProductDetailHelperClass(String productName, String productBrand, String productExpiry, String productQty, String productCalories, String productSugar, String productFibers, String productCarbs, String productImage) {


        ProductName = productName;
        this.ProductBrand = productBrand;
        ProductExpiry = productExpiry;
        ProductQty = productQty;
        ProductCalories = productCalories;
        ProductSugar = productSugar;
        ProductFibers = productFibers;
        ProductCarbs = productCarbs;
        this.productImage = productImage;
    }

    public String getProductName() {
        return ProductName;
    }

    public void setProductName(String productName) {
        ProductName = productName;

    }

    public String getProductBrand() {
        return ProductBrand;
    }

    public void setProductBrand(String productBrand) {
        this.ProductBrand = productBrand;
    }

    public String getProductExpiry() {
        return ProductExpiry;
    }

    public void setProductExpiry(String productExpiry) {
        ProductExpiry = productExpiry;
    }

    public String getProductQty() {
        return ProductQty;
    }

    public void setProductQty(String productQty) {
        ProductQty = productQty;
    }

    public String getProductImage() {
        return productImage;
    }

    public void setProductImage(String productImage) {
        this.productImage = productImage;
    }
    public String getProductCalories() {
        return ProductCalories;
    }

    public void setProductCalories(String productCalories) {
        ProductCalories = productCalories;
    }

    public String getProductSugar() {
        return ProductSugar;
    }

    public void setProductSugar(String productSugar) {
        ProductSugar = productSugar;
    }

    public String getProductFibers() {
        return ProductFibers;
    }

    public void setProductFibers(String productFibers) {
        ProductFibers = productFibers;
    }

    public String getProductCarbs() {
        return ProductCarbs;
    }

    public void setProductCarbs(String productCarbs) {
        ProductCarbs = productCarbs;
    }}

ProductAdater.java

public class ProjectAdapter extends 
RecyclerView.Adapter<ProjectAdapter.ViewHolder> {

ArrayList<ProductDetailHelperClass> list;
Context context;

public ProjectAdapter(ArrayList<ProductDetailHelperClass> list, 
Context context) {
this.list = list;
this.context = context;
}



@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
viewType) {
View view = 
LayoutInflater.from(parent.getContext()).inflate(R.layout.item, 
parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int 
position) {
ProductDetailHelperClass model = list.get(position);


       Picasso.get().load(model.getProductImage()).placeholder(R.drawable.jam2).into(holder.itemImage);
holder.itemHeadLine.setText(model.getProductName());
//   holder.itemBrand.setText(model.getProductBrand());         
holder.itemQty.setText(model.getProductQty());

holder.itemExpiry.setText(model.getProductExpiry());

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent  intent = new Intent(context,SingleProductActivity.class);
intent.putExtra("singleImg",model.getProductImage());
            
intent.putExtra("singleHeadLine",model.getProductName());
intent.putExtra("singleBrand",model.getProductBrand());
intent.putExtra("singleQty",model.getProductQty());
           
intent.putExtra("singleExpiry",model.getProductExpiry());
            
intent.putExtra("singleCal",model.getProductCalories());
intent.putExtra("singleSugar",model.getProductSugar());
            
intent.putExtra("singleFiber",model.getProductFibers());
intent.putExtra("singleCarbs",model.getProductCarbs());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

} });}

@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

TextView itemHeadLine, itemQty, itemBrand, itemExpiry;
ImageView itemImage;

public ViewHolder(@NonNull View itemView) {
super(itemView);

itemHeadLine = itemView.findViewById(R.id.itemHeadLine);
itemQty = itemView.findViewById(R.id.itemQty);
// itemBrand= itemView.findViewById(R.id.itemBrand);
itemExpiry = itemView.findViewById(R.id.itemExpiry);
itemImage = itemView.findViewById(R.id.itemImage);
}}}

`

1 Answers1

0

The Firebase SDK maps between your Java object and the JSON properties in the database based on JavaBean property naming conventions, which means it looks at the public getters/setters and fields in your class. If you look at product brand that gives us:

package com.example.myapplication;

public class ProductDetailHelperClass {
                               // 
    public String ProductName, ProductBrand, ProductExpiry, ProductQty,ProductCalories,ProductSugar,ProductFibers,ProductCarbs;
    public String productImage;
    ...
                   // 
    public String getProductName() {
        return ProductName;
    }
                   // 
    public void setProductName(String productName) {
        ProductName = productName;

    }
    ...

Again, according to JavaBean naming conventions, that first public field defines a property:

ProductBrand

While the getter/setter define a property:

productBrand

Those are two different casings for the same properties, which is what the error message points out.

Simplest solutions:

  • Remove all the getters/setters, and just use public fields for everything.
  • Make your fields private, as folks are supposed to use the getter/setter to access them.
  • Make the fields following the naming convention of the properties defined by the getters/setters, so productBrand rather than ProductBrand.
  • Use @PropertyName annotations on the getter/setter to explicitly map them to ProductBrand/

Neither of these is pertinently better than the others, so pick whatever feels most natural for you.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807