apache commons beanutils, how to set property value? - java

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 :)

Related

why this change signatures of java class

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
}

Data integrity when using parallel stream

I have the implemented the below and I have not tested it yet with large amount of data.
public class Main {
public static void main(String[] args) {
CombinedData combinedData = new CombinedData();
List<String> nameList = Arrays.asList("x1", "x3", "x2");
nameList.parallelStream().forEach(name -> {
populateDetails(name, combinedData);
});
}
static void log(String str) {
System.out.println(str);
}
static void populateDetails(String name, CombinedData combinedData) {
if (combinedData.getOrg() == null) {
combinedData.setOrg(MyUtil.getOrg(name));
}
if (combinedData.getRawData() != null) {
combinedData.setRawData(combinedData.getRawData() + name);
}
combinedData.addToDetails(new Details(name, MyUtil.getAdd(name)));
}
}
class CombinedData {
private String org;
private List<Details> details;
private String rawData;
public String getOrg() {
return org;
}
public void setOrg(String org) {
this.org = org;
}
public void addToDetails(Details details) {
this.details.add(details);
}
public List<Details> getDetails() {
return details;
}
public void setDetails(List<Details> details) {
this.details = details;
}
public String getRawData() {
return rawData;
}
public void setRawData(String rawData) {
this.rawData = rawData;
}
}
class Details {
private String name;
private String address;
public Details(
String name,
String address) {
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
If you check the line 13 and 16 I need to add the org to combined response only once and I have added the check there and also I need to append the name to the rawData. My question is that since I'm using parallel stream how would that behave.
Im sure there might be a scenario that when threads are in parallel thing might break.
How can I go ahead and handle that scenario?

How to add data to different model class android

I'm newbie about android programming and Today I want to add data to another model class
and in another class model is same model
but I have no idea to add it
example
my first model
public class TelephoneModel {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
and my second model
public class TelephoneDetailModel {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
and I have data in
List<TelephoneModel.DataBean> databeans = new ArrayList<>;
List<TelephoneDetailModel.DataBean> dataDetailbeans = new ArrayList<>;
and I want to add data databeans to dataDetailbeans
How to add it ,please give example for me,
thanks!
Hello change your model class like this (it is so simple to use)
--> TelephoneModel
public class TelephoneModel {
private String id;
private String name;
public TelephoneModel(String id,String name) {
this.id=id;
this.name=name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--> TelephoneDetailModel
public class TelephoneDetailModel {
public TelephoneDetailModel(String id, String name) {
this.id = id;
this.name = name;
}
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--> your activity
ArrayList<TelephoneModel> databeans = new ArrayList<>();
ArrayList<TelephoneDetailModel> dataDetailBeans = new ArrayList<>();
// add data into databean
databeans.add(new TelephoneModel("id-1", "name-1"));
databeans.add(new TelephoneModel("id-2", "name-2"));
// now add data databeans to dataDetailbeans
for (int i = 0; i < databeans.size(); i++) {
dataDetailBeans.add(new TelephoneDetailModel(databeans.get(i)
.getId(), databeans.get(i).getName()));
}
if any question then free to ask me...
Try this
List<TelephoneDetailModel.DataBean> databeans = new ArrayList();
TelephoneDetailModel.DataBean dataBean = new TelephoneDetailModel.DataBean();
dataBean.setId("1");
dataBean.setName("PREM");
databeans.add(dataBean);
TelephoneDetailModel.DataBean model1 = new TelephoneDetailModel.DataBean();
model1.setId("2");
model1.setName("PREM2");
databeans.add(model1);
TelephoneDetailModel.DataBean model2 = new TelephoneDetailModel.DataBean();
model2.setId("3");
model2.setName("PREM3");
databeans.add(model2);
TelephoneDetailModel.DataBean model3 = new TelephoneDetailModel.DataBean();
model3.setId("4");
model3.setName("PREM4");
databeans.add(model3);
for (int i = 0; i < databeans.size(); i++) {
Log.e("ID", databeans.get(i).getId());
Log.e("NAME", databeans.get(i).getName());
}
Out put
com.example.user33.demoapplication E/ID: 1
com.example.user33.demoapplication E/NAME: PREM
com.example.user33.demoapplication E/ID: 2
com.example.user33.demoapplication E/NAME: PREM2
com.example.user33.demoapplication E/ID: 3
com.example.user33.demoapplication E/NAME: PREM3
com.example.user33.demoapplication E/ID: 4
com.example.user33.demoapplication E/NAME: PREM4
Try this
public class TelephoneDetailModel {
private List<DataBean> data;
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String id;
private String name;
public DataBean() {
}
public DataBean(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
now to add data in your arraylist try this
List<TelephoneDetailModel.DataBean> databeans = new ArrayList();
databeans.add(new TelephoneDetailModel.DataBean("6","NEW NAME"));
DataBean in TelephoneModel class and TelephoneDetailModel is different, you can't add the Datadean to another list

Firebase DataSnapshot Not Filling Object

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;
}
}

PlayFramework Attempted to serialize java.lang.Class

I am trying to integrate gson in my Play! 2.x. I am using gson & jongo and facing this error : http://hastebin.com/agewopocen.bash
Here is my model class:
public class Person extends MongoModel<Person>
{
ObjectId _id;
String name;
int age;
public Person()
{
super(Person.class, "person");
}
public Person(String name, int age)
{
super(Person.class, "person");
this.name = name;
this.age = age;
System.out.println();
}
#Override
public Person setModel()
{
return this;
}
#Override
public ObjectId getId()
{
return null;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String toJson()
{
return new GsonBuilder().create().toJson(this);
}
#Override
public String toString()
{
return new Gson().toJson(this);
}
}
I don't know what a "TypeAdapter" is (mentioned in the error log in the above link) and I am sure that we can get this working without using it. But, I dont know what I am missing.
Here is the code I tried (without Play, in regular Java) and it works.
public class Test
{
public static void main(String[] args)
{
Person nfe = new Person(10, "nfe");
String s = new Gson().toJson(nfe);
System.out.println(s);
}
public static class Person {
int age;
String name;
public Person(int age, String name)
{
this.age = age;
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public void setName(String name)
{
this.name = name;
}
}
}
Output :
{"age":10,"name":"nfe"}
Can someone help me in fixing this?

Categories

Resources