It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
That's the error i get when trying to execute my program. Honestly i really dont know where to start my code.
i have a button name old and new.
i want the textfieldMemberID to display yearToday and an array of members who joined the organization. example if i click new, textfieldMemberId value will automatically display "2012-000001" and if ic lick again new then it will display "2012-000002".
'
i have a Member Class :
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class Member {
private ArrayList accounts;
private String MemberId;
private String VotersId;
private String FirstName;
private String MiddleName;
private String LastName;
private String LotNo;
private String Street;
private String Barangay;
private String City;
private String Region;
private int Age;
private String Gender;
private Date Birthday;
private String ContactNo;
private String EmailAddress;
private int size=0;
private int yearToday = Calendar.getInstance().get(Calendar.YEAR);
public Member(String MemberId) {
NumberFormat formatter = new DecimalFormat("000000");
size= this.accounts.size()+1;
this.setMemberId(""+yearToday + "-" + (formatter.format(size)));
} //im trying to have the solution here
public Member(String MemberId,String FirstName,String MiddleName,String LastName){
this.setMemberId(MemberId);
this.setFirstName(FirstName);
this.setMiddleName(MiddleName);
this.setLastName(LastName);
}
public int getAge() {
return Age;
}
public void setAge(int Age) {
this.Age = Age;
}
public String getBarangay() {
return Barangay;
}
public void setBarangay(String Barangay) {
this.Barangay = Barangay;
}
public Date getBirthday() {
return Birthday;
}
public void setBirthday(Date Birthday) {
this.Birthday = Birthday;
}
public String getCity() {
return City;
}
public void setCity(String City) {
this.City = City;
}
public String getContactNo() {
return ContactNo;
}
public void setContactNo(String ContactNo) {
this.ContactNo = ContactNo;
}
public String getEmailAddress() {
return EmailAddress;
}
public void setEmailAddress(String EmailAddress) {
this.EmailAddress = EmailAddress;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public String getGender() {
return Gender;
}
public void setGender(String Gender) {
this.Gender = Gender;
}
public String getLastName() {
return LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public String getLotNo() {
return LotNo;
}
public void setLotNo(String LotNo) {
this.LotNo = LotNo;
}
public String getMemberId() {
return MemberId;
}
public void setMemberId(String MemberId) {
this.MemberId = MemberId;
}
public String getMiddleName() {
return MiddleName;
}
public void setMiddleName(String MiddleName) {
this.MiddleName = MiddleName;
}
public String getRegion() {
return Region;
}
public void setRegion(String Region) {
this.Region = Region;
}
public String getStreet() {
return Street;
}
public void setStreet(String Street) {
this.Street = Street;
}
public String getVotersId() {
return VotersId;
}
public void setVotersId(String VotersId) {
this.VotersId = VotersId;
}
public ArrayList getAccounts() {
return accounts;
}
public void setAccounts(ArrayList accounts) {
this.accounts = accounts;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Member other = (Member) obj;
if ((this.MemberId == null) ? (other.MemberId != null) : !this.MemberId.equals(other.MemberId)) {
return false;
}
return true;
}
#Override
public int hashCode() {
int hash = 5;
hash = 89 * hash + (this.MemberId != null ? this.MemberId.hashCode() : 0);
return hash;
}
#Override
public String toString() {
return "Member{" + "MemberId=" + MemberId + ", VotersId=" + VotersId + ", FirstName=" + FirstName + ", MiddleName=" + MiddleName + ", LastName=" + LastName + ", LotNo=" + LotNo + ", Street=" + Street + ", Barangay=" + Barangay + ", City=" + City + ", Region=" + Region + ", Age=" + Age + ", Gender=" + Gender + ", Birthday=" + Birthday + ", ContactNo=" + ContactNo + ", EmailAddress=" + EmailAddress + '}';
}
}
im trying to call the getmemberId by this code in Button"New"
String last="";
last = member.getMemberId();
this.txtMembersID.setText((last));
but i got this error.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
pls help. thank u.
The issue occurs on this line of your first constructor:
size= this.accounts.size()+1;
Because accounts has not been initialized yet.
Adding this line before it:
accounts = new ArrayList();
Should fix your problem.
Also, for future reference, one of the most helpful pieces of information you can provide here would be the number of the line causing the exception- makes things a lot easier to solve!
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
please teach me how to convert the code into a interface. What should be done is that there is a person class which has a name, address, gender, and phone. then a student, faculty, staff class which inherits the person class. but the student has a course and major, faculty has a rank and research area, and staff has a position and an office
this is my person code
//Person.java
class Person{
private String myName;
private String myAddress;
private char myGender;
private int myPhone;
public Person()
{
myName = "";
myAddress = "";
myGender = 'N';
myPhone = 0;
}
public Person(String name, String address, char gender, int phone){
myName = name;
myAddress = address;
myGender = gender;
myPhone = phone;
}
public String getName(){
return myName;
}
public String getAddress()
{
return myAddress;
}
public char getGender()
{
return myGender;
}
public int getPhone()
{
return myPhone;
}
public void setName(String name)
{
myName = name;
}
public void setAddress(String address)
{
myAddress = address;
}
public void setGender(char gender)
{
myGender = gender;
}
public void setPhone(int phone)
{
myPhone = phone;
}
public String toString()
{
return myName + ", address: " + myAddress + ", gender: " + myGender + ", phone: " + myPhone;
}
}
this is my student code
//Student.java
class Student extends Person
{
private String myCourse;
private String myMajor;
public Student()
{
super();
myCourse = "";
myMajor = "";
}
public Student(String name, String address, char gender, int phone,
String course, String major)
{
super(name, address, gender, phone);
myCourse = course;
myMajor = major;
}
public String getCourse()
{
return myCourse;
}
public String getMajor()
{
return myMajor;
}
public void setCourse(String course)
{
myCourse = course;
}
public void setMajor(String major)
{
myMajor = major;
}
public String toString()
{
return super.toString() + ", course: " + myCourse + ", major: " + myMajor;
}
}
this is my faculty code
//Faculty.java
public class Faculty extends Person
{
private String myRank;
private String myResearch;
public Faculty()
{
super();
myRank = "";
myResearch = "";
}
public Faculty(String name, String address, char gender, int phone, String rank, String research)
{
super(name, address, gender, phone);
myRank = rank;
myResearch = research;
}
public String getRank()
{
return myRank;
}
public String getResearch()
{
return myResearch;
}
public void setRank(String rank)
{
myRank = rank;
}
public void setResearch(String research)
{
myResearch = research;
}
public String toString() {
return super.toString() + ", rank: " + myRank + ", research: " + myResearch;
}
}
this is my staff code
//Staff.java
public class Staff extends Person
{
private String myPosition;
private String myOffice;
public Staff()
{
super();
myPosition = "";
myOffice = "";
}
public Staff(String name, String address, char gender, int phone, String position, String office)
{
super(name, address, gender, phone);
myPosition = position;
myOffice = office;
}
public String getPosition()
{
return myPosition;
}
public String getOffice()
{
return myOffice;
}
public void setPosition(String position)
{
myPosition = position;
}
public void setResearch(String office)
{
myOffice = office;
}
public String toString() {
return super.toString() + ", position: " + myPosition + ", office: " + myOffice;
}
}
enter image description here
please teach me how to convert the code into a interface
Basicly these are just the Models that your application is going to use. You cannot create a user interface having just these Models, but you can make it yourself.
In Java, you have various libraries which help you to create GUI, for example JavaFX or Swing (not really recommended today).
When it comes to the project architecture, what you can use is a design pattern called Model-View-Controller (MVC).
There are also other questions which consider basics of user interfaces in Java, which could help you:
How to create a GUI in Java
or
Create an application GUI by Javafx
And if you are still unsure of which GUI library you should use, see this question:
Java GUI frameworks. What to choose? Swing, SWT, AWT, SwingX, JGoodies, JavaFX, Apache Pivot?
I wrote a REST web service which is returning JSON as below
[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]
But I want the output with the "student" appended as below.
{"student":[{"id":0,"name":"Vishal","age":"23","dob":"21/1/1992","phone":"9966558","sslc":"90","hsc":"90","college":"90"},
{"id":0,"name":"Karthik","age":"27","dob":"14/8/1988","phone":"995674","sslc":"99","hsc":"100","college":"100"},
{"id":0,"name":"Jeeva","age":"29","dob":"10/1/1987","phone":"77422","sslc":"99","hsc":"99","college":"100"},
{"id":0,"name":"Arya","age":"26","dob":"10/1/1989","phone":"55668","sslc":"100","hsc":"99","college":"99"}]}
how can I achieve this output?
Below is the Product Class
#XmlRootElement(name="student")
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Student() {
super();
}
public Student(int id, String name, String age, String dob, String phone,
String sslc, String hsc, String college) {
super();
this.id = id;
this.name = name;
this.age = age;
this.dob = dob;
this.phone = phone;
this.sslc = sslc;
this.hsc = hsc;
this.college = college;
}
private int id;
private String name;
private String age;
private String dob;
private String phone;
private String sslc;
private String hsc;
private String college;
#XmlElement
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#XmlElement
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
#XmlElement
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
#XmlElement
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
#XmlElement
public String getSslc() {
return sslc;
}
public void setSslc(String sslc) {
this.sslc = sslc;
}
#XmlElement
public String getHsc() {
return hsc;
}
public void setHsc(String hsc) {
this.hsc = hsc;
}
#XmlElement
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
#Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age
+ ", dob=" + dob + ", phone=" + phone + ", sslc=" + sslc
+ ", hsc=" + hsc + ", college=" + college + "]";
}
}
Below is the service class.
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
return Response.ok(studentsList).build();
}
Kindly help me to achieve the above mentioned output.
Thanks in Advance.
To get the desired output, you will have to create one single root object containing a List<Student> student and return it:
Root.java
#XmlRootElement(name="root")
public class Root implements Serializable {
#XmlList
private List<Student> student = new ArrayList<Student>();
// getter and setter
}
Service.java
#GET
#Path("/student.srv")
#Produces("application/json")
public Response getStudentJson(){
DAOLayer daoLayer=new DAOLayer();
List<Student> studentsList=null;
try {
studentsList=daoLayer.getStudents();
} catch (SQLException e) {
e.printStackTrace();
}
Root root = new Root();
root.setStudent(studentsList),
return Response.ok(root).build();
}
I am using Framework for Integrated Test. I know how to use ColumnFixture, RowFixture and ActionFixture basically. Now my problem is, if I have nested objects like Customer object is holding Address object with some fields, how can I parse such kind of objects.
ex:
package com.sample;
import java.sql.Date;
public class Customer {
private String name;
private int no;
private Date dob;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
#Override
public String toString() {
return "Customer [name=" + name + ", no=" + no + ", dob=" + dob
+ ", address=" + address + "]";
}
}
package com.sample;
public class Address {
private int dno;
private String street;
private String city;
public int getDno() {
return dno;
}
public void setDno(int dno) {
this.dno = dno;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
#Override
public String toString() {
return "Address [dno=" + dno + ", street=" + street + ", city=" + city
+ "]";
}
}
Now, in my my fixture, I want to check (using ActionFixture) getCustomer() method that returns customer object. Now in the parse(String s, Type) where 's' is string format of customer object coming from input file, how can I convert it into Customer object.
Is my approach proper?
This is my class containing setters and getters
package Pack;
public class Details {
String FirstName,LastName,City,Country;
public Details(String firstName, String lastName, String city,
String country) {
super();
FirstName = firstName;
LastName = lastName;
City = city;
Country = country;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
}
===========================================================================
This is my main()
package Pack;
public class MainClass {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Details d = new Details("Hari","L","Bangalore","India");
}
}
==========================================================================
I know my main() is incomplete. What should i write to display the contents of "d"?
There are two ways.
One, just print each property of your details object :
System.out.println("FirstName :"+d.getFirstName()); etc..
Or, a better option would be to override toString() method in your class
public void toString() {
return this.getFirstName()+ " " + this.getLastName()+" "+.... ;
}
and then just print your class System.out.println(d);
you need a toString() method in Details class:
public String toString(){
return this.firstName + " " + this.lastName + ", " + this.city + " " + this.country;
}
and
System.out.println(d.toString());
in main
Override toString() method in Details as follow and then just call to print what you want:
public String toString(){
return this.firstName+" "+this.lastName+" "+this.city+" "+this.country;
}
in main just call it as System.out.println(d);
Something like this?
System.out.printf("%s %s (%s, %s)\n", d.getFirstName(), d.getLastName(), d.getCity(), d.getCountry());
I would make your fields (FirstName, LastName, City, and Country) private, otherwise there's not much point in using getters and setters.
Try to add methods (or something similar with more properties):
public String asFirstnameLastname()
{
return firstName + " " + lastName;
}
public String asLastNameFirstname()
{
return lastName + " " + firstName;
}
toString() is also a good choice.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my class for Address Book Entry
public class AddressBookEntry {
private String firstname;
private String lastname;
private int streetno;
private String street;
private String city;
private int telephonenumber;
private String emailaddress;
public AddressBookEntry(String firstname, String lastname, int streetno,
String street, String city, int telephonenumber, String emailaddress) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.streetno = streetno;
this.street = street;
this.city = city;
this.telephonenumber = telephonenumber;
this.emailaddress = emailaddress;
}
public AddressBookEntry() {
// TODO Auto-generated constructor stub
}
THESE ARE THE SETTER-GETTER
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getStreetno() {
return streetno;
}
public void setStreetno(int streetno) {
this.streetno = streetno;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public int getTelephonenumber() {
return telephonenumber;
}
public void setTelephonenumber(int telephonenumber) {
this.telephonenumber = telephonenumber;
}
public String getEmailaddress() {
return emailaddress;
}
public void setEmailaddress(String emailaddress) {
this.emailaddress = emailaddress;
}
THIS IS THE CUSTOM METHODS
public String fullName() {
return this.firstname + " " + this.lastname;
}
public String fullAddress() {
return this.streetno + " " + this.street + " st. " + this.city + " city";
}
public void displayAddress(){
System.out.println("Name : " + fullName());
System.out.println("Address : " + fullAddress());
System.out.println("Telephone Number : " + this.telephonenumber);
System.out.println("Email Address : " + this.emailaddress + "#gmail.com"+"\n");
}
}
NOW THIS IS THE CLASS FOR THE ADDRESSBOOK
import java.util.ArrayList;
import java.util.List;
public class AddressBook {
private List<AddressBookEntry> listOfEntries;
public AddressBook() {
this.listOfEntries = new ArrayList<>();
}
public void add(AddressBookEntry addressBookEntry) {
this.listOfEntries.add(addressBookEntry);
}
public void delete(int index) {
this.listOfEntries.remove(index);
}
public AddressBookEntry get(int index) {
return this.listOfEntries.get(index);
}
public AddressBookEntry[] viewAll() {
AddressBookEntry[] result = new AddressBookEntry[this.listOfEntries.size()];
this.listOfEntries.toArray(result);
return result;
}
}
How can I create a class address book that has a limit of 100 entries of AddressBookEntry objects using the given code above using arrays? While it includes a menu: AddEntry, DeleteEntry, ViewAllEntries, and UpdateEntry?
If you want fixed-sized you should use an array instead of List.
If for some reason you cannot change the list, you can provide the size to the ArrayList constructor.
public AddressBook() {
this.listOfEntries = new ArrayList<>(100);
}
However, you need to check the size before inserting the element.
public void add(AddressBookEntry addressBookEntry) {
if(this.listOfEntries.size() <= 100)
this.listOfEntries.add(addressBookEntry);
else
throw new IllegalStateException("Address book is full");
}