I have the following
public enum Gender {MALE, FEMALE}
and
public class Person {
private String name;
private Gender gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
}
In a FreeMarker termplate ${person.name} works fine, but ${person.gender} gives Expression person.gender is undefined on ....
Any idea why?
user person.gender.MALE or person.gender.FEMALE
The problem was when the method getGender was returning null. Although the error message is not intuitive.
Related
I'm new to java reflexion and I'm trying to integrate SQLite with java.
I have 2 objects Person and Department. There is relation OneToMany between them.
As I'm working on save functionality (SQLite) I want to extract field names and its values so I can build full query. I have no problem with extracting names and values of fields that are of primitive type (String, int etc.). I have problem with type of Object (in this case it is Department field in Person object).
I'm able to print object but unable to access its fields (namely pk).
Could you help me please?
METHOD FOR EXTRACTING FIELDS
// method for extracting fields
private StringBuilder getFieldsWithValues(Object entity) throws IllegalAccessException, NoSuchFieldException {
StringBuilder query = new StringBuilder();
for (Field field : entity.getClass().getDeclaredFields()) {
System.out.print((field.getName() + " - "));
field.setAccessible(true);
// TODO: eliminate if statement from for cycle
if (field.isAnnotationPresent(ManyToOne.class)) {
// HERE I want to extract the pk value from Department object
System.out.println(field.get(entity));
} else {
System.out.println(field.get(entity));
}
}
return query;
}
DEPARTMENT OBJECT
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class Department {
#Id
private long pk;
private String name;
private String code;
public Department() {
}
public Department(String name, String code) {
this.name = name;
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String toString() {
return String.format("Department %d: %s (%s)", pk, name, code);
}
}
PERSON OBJECT
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
#Entity
public class Person {
#Id
private long id;
private String surname;
private String name;
private int age;
#ManyToOne
private Department department;
public Person(String surname, String name, int age) {
this.surname = surname;
this.name = name;
this.age = age;
}
public Person() {
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getId() {
return id;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
#Override
public String toString() {
return String.format("Person %d: %s %s (%d)", id, surname, name, age);
}
}
public class Client {
private String idNum;
private int driverLicence;
private String name;
private String surname;
private String mailAddress;
private String address;
private int phoneNum;
public Client(String idNum, int driverLicence, String name, String surname, String mailAddress, String address, int phoneNum) {
this.address=address;
this.driverLicence=driverLicence;
this.idNum=idNum;
this.mailAddress=mailAddress;
this.name=name;
this.phoneNum=phoneNum;
this.surname=surname;
}
public String getIdNum() {
return idNum;
}
public void setIdNum(String idNum) {
this.idNum = idNum;
}
public int getDriverLicence() {
return driverLicence;
}
public void setDriverLicence(int driverLicence) {
this.driverLicence = driverLicence;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getMailAddress() {
return mailAddress;
}
public void setMailAddress(String mailAddress) {
this.mailAddress = mailAddress;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
this.address = address;
}
public int getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(int phoneNum) {
this.phoneNum = phoneNum;
}
}
THE VALUE OF THE FIELD Client.idNum IS NOT USED
for some reason i am getting this kind of error on every field i have written on this class
ALL getters and setters are generated from eclipse
and all my other classes are fine but for some reason this specific class gives this "error"
i have wasted a lot of time on this and can't seem to find the reason why this happends
any ideas?
I copy pasted my code in, and an issue that may be causing your problem is that the code below returns the incorrect instance variable. Your instance variable is "address" not "Address".
public String getAddress() {
return Address;
}
I 'm trying to use Fastjson library for JSON serialization.
When I try to deserialize , it fails showing no default constructor error.
Note: My class here is a toy example. I realty, it contains so many references to other classes which are in other maven projects and its practically not possible to modify every class.
Here is the code.
Student s = new Student("vineel", "20");
String hell = JSON.toJSONString(s);
Student model2 = JSON.parseObject(hell, Student.class);
System.out.println(model2);
public class Student {
private String name;
private String age;
Student(String name,String age){
this.name = name;
this.age = age;
}
#override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
Here is the error:
Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class com.alibaba.fastjson.Student
at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:467)
at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:213)
at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:656)
at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:573)
at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:386)
at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:658)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:365)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:269)
at com.alibaba.fastjson.JSON.parseObject(JSON.java:488)
at com.alibaba.fastjson.JSON.main(JSON.java:1068)
Change constructor to.
#JsonCreator
public Student(#JsonProperty("name") String name, #JsonProperty("age") String age){
this.name = name;
this.age = age;
}
So create a TO class.
Student model2 = JSON.parseObject(hell, StudentTO.class).asStudent();
System.out.println(model2);
public class StudentTO {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Student asStudent() {
return new Student(name, age);
}
}
I have created a class of Patient object containing patient name and gender and I want to remove it base on Patient name. What is the correct way to do it?
This is my Patient object:
class Patient {
private String name;
private int gender;
public Patient(String name, int gender){
this.name = name;
this.gender = gender;
}
public String getName(){
return this.name;
}
public int getGender(){
return this.gender;
}
public void setName(String name){
this.name = name;
}
public void setGender(int gender){
this.gender = gender;
}
}
This is my Treeset declaration: private TreeSet<Patient> ts = new TreeSet<Patient>(new nameComp());
This is my remove method (I don't know how to start)
void RemovePatient(String patientName) {
}
Just iterating and removing while doing so, will result in a Concurrent Modification Exception. You could temp save the item to remove and remove it later:
For example:
void removePatient(String patientName) {
Person deleteThat;
for (Patient p : ts){
if(p.getName().equals(patientName){
deleteThat = p;
}
}
if(deleteThat != null){
ts.remove(deleteThat);
}
}
Please help me to why am getting strange output for this Below Code.....
why am getting null for the getName().
Output :
List Check :null:1
public class ListTest
{
public static void main(String args[])
{
List<Movie> lst = new java.util.ArrayList<Movie>();
lst.add(new Movie("move1", "genre1"));
System.out.println("List Check :" + lst.get(0).getName() + ":"
+ lst.size());
}
}
class Movie
{
private String name;
private String genre;
public Movie(String name, String genre)
{
name = this.name;
genre = this.genre;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getGenre()
{
return genre;
}
public void setGenre(String genre)
{
this.genre = genre;
}
}
This is wrong:
public Movie(String name, String genre)
{
name = this.name;
genre = this.genre;
}
should be
public Movie(String name, String genre)
{
this.name = name;
this.genre = genre;
}
like in the setters.
You are assigning parameters using this.name should be other way around
public Movie(String name, String genre)
{
this.name = name;
this.genre = genre;
}
your constructor is wrong, it should be
public Movie(String name, String genre)
{
this.name = name;
this.genre = genre;
}
The local variables, name and genre are being assigned to the global variable names of the same name in the constructor of Movie. The default value of Object types is null so these variables remain unassigned. The corrected constructor should appear as
public Movie(String name, String genre) {
this.name = name;
this.genre = genre;
}
Reimeus has it right.
The "this" refers to the class itself, so "this.genre" would refer to the class variable "genre".
Switch them around to fix the problem.
When you write name = this.name, you are assigning the value of the this.name to name. So in your case, this.name holds null when initialised and you are assigning it to name.
It is a good practice to use the getters and setters that you have written in your bean.
you can set it like setName(name) instead of writing this.name=name. Both eventually perform the same action though.