?
Solved

sort

Posted on 2005-04-17
14
Medium Priority
?
266 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:eugene007
  • 7
  • 7
14 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13800564
>>I am getting an exception error.

It will help if you tell us *what* error
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 240 total points
ID: 13800577
You're right - on the face of it the code is OK, although this:

>>
         if(!(obj instanceof Department))
        {
               return 0;
        }
>>

is logically dubious. Why would you assert equality in a comparison if the objects are of a different type? You should remove that and let a ClassCastException occur if they are of different type.

In the meantime, please post the stack trace of the exception you're getting
0
 

Author Comment

by:eugene007
ID: 13800596
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)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13800608
Try changing

>>if(dept.isEmpty())

to

if(dept == null || dept.isEmpty())
0
 

Author Comment

by:eugene007
ID: 13800624
Im still getting the same error.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13800659
Are you sure you printed the right error? ;-)
0
 

Author Comment

by:eugene007
ID: 13800787
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)
0
 

Author Comment

by:eugene007
ID: 13800799
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 :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13800817
Can you tell me how to reproduce that error?
0
 

Author Comment

by:eugene007
ID: 13800848
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.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13800854
OK
0
 

Author Comment

by:eugene007
ID: 13800955
still has some problems, when there is more than one department.
0
 

Author Comment

by:eugene007
ID: 13800971
I have done further upgrades on my code, to make it efficient. You can download it from the same path.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13801066
>> still has some problems, when there is more than one department.

If you say how to reproduce this, i'll take a look
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question