Solved

Issues with casting

Posted on 2013-02-06
4
422 Views
Last Modified: 2013-02-06
My users can select data real time as long as the application is open.
So I am using a WritableList with a Listener to store user selected data.  Using the WritableList allows my Tables to be updated on the fly.  So when a user adds data to the List, the listener fires and updates my tables.  Works the same for when data is removed.

The Method I am using to add the user data to the WritableList

public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
      AplotDatasetData pp = new AplotDatasetData(tcRevision, selectedDataset);
      if (!observableList.contains(pp)) {
         observableList.add(pp);
      }
   }

Open in new window


I am using the Class AplotDatasetData to format the data to add to my List
   
   ////////////////////////////////////////////////////////////////////////   
   // Start of AplotDatasetData Class                                    // 
   ////////////////////////////////////////////////////////////////////////   
   public class AplotDatasetData {

      TCComponentItemRevision rev;
      TCComponentDataset   componentdataset;
      String markUp = "no";
              
      //////////////////////////////////////////////////////////////////////////
      //                           Constructor                                //
      //////////////////////////////////////////////////////////////////////////
      public AplotDatasetData(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
         rev = tcRevision;
         componentdataset = selectedDataset;
                  
      }// end Constructor
      
      //////////////////////
      //   getDataset()   //
      //////////////////////
      public TCComponent getDataset() {
         return componentdataset;
      }
      
      //////////////////
      //   getRev()   //
      //////////////////
      public TCComponent getRev() {
         return rev;
      }

      //////////////////
      //   equals()   //
      //////////////////
      @Override
      public boolean equals(Object o) {
         AplotDatasetData p = (AplotDatasetData) o;
         if (rev.equals(p.getRev()) && componentdataset.equals(p.getDataset())) {
            return true;
         }
         else {
           return false;
          }
      }// end equals()
   }

Open in new window


The code complies.  Even when the user is adding data.  The code works great, checks for duplicate data being entered.  But when other operations are executed, it fails with the following error.

ERROR: 11:31:00,591 - TcLogger$IC_LogListener.logging:? org.eclipse.core.runtime - org.eclipse.ui - 0 - Unhandled event loop exception org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.ClassCastExc eption: org.eclipse.core.databinding.observable.list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData)
etc .....
Caused by: java.lang.ClassCastException: org.eclipse.core.databinding.observable .list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel $AplotDatasetData at com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData.equals(A plotDataModel.java:167

So what I know.

I know it is a casting issue in the equals method.
AplotDatasetData p = (AplotDatasetData) o;

Open in new window


1.  I am not sure how to change the equals method?
2.  Am I not creating the WritableList correctly?
     There are other places in the code where I need to create a new ArrayList with the data    in the WritableList
ArrayList<AplotDataModel.AplotDatasetData> tableData = new ArrayList<AplotDataModel.AplotDatasetData>(AplotDataModel.getInstance().getObservableList());

Open in new window


The above could code could be where the equals() is being called and causing the casting error.

I am looking for any help
0
Comment
Question by:jkteater
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 38860397
if (!(o instanceof AplotDatasetData)) {
  return false;
} 

Open in new window

or something to that effect should probably start your equals method
0
 

Author Comment

by:jkteater
ID: 38860488
Thank you very much!!  The comment works perfectly.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38860507
:) It must be said that you should not normally be using equals with different types. If you are, this could indicate an error
0
 

Author Comment

by:jkteater
ID: 38860578
This is the finished product.  

 @Override
      public int hashCode() {
         final int prime = 31;
         int result = 1;
         result = prime * result + getOuterType().hashCode();
         result = prime * result
               + ((componentdataset == null) ? 0 : componentdataset.hashCode());
         result = prime * result + ((rev == null) ? 0 : rev.hashCode());
         return result;
      }

Open in new window


@Override
      public boolean equals(Object obj) {
         if (this == obj)
            return true;
         if (obj == null)
            return false;
         if (getClass() != obj.getClass())
            return false;
         AplotDatasetData other = (AplotDatasetData) obj;
         if (!getOuterType().equals(other.getOuterType()))
            return false;
         if (componentdataset == null) {
            if (other.componentdataset != null)
               return false;
         }
         else if (!componentdataset.equals(other.componentdataset))
            return false;
         if (rev == null) {
            if (other.rev != null)
               return false;
         }
         else if (!rev.equals(other.rev))
            return false;
         return true;
      }

      private AplotDataModel getOuterType() {
         return AplotDataModel.this;
      }

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Core java. Create the class with method that do logic for all elements of collection. 1 69
solarwind tftp server 2 44
eclipse argument 14 53
servlet example issue 6 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now