I have an object in a firebase database that I am trying to extract from a snapshot.
The JSON object from firebase is:
"-KoVHZ8YVll0RiI2GwKb" : {
"name" : "Name 1",
"phone_number" : "4443335555"
}
I am trying to safe it in an object that looks like this
public class Contact {
private String mName;
private String mPhoneNumber;
public Contact() {
mName = "";
mPhoneNumber = "";
}
public String getName() { return mName; }
public String getPhoneNumber() { return mPhoneNumber; }
public void setName(final String name) { mName = name; }
public void setPhoneNumber(final String phoneNumber) { mPhoneNumber = phoneNumber; }
}
I am calling
Contact contact = snapshot.getValue(Contact.class);
The contact object only has the name populated and not the phone number. The documentation just states that there must be public getters and an empty constructor in order for this to work. My guess is something is wrong with my naming convention anyone have any ideas?
EDIT
I am aware that I can extract the data out by doing this:
mName = (String) snapshot.child("nane").getValue();
mPhoneNumber = (String) snapshot.child("phone_number").getValue();
But then what is the point of creating the P.O.J.O.?
Make your Contact.java like this:
#IgnoreExtraProperties
public class Contact {
private String name;
private String phone_number;
public Contact() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone_number() {
return phone_number;
}
public void setPhone_number(String phone_number) {
this.phone_number = phone_number;
}
}
Related
I have Fragments call CategoryFragments where I am adding a list name catList for that I define the variable in Class name CategoryMODEL and variable are
public class CategoryModel {
private String docID;
private String name;
private int noOfTests;
public CategoryModel() {
this.docID = docID;
this.name = name;
this.noOfTests = noOfTests;
}
public String getDocID() {
return docID;
}
public void setDocID(String docID) {
this.docID = docID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNoOfTests() {
return noOfTests;
}
public void setNoOfTests(int noOfTests) {
this.noOfTests = noOfTests;
}
}
but when I try to use add item in my catLIst I get error that Change Signature of CategoryModel
Because you have an empty constructor but inside your are init the values with themself, so create a constructor with params:
public CategoryModel(String docId, String name, int noOfTests) {
// Init your scope variables
}
I am just wondering why my getTotal_Score() is null in my model class but I can retrieve it successfully from the database...I have tried both capital Integer and just a normal int, but that doesn't work...I have also tried Long type but I still get a null variable..The other thing that is puzzling me too is that when I use int type instead of long, it doesn't work.... Can you retrieve the numbers as int simple by doing:
int score = (int) snapshop.child("Glen Family Medical Centre").child("Total Score");
I can only get the above to work with long type...
public class Information { // variables have to match in firebase database or it will show null
private String Address;
private String Name;
private String Phone_No;
private String Suburb;
private String State;
private String Postcode;
private String Doctor;
private int Total_Score;
#SuppressWarnings("unused")
public Information() {
}
#SuppressWarnings("unused")
public Information(String address, String name, String phone_No, String suburb, String state, String postcode, String doctor, int total_score) {
Address = address;
Name = name;
Phone_No = phone_No;
Suburb = suburb;
State = state;
Postcode = postcode;
Doctor = doctor;
Total_Score = total_score;
}
public int getTotal_Score() {
return Total_Score;
}
public void setTotal_Score(int total_Score) {
Total_Score = total_Score;
}
public String getAddress() {
return Address;
}
#SuppressWarnings("unused")
public void setAddress(String address) {
Address = address;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone_No() {
return Phone_No;
}
#SuppressWarnings("unused")
public void setPhone_No(String phone_No) {
Phone_No = phone_No;
}
public String getSuburb() {
return Suburb;
}
#SuppressWarnings("unused")
public void setSuburb(String suburb) {
Suburb = suburb;
}
public String getState() {
return State;
}
#SuppressWarnings("unused")
public void setState(String state) {
State = state;
}
public String getPostcode() {
return Postcode;
}
#SuppressWarnings("unused")
public void setPostcode(String postcode) {
Postcode = postcode;
}
public String getDoctor() {
return Doctor;
}
#SuppressWarnings("unused")
public void setDoctor(String doctor) {
Doctor = doctor;
}
}
In my other class, I have used:
Information info = snapshot.getValue(Information.class);
assert info != null;
String txt = "Medical Clinic: " + info.getName() + "Total Score: " + info.getTotal_Score();
list.add(txt);
I have a model and repository. Model has getter and setter values which has to be added in the response. My model is like follows
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.katharsis.resource.annotations.JsonApiId;
import io.katharsis.resource.annotations.JsonApiResource;
#JsonApiResource(type="employee") //no i18n
public class Employee {
#JsonApiId
private String name;
private int emp_id;
private String dob;
private String profile_url;
private String status_message;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getEmp_id() {
return this.emp_id;
}
public void setEmp_id(int empid) {
this.emp_id = empid;
}
public String getDob() {
return this.dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getProfile_url() {
return this.profile_url;
}
public void setProfile_url(String url) {
this.profile_url = url;
}
public String getStatus_message() {
return this.status_message;
}
public void setStatus_message(String message) {
this.status_message = message;
}
}
Here the variable status_message represents my api status. The api response should be like
When the database has the employee with inputted id
{name : "rajasuba", emp_id : "123", dob : "March301993", profile_url : "https:", status_message : "success"}
When the employee left the organization the response should be like
{name : "rajasuba", emp_id : "567", status_messsage : "fired"}
When there is no such employee then my response should be like
{status_message : "Invalid employeed id"}
But for all the above cases i am getting all the attribute values. How can i ignore the attribute value (like #JsonIgnore) selectively for a particular scenario?
I don't know what version of katharsis you're using, but in 2.8.2 it's supported
There is a compliation error stating that class name is already define i can't find the way to resolve it
further the class name are declared only once and can't find the place where the things are going wrong
package practo;
import java.io.*;
import java.lang.*;
import java.util.*;
#SuppressWarnings("unused")
class Customer /* compilation error occurs here */
{
private int id;
private String name;
private String email;
private String address;
void setid(int id)
{
this.id=id;
}
int getid()
{
return id;
}
void setname(String name)
{
this.name=name;
}
String getname()
{
return name;
}
void setemail(String email)
{
this.email=email;
}
String getemail()
{
return email;
}
void setaddress(String address)
{
this.address=address;
}
String getaddress()
{
return address;
}
class PhoneNumber
{
private String phoneNumber;
private String heldFromDate;
private String heldToDate;
void setphoneNumber(String phoneNumber)
{
this.phoneNumber=phoneNumber;
}
String getphoneNumber()
{
return phoneNumber;
}
void setheldToDate(String heldToDate)
{
this.heldToDate=heldToDate;
}
String getheldToDate()
{
return heldToDate;
}
public String getHeldFromDate() {
return heldFromDate;
}
public void setHeldFromDate(String heldFromDate) {
this.heldFromDate = heldFromDate;
}
class NumberType
{
private String code;
private String description;
void setcode(String code)
{
this.code=code;
}
void setdescription(String description)
{
this.description=description;
}
String getcode()
{
return code;
}
String getdescription()
{
return description;
}
}
}
}
class x1
{
public void main(String args[])
{
#SuppressWarnings("resource")
Scanner s=new Scanner(System.in);
Customer c=new Customer();
Customer.PhoneNumber p=c.new PhoneNumber();
Customer.PhoneNumber.NumberType n=p.new NumberType();
System.out.println("Enter the customer details");
System.out.println("Enter the id :");
int id=s.nextInt();
c.setid(id);
System.out.println(c.getid());
System.out.println("Enter the name :");
String name=s.nextLine();
c.setname(name);
System.out.println(c.getname());
System.out.println("Enter the email :");
String email=s.nextLine();
c.setemail(email);
System.out.println(c.getemail());
System.out.println("Enter the address :");
String address=s.nextLine();
c.setaddress(address);
System.out.println(c.getaddress());
System.out.println("Enter the customer contact details");
System.out.println("Enter the phone number :");
String phoneNumber=s.nextLine();
p.setphoneNumber(phoneNumber);
System.out.println(p.getphoneNumber());
System.out.println("Enter the held from date (dd/MM/yyyy) :");
String heldFromDate=s.next();
p.setHeldFromDate(heldFromDate);
System.out.println(p.getHeldFromDate());
System.out.println("Enter the held to date (dd/MM/yyyy) :");
String heldToDate=s.next();
p.setheldToDate(heldToDate);
System.out.println(p.getheldToDate());
System.out.println("Enter number type code :");
String code=s.next();
n.setcode(code);
System.out.println(n.getcode());
System.out.println("Enter number type description");
String description=s.next();
n.setdescription(description);
System.out.println(n.getdescription());
}
}
Your class does not give me any compilation error. You might try making the class public i.e. public class Customer and file name having the name Customer.java. It may happen that the package practo already contains a class named Customer.
Can you please verify Customer class is not duplicate ? If it is not there, can you choose Clean from the Project menu, it might fix these errors.
Sometime eclipse trouble us.
Lots of recommendations for improvement:
Open public class per file, and a file for each class. Your arrangement is confusing.
Learn and follow the Java coding standards.
Using a Date for a date instead of a String is a better design, especially with JDK 8 and the java.time package.
Learn JUnit instead of that x1.main.
These are examples of how your classes should look.
Customer.java
package practo;
/**
* Created by Michael
* Creation date 5/29/2016.
* #link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
*/
public class Customer {
private int id;
private String name;
private String email;
private String address;
public Customer() {
this(0, "", "", "");
}
public Customer(int id, String name, String email, String address) {
this.id = id;
this.name = name;
this.email = email;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
PhoneNumber.java:
package practo;
/**
* Created by Michael
* Creation date 5/29/2016.
* #link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
*/
public class PhoneNumber {
private String phoneNumber;
private String heldFromDate; // Bad design. This ought to be a Date, not a String
private String heldToDate; // Bad design. This ought to be a Date, not a String
public PhoneNumber() {
this("", "", "");
}
public PhoneNumber(String phoneNumber, String heldFromDate, String heldToDate) {
this.phoneNumber = phoneNumber;
this.heldFromDate = heldFromDate;
this.heldToDate = heldToDate;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getHeldFromDate() {
return heldFromDate;
}
public void setHeldFromDate(String heldFromDate) {
this.heldFromDate = heldFromDate;
}
public String getHeldToDate() {
return heldToDate;
}
public void setHeldToDate(String heldToDate) {
this.heldToDate = heldToDate;
}
}
Check if you have another class called Customer in the package practo. That would cause a name conflict.
In the java, commons beanutils, try to set property 'address' and 'creditCardList' to object, but it gave me error :
java.lang.NoSuchMethodException: Property 'address' has no setter method in class 'class com.dao.Student'
but I have this method there. The code is here:
public class Main {
public static void main(String[] args) {
Object student = new Student("John");
Object address = new Address("NJ");
try {
PropertyUtils.setProperty(student, "address", address);
//----------
List list = new ArrayList();
Object creditCard = new CreditCard();
list.add(creditCard);
PropertyUtils.setProperty(student, "creditCardList", list);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class Student {
private String name;
private Address address;
private List<CreditCard> creditCardList;
public Student(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public List<CreditCard> getCreditCardList() {
return creditCardList;
}
public void setCreditCardList(List<CreditCard> creditCardList) {
this.creditCardList = creditCardList;
}
}
class Address {
private String name;
public Address(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class CreditCard{
private String cardName;
public String getCardName() {
return cardName;
}
public void setCardName(String cardName) {
this.cardName = cardName;
}
}
Your class Student should be a public class , try making it public and rerun your code.
I moved Student to a own file and made it public, that worked fine :)