Link to home
Start Free TrialLog in
Avatar of eugene007
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().compareTo(d.getDepartmentName());
}

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>I am getting an exception error.

It will help if you tell us *what* error
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eugene007
eugene007

ASKER

Exception in thread "main" java.lang.NullPointerException
        at java.io.Writer.write(Writer.java:126)
        at java.io.PrintStream.write(PrintStream.java:303)
        at java.io.PrintStream.print(PrintStream.java:462)
        at java.io.PrintStream.println(PrintStream.java:599)
        at driver.disSortedDepartment(driver.java:354)
        at driver.switchmenu(driver.java:97)
        at driver.menu(driver.java:42)
        at driver.switchmenu(driver.java:52)
        at driver.menu(driver.java:42)
        at driver.switchmenu(driver.java:98)
        at driver.menu(driver.java:42)
        at driver.main(driver.java:14)
Try changing

>>if(dept.isEmpty())

to

if(dept == null || dept.isEmpty())
Im still getting the same error.
Are you sure you printed the right error? ;-)
Exception in thread "main" java.lang.NullPointerException
        at java.io.Writer.write(Writer.java:126)
        at java.io.PrintStream.write(PrintStream.java:303)
        at java.io.PrintStream.print(PrintStream.java:462)
        at java.io.PrintStream.println(PrintStream.java:599)
        at driver.disSortedDepartment(driver.java:372)
        at driver.switchmenu(driver.java:100)
        at driver.menu(driver.java:45)
        at driver.switchmenu(driver.java:55)
        at driver.menu(driver.java:45)
        at driver.main(driver.java:14)
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 :)
Can you tell me how to reproduce that error?
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.

OK
still has some problems, when there is more than one department.
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