eugene007
asked on
sort
In my driver.java I have the code bellow.
public static void disSortedDepartment()
{
Vector dept = cpn.getDepartments();
if(dept.isEmpty())
{
System.out.println("There is no departments in the company");
}
else
{
Collections.sort(dept);
Iterator iter = dept.iterator();
while(iter.hasNext())
{
Department d = (Department) iter.next();
System.out.println(d);
}
}
}
In my department.java I have the following code:
public int compareTo(Object obj)
{
if(this==obj)
{
return 0;
}
if(!(obj instanceof Department))
{
return 0;
}
Department d = (Department) obj;
return this.getDepartmentName().c ompareTo(d .getDepart mentName() );
}
The code seems to be right, but when I try to run it I am getting an exception error.
How do I solve the problem?
You can download the code from:
www.mutaiyas.com/db/Employee.zip
Your help is kindly appreciated.
Regards
Eugene
public static void disSortedDepartment()
{
Vector dept = cpn.getDepartments();
if(dept.isEmpty())
{
System.out.println("There is no departments in the company");
}
else
{
Collections.sort(dept);
Iterator iter = dept.iterator();
while(iter.hasNext())
{
Department d = (Department) iter.next();
System.out.println(d);
}
}
}
In my department.java I have the following code:
public int compareTo(Object obj)
{
if(this==obj)
{
return 0;
}
if(!(obj instanceof Department))
{
return 0;
}
Department d = (Department) obj;
return this.getDepartmentName().c
}
The code seems to be right, but when I try to run it I am getting an exception error.
How do I solve the problem?
You can download the code from:
www.mutaiyas.com/db/Employee.zip
Your help is kindly appreciated.
Regards
Eugene
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Exception in thread "main" java.lang.NullPointerExcep tion
at java.io.Writer.write(Write r.java:126 )
at java.io.PrintStream.write( PrintStrea m.java:303 )
at java.io.PrintStream.print( PrintStrea m.java:462 )
at java.io.PrintStream.printl n(PrintStr eam.java:5 99)
at driver.disSortedDepartment (driver.ja va:354)
at driver.switchmenu(driver.j ava:97)
at driver.menu(driver.java:42 )
at driver.switchmenu(driver.j ava:52)
at driver.menu(driver.java:42 )
at driver.switchmenu(driver.j ava:98)
at driver.menu(driver.java:42 )
at driver.main(driver.java:14 )
at java.io.Writer.write(Write
at java.io.PrintStream.write(
at java.io.PrintStream.print(
at java.io.PrintStream.printl
at driver.disSortedDepartment
at driver.switchmenu(driver.j
at driver.menu(driver.java:42
at driver.switchmenu(driver.j
at driver.menu(driver.java:42
at driver.switchmenu(driver.j
at driver.menu(driver.java:42
at driver.main(driver.java:14
Try changing
>>if(dept.isEmpty())
to
if(dept == null || dept.isEmpty())
>>if(dept.isEmpty())
to
if(dept == null || dept.isEmpty())
ASKER
Im still getting the same error.
Are you sure you printed the right error? ;-)
ASKER
Exception in thread "main" java.lang.NullPointerExcep tion
at java.io.Writer.write(Write r.java:126 )
at java.io.PrintStream.write( PrintStrea m.java:303 )
at java.io.PrintStream.print( PrintStrea m.java:462 )
at java.io.PrintStream.printl n(PrintStr eam.java:5 99)
at driver.disSortedDepartment (driver.ja va:372)
at driver.switchmenu(driver.j ava:100)
at driver.menu(driver.java:45 )
at driver.switchmenu(driver.j ava:55)
at driver.menu(driver.java:45 )
at driver.main(driver.java:14 )
at java.io.Writer.write(Write
at java.io.PrintStream.write(
at java.io.PrintStream.print(
at java.io.PrintStream.printl
at driver.disSortedDepartment
at driver.switchmenu(driver.j
at driver.menu(driver.java:45
at driver.switchmenu(driver.j
at driver.menu(driver.java:45
at driver.main(driver.java:14
ASKER
The latest code can be downloaded from:
www.mutaiyas.com/db/Employee.zip
I have done several types of sorting in my application and it works, only this one seems to trouble me :)
www.mutaiyas.com/db/Employee.zip
I have done several types of sorting in my application and it works, only this one seems to trouble me :)
Can you tell me how to reproduce that error?
ASKER
I found the solution:
In my Department.java I replaced the toString() to
public String toString()
{
String allEmps = "Department : " + deptName + "\n\n";
Iterator ite = getEmployees().iterator();
while (ite.hasNext())
{
Employee e = (Employee) ite.next();
allEmps += e.getName() + "\t\t" + e.getNumber() + "\n";
}
return allEmps;
}
now it works.
In my Department.java I replaced the toString() to
public String toString()
{
String allEmps = "Department : " + deptName + "\n\n";
Iterator ite = getEmployees().iterator();
while (ite.hasNext())
{
Employee e = (Employee) ite.next();
allEmps += e.getName() + "\t\t" + e.getNumber() + "\n";
}
return allEmps;
}
now it works.
OK
ASKER
still has some problems, when there is more than one department.
ASKER
I have done further upgrades on my code, to make it efficient. You can download it from the same path.
>> still has some problems, when there is more than one department.
If you say how to reproduce this, i'll take a look
If you say how to reproduce this, i'll take a look
It will help if you tell us *what* error